4

I would like to use the system font but with a custom leading, but the leading property of a UIFont is readonly. Is there a way to create a system font with a custom leading value?

I am trying to display multiple lines with a UILabel, but the default leading is too narrow. (\n\n is too wide)

If there is no way, then I need to create multiple UILabels but that seems a bit overkill.

hyn
  • 473
  • 7
  • 20
  • The UILabel is not what you want. UILabel does not even obey the leading in the UIFont. – jamie Aug 24 '11 at 10:45

3 Answers3

2

I think you can't do that. UIFont is pretty tightly constrained.

If you have the linebreaks in place yourself then it's fairly trivial to draw the string yourself rather than creating multiple UILabels. You can generating sub-strings using – componentsSeparatedByString: and rendering each of them at your specified leading increments using the UIKit NSString addition: – drawAtPoint:withFont:.

If you need to generate your own wrapping, it's difficult - see this related question.

Community
  • 1
  • 1
Rog
  • 17,070
  • 9
  • 50
  • 73
1

There is a way to customize the line spacing in a multi line UILabel. Instead of using the text attribute of the label, use the attributedText property. The attributed text can have a paragraph style with a negative lineSpacing value to make the lines a closer together. Here is an example:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = -4;

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:yourStringHere];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, titleText.length)];
yourUILabel.attributedText = attributedString;
progrmr
  • 75,956
  • 16
  • 112
  • 147
-2

It could be done by crating a custom category on UIFont and replacing property for lading with custom setter. I am still trying to do this.

Miroslav Kovac
  • 1,168
  • 1
  • 16
  • 27