2

I have two UILabels named headingLabel and descriptionLabel whose number of lines is 3 and 5 respectively. Now I want the font-size of the text to change and fit into the numberOfLines where the text varies based on some networking json data received.

image for text in iphone5s image for text in iphoneX

 here is my code

    let headingLabel : UILabel = {
        let hl = UILabel()
        hl.translatesAutoresizingMaskIntoConstraints = false
        hl.text = "Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor"

        hl.textColor = .black
        hl.lineBreakMode = NSLineBreakMode.byWordWrapping
        hl.numberOfLines = 3;
        hl.sizeToFit()
        hl.clipsToBounds = true
        hl.font = hl.font.withSize(20)

        return hl
    }()

    let descriptionLabel : UILabel = {
        let dl = UILabel()
        dl.translatesAutoresizingMaskIntoConstraints = false
        dl.text = "Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolorLorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor"
        dl.textColor = .black
        dl.lineBreakMode = NSLineBreakMode.byWordWrapping
        dl.numberOfLines = 5
        dl.font = dl.font.withSize(18)
        dl.sizeToFit()
        dl.clipsToBounds = true
        dl.alpha = 0.8
        return dl
    }()
rmaddy
  • 314,917
  • 42
  • 532
  • 579
kamalakshi hegde
  • 101
  • 1
  • 10

2 Answers2

1

Use Autoshrink to adjust text into label frame.

Authoshrink with Minimum Font Size

enter image description here

Authoshrink with Minimum Font Scale

enter image description here

Apple document about UILabel - Autoshrink

Determines whether the label adjusts the appearance of the text before resorting to truncation. Choose Minimum Font Scale and enter a value to allow the label to reduce the font size to fit the text. Enable Tighten Letter Spacing to allow the label to reduce intercharacter spacing. Access these values at runtime with the minimumScaleFactor and allowsDefaultTighteningForTruncation properties, respectively.

Community
  • 1
  • 1
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • 1
    I would just add: note that the Line Break : Truncate Tail setting is important for this to work. Mere Word Wrap line breaking will not behave this way. Also note that there is _no guarantee_ that the text will shrink enough for it all to fit (as indeed in the last example of your screen shot, it does not fit: it is truncated instead). – matt May 31 '18 at 16:35
  • @matt - You are welcomed to improve, quality of this answer. Please add your input by editing this answer. About last example, I've just shown a way, how an OP can solve her problem. She can reduce font scale to fit text into frame, according to text size visibility requirement. – Krunal May 31 '18 at 17:24
  • 1
    Well, let's let the comment stand as a comment, then. I really don't want to detract from your answer in any way. – matt May 31 '18 at 17:48
0

Please give:

dl.numberOfLines =  0

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Hardik Bar
  • 1,660
  • 18
  • 27