UIbutton 标题行间距
Posted
技术标签:
【中文标题】UIbutton 标题行间距【英文标题】:UIbutton title line spacing 【发布时间】: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;
一切正常,但如何控制按钮标题的行距?
【问题讨论】:
【参考方案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];
编码愉快。
【讨论】:
developer.apple.com/documentation/uikit/nsmutableparagraphstyle/… - 如您所见,lineSpacing 的值应该是非负数。请修改您的答案。【参考方案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];
作为替代解决方案。
【讨论】:
以上是关于UIbutton 标题行间距的主要内容,如果未能解决你的问题,请参考以下文章