UI按钮标题行间距

时间:2014-06-30 03:28:50

标签: ios objective-c xcode uibutton

我已经通过此代码构建了一个带有两个标题行的按钮:

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;

一切正常但我如何控制按钮标题的行间距?

4 个答案:

答案 0 :(得分:4)

您可以使用xib进行样式设置。在属性检查器中使用按钮标题属性,您可以设置所有样式参数以及间距。

答案 1 :(得分:2)

我已经解决了我的问题,这个解决方案适用于任何有类似问题的人。

        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];

快乐的编码。

答案 2 :(得分:0)

这适用于Swift 2,使用.lineHeightMultiple来压缩按钮上的标题文本。

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

答案 3 :(得分:0)

更改UIButton标题标签的行高对我真正起作用的是:

        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];

作为替代解决方案。