【问题标题】:UIbutton title line spacingUIbutton 标题行间距
【发布时间】:2014-06-30 03:28:49
【问题描述】:

我用这段代码构建了一个带有两个标题行的按钮:

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;

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

【问题讨论】:

    标签: ios objective-c xcode uibutton


    【解决方案1】:

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

    【讨论】:

    • 感谢您的快速回复,但我会解决我的问题!再次感谢。
    【解决方案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];
    

    编码愉快。

    【讨论】:

    【解决方案3】:

    这适用于 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
    

    【讨论】:

      【解决方案4】:

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

      作为替代解决方案。

      【讨论】: