How to implement UILabel line spacing using xib?
Solution 1
Yes it is possible to adjust line spacing via xib. Click on UIlabel on xib,then change ‘text’ property type to ’Attributed’,then go through following images. Then enter new line(press ctrl-return shortcut key)and keep cursor in between two lines then adjust ‘line’ property and ‘Max Height’ property you want. My UILael text is "Hello,how are you?"
Solution 2
Create this simple UILabel subclass:
@interface CustomLabel : UILabel
@property (assign, nonatomic) CGFloat myLineSpacing;
@end
@implementation CustomLabel
- (void)setMyLineSpacing:(CGFloat)myLineSpacing {
_myLineSpacing = myLineSpacing;
self.text = self.text;
}
- (void)setText:(NSString *)text {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = _myLineSpacing;
paragraphStyle.alignment = self.textAlignment;
NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyle};
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
attributes:attributes];
self.attributedText = attributedText;
}
Set myLineSpacing
property value with IB.
Still you cannot preview in IB, but line spacing value is in xib!
You can set label.text
in your code without caring about line spacing.
Note:
don't make property name "lineSpacing
".
UILabel do have undocumented lineSpacing property, and overriding that breaks something.
Solution 3
*From Interface Builder:**
Programmatically:
SWift 4
Using label extension
extension UILabel {
// Pass value for any one of both parameters and see result
func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) {
guard let labelText = self.text else { return }
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing
paragraphStyle.lineHeightMultiple = lineHeightMultiple
let attributedString:NSMutableAttributedString
if let labelattributedText = self.attributedText {
attributedString = NSMutableAttributedString(attributedString: labelattributedText)
} else {
attributedString = NSMutableAttributedString(string: labelText)
}
// Line spacing attribute
attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
self.attributedText = attributedString
}
}
Now call extension function
let label = UILabel()
let stringValue = "How\nto\nimplement\nUILabel\nline\nspacing\nusing\nxib?"
// Pass value for any one argument - lineSpacing or lineHeightMultiple
label.setLineSpacing(lineSpacing: 2.0) . // try values 1.0 to 5.0
// or try lineHeightMultiple
//label.setLineSpacing(lineHeightMultiple = 2.0) // try values 0.5 to 2.0
Or using label instance (Just copy & execute this code to see result)
let label = UILabel()
let stringValue = "How\nto\nimplement\nUILabel\nline\nspacing\nusing\nxib?"
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
// Line spacing attribute
attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
// Character spacing attribute
attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length))
label.attributedText = attrString
IKKA
Updated on June 04, 2022Comments
-
IKKA almost 2 years
I want to align a
UILabel
's text vertically with line spacing via xib in iOS, for example:hello, how are you?
Please help me.
-
IKKA almost 10 yearshow to increase line space between "hello" and "This is"?
-
Subhransu almost 10 years@user Just add space in the text in TextEdit and paste it here. You can't edit the text directly in Xib. So type it in a text editor exactly how you want and then paste in the text section of your Xib.
-
Dipu Rajak almost 10 yearswhile setting text you must do myLabel.attributedText = @"My texts....". This will not work-->myLabel.text = @"...."
-
user1139921 about 9 years"Note: don't make property name "lineSpacing". UILabel do have undocumented lineSpacing property, and overriding that breaks something." @rintaro this really helps
-
ssh88 over 8 yearsCouldn't get this to work for a month. de-prioritised this feature, had character spacing, underlining etc working. Finally, on the eve of our UAT thought i would have another crack at this and came across that simple 'note' and it solved it :| what a pain that thats not more obvious. Thanks @rintaro