0

currently, I am creating a table view that contains an imageView as main UI item inside the cell and on top op that there is a label. Now I'm facing an issue where for long text, the frame of the label is not accurate, and for a long text it is taking a lot of space and it seems like there is a line break but there isn't, this is the issue:

Label issue

This are the properties of the label Label properties

This is my ViewController and the constraints that I'm using View Controller on Storyboard

Additional Details

  1. For the Cell, I'm not adding extra code, I just have the IBOutlets and nothing more.
  2. On the CellForRowAtIndexPath, I'm not doing additional code more than setting the image and setting the text.

Thanks

Hiren Dhamecha
  • 658
  • 5
  • 15
  • 1
    Have you tried running it on the actual device? – PGDev Sep 16 '20 at 05:55
  • Does this answer your question? [How to control the line spacing in UILabel](https://stackoverflow.com/questions/5494498/how-to-control-the-line-spacing-in-uilabel) – Roman Podymov Sep 16 '20 at 07:03

2 Answers2

0

Try to change Content hugging Priority of Label

Jai
  • 11
0

The font you've selected - Noto Nastaliq Urdu Bold - has very specific type layout properties.

Compare it with the default system font.

Using this code:

    let fontDefault = UIFont.systemFont(ofSize: 17.0, weight: .bold)
    guard let fontNotoBld = UIFont(name: "NotoNastaliqUrdu-Bold", size: 17) else {
        fatalError("Could not create font!")
    }

    print("fontDefault.lineHeight:\t",fontDefault.lineHeight)
    print("fontNotoBld.lineHeight:\t",fontNotoBld.lineHeight)
    print()
    
    print("fontDefault.ascender:\t",fontDefault.ascender)
    print("fontNotoBld.ascender:\t",fontNotoBld.ascender)
    print()

    print("fontDefault.descender:\t",fontDefault.descender)
    print("fontNotoBld.descender:\t",fontNotoBld.descender)
    print()

    print("fontDefault.xHeight:\t",fontDefault.xHeight)
    print("fontNotoBld.xHeight:\t",fontNotoBld.xHeight)
    print()

    print("fontDefault.capHeight:\t",fontDefault.capHeight)
    print("fontNotoBld.capHeight:\t",fontNotoBld.capHeight)
    print()

we get this output in the debug console:

fontDefault.lineHeight:  20.287109375
fontNotoBld.lineHeight:  42.5

fontDefault.ascender:    16.1865234375
fontNotoBld.ascender:    32.368

fontDefault.descender:   -4.1005859375
fontNotoBld.descender:   -10.132000000000001

fontDefault.xHeight:     9.13916015625
fontNotoBld.xHeight:     9.112

fontDefault.capHeight:   11.97802734375
fontNotoBld.capHeight:   12.138000000000002

I don't know exactly why, but my assumption is that Noto Nastaliq Urdu supports many more language characters which often have very different shapes and heights.

You can either use a more standard font, or design your UI to accommodate the bigger spacing.


Edit

You could try using attributed text with line height settings. However, it will affect the top spacing of the text to a point that it may not suffice.

Here's an example with 3 labels:

  • top label uses default settings

  • second label changes maximumLineHeight to 17

  • third label changes maximumLineHeight to 26

      let titleText = "Label with enough text to cause word wrapping onto multiple lines."
    
      guard let fontNotoBold = UIFont(name: "NotoNastaliqUrdu-Bold", size: 17) else {
          fatalError("Could not create font!")
      }
    
      // without paragraphStyle attribute
      var attrString = NSMutableAttributedString(string: titleText)
      attrString.addAttribute(NSAttributedString.Key.font, value: fontNotoBold, range:NSMakeRange(0, attrString.length))
      label1.attributedText = attrString
    
      // with paragraphStyle max line height: 17
      let ps2 = NSMutableParagraphStyle()
      ps2.maximumLineHeight = 17
      attrString = NSMutableAttributedString(string: titleText)
      attrString.addAttribute(NSAttributedString.Key.font, value: fontNotoBold, range:NSMakeRange(0, attrString.length))
      attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:ps2, range:NSMakeRange(0, attrString.length))
      label2.attributedText = attrString
    
      // with paragraphStyle max line height: 26
      let ps3 = NSMutableParagraphStyle()
      ps3.maximumLineHeight = 26
      attrString = NSMutableAttributedString(string: titleText)
      attrString.addAttribute(NSAttributedString.Key.font, value: fontNotoBold, range:NSMakeRange(0, attrString.length))
      attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:ps3, range:NSMakeRange(0, attrString.length))
      label3.attributedText = attrString
    

Here's the result:

enter image description here

With a little more experimentation, such as adding a newline ("\n") at the beginning, you might be able to get it to a satisfactory appearance.

DonMag
  • 69,424
  • 5
  • 50
  • 86