3

I have builed a button with two titles line by this code:

rootBntUI.titleLabel.font = [UIFont fontWithName:@"Avenir-Black" size:UserListFontSize];
[rootBntUI.layer setBorderWidth:0];
rootBntUI.titleLabel.textColor = [UIColor whiteColor];
rootBntUI.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
rootBntUI.titleLabel.textAlignment = NSTextAlignmentCenter;
rootBntUI.titleLabel.numberOfLines = 2;

Everything is working fine but how can I control line spacing of button title?

rkyr
  • 3,131
  • 2
  • 23
  • 38
Viet Nguyen
  • 2,285
  • 2
  • 26
  • 43

4 Answers4

4

You can do the styling from the xib . Use button title attributed in attribute inspector and you can set all the styling parameter along with spacing .

V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
2

I have resolved my problem, and this solution for anyone who have similar question.

        NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        [style setAlignment:NSTextAlignmentCenter];
        [style setLineBreakMode:NSLineBreakByWordWrapping];
        [style setLineSpacing:-50];

        UIFont *font1 = [UIFont fontWithName:@"Avenir-Black" size:UserListFontSize];

        NSDictionary *dict1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
                                NSFontAttributeName:font1,
                                NSParagraphStyleAttributeName:style};


        NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
        [attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", obj] attributes:dict1]];
        [FriendBnt setAttributedTitle:attString forState:UIControlStateNormal];
        [[FriendBnt titleLabel] setNumberOfLines:0];
        [[FriendBnt titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];

Happy coding.

Viet Nguyen
  • 2,285
  • 2
  • 26
  • 43
  • https://developer.apple.com/documentation/uikit/nsmutableparagraphstyle/1528742-linespacing - As you can see, the value for lineSpacing should be non-negative. Please revise your answer. – Software Engineer Sep 15 '20 at 05:46
2

This works in Swift 2 using .lineHeightMultiple to compress the title text on a button.

let style = NSMutableParagraphStyle()
    style.lineHeightMultiple = 0.8
    style.alignment = .Center
    style.lineBreakMode = .ByWordWrapping

    let dict1:[String:AnyObject] = [
        NSParagraphStyleAttributeName: style,
        NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
    ]

    let attrString = NSMutableAttributedString()
    attrString.appendAttributedString(NSAttributedString(string: "Button Text here over two lines", attributes: dict1))
    myButton.setAttributedTitle(attrString, forState: .Normal)
    myButton.titleLabel?.numberOfLines = 0
richc
  • 1,648
  • 5
  • 20
  • 48
0

What really worked for me to change line height of the UIButton title label, was this:

        NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        style.maximumLineHeight = 12.0;
        style.minimumLineHeight = 12.0;
        UIColor *colorO = [UIColor whiteColor];
        UIColor *colorD = [UIColor redColor];

        NSDictionary *firstAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:getFloatScaledFactor(13.0)],
                                          NSForegroundColorAttributeName : colorO,
                                          NSParagraphStyleAttributeName:style
                                          };
        NSDictionary *secondAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:getFloatScaledFactor(13.0)],
                                           NSForegroundColorAttributeName : colorD,
                                           NSParagraphStyleAttributeName:style
                                           };

        NSArray *textArray = [title componentsSeparatedByString:@"\n"];
        NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
        [attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", textArray[0]] attributes:firstAttributes]];
        [attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", textArray[1]] attributes:secondAttributes]];
        [self.btnRight setAttributedTitle:attString forState:UIControlStateNormal];

As a alternative solution.

santiacho
  • 46
  • 3