UILabel second line indent

So, I have UILabel, which may or may not go to the second line, depending on whether it is on the iPhone or iPad. What I would like to do is to indent the second line, if necessary, to line up correctly.

The iPad will almost never need a second line break, and depending on which iPhone it runs on, it may or may not. Therefore, in fact, I need a way to dynamically indent the second line only when there is a second line.

+8
ios objective-c uilabel
source share
1 answer

Use NSAttributedString for your shortcut and set headIndent its paragraph style.

 NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; style.headIndent = 14; NSDictionary *attributes = @{ NSParagraphStyleAttributeName: style }; NSAttributedString *richText = [[NSAttributedString alloc] initWithString:@"So this UILabel walks into a bar…" attributes:attributes]; self.narrowLabel.attributedText = richText; self.wideLabel.attributedText = richText; 

Result:

example result

+21
source share

All Articles