Objective C label line spacing?
Asked Answered
M

3

7

Is there a way to set the distance of two lines within a UILabel? I tried to do it within Interface Builder but without success.

Mickens answered 19/4, 2017 at 14:16 Comment(1)
You can do it with NSAttributedString with NSParagraphStyle by settings its lineSpacing.Isotone
I
11

The code you want will be something like this:

NSMutableAttributedString* attrString = [[NSMutableAttributedString  alloc] initWithString:@"Sample text"];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:24];
[attrString addAttribute:NSParagraphStyleAttributeName
    value:style
    range:NSMakeRange(0, strLength)];
uiLabel.attributedText = attrString;
Ie answered 19/4, 2017 at 14:22 Comment(0)
S
8

You can use NSAttributedString to add spacing between two lines within a UILabel:

NSString *labelText = @"My String"; 
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:20];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
cell.label.attributedText = attributedString ;

OR

If you are using storyboard then you can control line spacing in the storyboard by selecting text type is attributed and add spacing value:

Spellbound answered 19/4, 2017 at 14:35 Comment(0)
R
0

Since iOS 6, Apple added NSAttributedString to UIKit, making it possible to use NSParagraphStyle to change the line spacing.

Alternatively, you can do this via Storyboards using Attributed Text and then clicking the ... symbol. See link below for screenshot.

https://i.sstatic.net/aiNfR.png

Romney answered 19/4, 2017 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.