RVN*_*RVN 135 formatting text uibutton
任何人都可以建议如何强调UIButton的标题?我有一个自定义类型的UIButton,我希望标题有下划线,但Interface Builder没有提供任何选项.
在Interface Builder中为按钮选择字体选项时,它提供了选择无,单,双,颜色的选项,但这些选项都不会对按钮上的标题进行任何更改.
任何帮助赞赏.
fin*_*elp 370
要使用界面构建器加下划线,必须:

其他人制作的视频 https://www.youtube.com/watch?v=5-ZnV3jQd9I
Nic*_*247 127
从iOS6开始,现在可以使用NSAttributedString以更灵活的方式执行下划线(以及其他任何归因于字符串支持的内容):
NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"The Quick Brown Fox"];
[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];
[button setAttributedTitle:commentString forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
注意:将此添加为另一个答案 - 因为它与我之前的解决方案完全不同.
编辑: 奇怪(至少在iOS8中)你必须为第一个字符加下划线,否则它不起作用!
所以作为一种解决方法,将第一个char设置为带有明确颜色的下划线!
// underline Terms and condidtions
NSMutableAttributedString* tncString = [[NSMutableAttributedString alloc] initWithString:@"View Terms and Conditions"];
// workaround for bug in UIButton - first char needs to be underlined for some reason!
[tncString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:(NSRange){0,1}];
[tncString addAttribute:NSUnderlineColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, 1)];
[tncString addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:(NSRange){5,[tncString length] - 5}];
[tncBtn setAttributedTitle:tncString forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
Nic*_*247 78
UIUnderlinedButton.h
@interface UIUnderlinedButton : UIButton {
}
+ (UIUnderlinedButton*) underlinedButton;
@end
Run Code Online (Sandbox Code Playgroud)
UIUnderlinedButton.m
@implementation UIUnderlinedButton
+ (UIUnderlinedButton*) underlinedButton {
UIUnderlinedButton* button = [[UIUnderlinedButton alloc] init];
return [button autorelease];
}
- (void) drawRect:(CGRect)rect {
CGRect textRect = self.titleLabel.frame;
// need to put the line at top of descenders (negative value)
CGFloat descender = self.titleLabel.font.descender;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
// set to same colour as text
CGContextSetStrokeColorWithColor(contextRef, self.titleLabel.textColor.CGColor);
CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender);
CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);
CGContextClosePath(contextRef);
CGContextDrawPath(contextRef, kCGPathStroke);
}
@end
Run Code Online (Sandbox Code Playgroud)
Rin*_*nku 28
使用属性字符串非常简单
创建具有set属性的字典并应用于属性字符串.然后,您可以将uibutton中的属性字符串设置为attibutedtitle或uilabel中的attributestext.
NSDictionary *attrDict = @{NSFontAttributeName : [UIFont
systemFontOfSize:14.0],NSForegroundColorAttributeName : [UIColor
whiteColor]};
NSMutableAttributedString *title =[[NSMutableAttributedString alloc] initWithString:@"mybutton" attributes: attrDict];
[title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0,[commentString length])]; [btnRegLater setAttributedTitle:title forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
Ada*_*nic 20
这是我的功能,适用于Swift 1.2.
func underlineButton(button : UIButton, text: String) {
var titleString : NSMutableAttributedString = NSMutableAttributedString(string: text)
titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(0, count(text.utf8)))
button.setAttributedTitle(titleString, forState: .Normal)
}
Run Code Online (Sandbox Code Playgroud)
更新Swift 3.0扩展:
extension UIButton {
func underlineButton(text: String) {
let titleString = NSMutableAttributedString(string: text)
titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, text.characters.count))
self.setAttributedTitle(titleString, for: .normal)
}
}
Run Code Online (Sandbox Code Playgroud)
ann*_*nie 13
尼克的答案是一个很好的,快速的方法来做到这一点.
我添加了drawRect对阴影的支持.
如果您的按钮标题在文本下方有阴影,则Nick的回答不会考虑:

但你可以将下划线向下移动阴影的高度,如下所示:
CGFloat descender = self.titleLabel.font.descender;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGFloat shadowHeight = self.titleLabel.shadowOffset.height;
descender += shadowHeight;
Run Code Online (Sandbox Code Playgroud)
然后你会得到这样的东西:

Max*_*tov 10
截至 2019 年 9 月在 Xcode 10.3 中运行的 Swift 5.0 版本:
extension UIButton {
func underlineText() {
guard let title = title(for: .normal) else { return }
let titleString = NSMutableAttributedString(string: title)
titleString.addAttribute(
.underlineStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSRange(location: 0, length: title.count)
)
setAttributedTitle(titleString, for: .normal)
}
}
Run Code Online (Sandbox Code Playgroud)
要使用它,请先设置按钮标题button.setTitle("Button Title", for: .normal),然后调用button.underlineText()使该标题带有下划线。
对于Swift 3,可以使用以下扩展名:
extension UIButton {
func underlineButton(text: String) {
let titleString = NSMutableAttributedString(string: text)
titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, text.characters.count))
self.setAttributedTitle(titleString, for: .normal)
}
}
Run Code Online (Sandbox Code Playgroud)