1

I have mentioned my code below where I want to adjust my label height according to the text. In the picture below, black background is the label and this label is inserted into a UIScrollView. Label is aligned with the top left corner of the scroll view but when I run my app following is the display of view controller i.e. label has gone far below the top left corner of the scrollview. Please help me in this matter enter image description here

func getLabelsHeightAccordingToTheTextContent() -> CGFloat
        {

        let label:UILabel = UILabel(frame: CGRectMake(0, 0, 250, CGFloat.max))
        label.numberOfLines = 0
        label.font = outletLabel.font
        label.text = outletLabel.text
        label.lineBreakMode = NSLineBreakMode.ByWordWrapping

        var fontName: String = outletLabel.font.fontName

        label.font = UIFont (name: fontName, size: outletLabel.font.pointSize)


        label.sizeToFit()
        return label.frame.height
    }

when i get the height of label, I set the height of scrollview

let height = getLabelsHeightAccordingToTheTextContent()
        let width = outletScrollView.frame.width

following are the properties of the label.................................... enter image description here

Muneeb Rehman
  • 107
  • 1
  • 7

1 Answers1

1

The easiest way to add scrolling functionality for text is to use a UITextView. The setup is more or less the same as a label but scrolling is built in.

One thing to note is that users are allowed to edit the text by default, so either in the storyboard or programmatically you should set editable to false for your textView.

Olivier Wilkinson
  • 2,646
  • 11
  • 16