在UIButton中加下划线

RVN*_*RVN 135 formatting text uibutton

任何人都可以建议如何强调UIButton的标题?我有一个自定义类型的UIButton,我希望标题有下划线,但Interface Builder没有提供任何选项.

在Interface Builder中为按钮选择字体选项时,它提供了选择无,单,双,颜色的选项,但这些选项都不会对按钮上的标题进行任何更改.

任何帮助赞赏.

fin*_*elp 370

要使用界面构建器加下划线,必须:

  • 将其更改为属性
  • 突出显示"属性"检查器中的文本
  • 右键单击,选择"字体"然后"下划线"

使用IB强调

其他人制作的视频 https://www.youtube.com/watch?v=5-ZnV3jQd9I

  • 好问题@ new2ios也许别人都知道 (2认同)
  • 它应该标记为真.完美的亲爱的. (2认同)
  • finneydonehelped!谢谢你!无法弄清楚为什么字体弹出对话框没有效果.右键单击是完美的. (2认同)
  • Interface Builder用户可以很好地回答这种简单的事情,这些事情在使用Code时有点工作.谢谢!(Y) (2认同)

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)

  • 请注意,当您这样做时,您还必须添加颜色的属性,因为属性标题文本将不使用您使用setTitleColor设置的颜色:forState: (9认同)
  • 太棒了,感谢@daveMac对色彩的追求.对于那些不知道属性的人:NSForegroundColorAttributeName (2认同)

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)

  • 谢谢,我最终调用它如下:UIButton*btn = [UIUnderlinedButton buttonWithType:UIButtonTypeCustom]; (4认同)
  • 你也可以像这样覆盖`setTitle`方法:```objective-c - (void)setTitle:(NSString*)title forState:(UIControlState)state {[super setTitle:title forState:state]; [self setNeedsDisplay]; }``` (4认同)
  • 代码运行良好,但我注意到当视图改变旋转大小时,下划线没有收缩/增长,这是由于'drawRect`没有在旋转时被调用引起的.这可以通过将按钮设置为重绘来解决:`myButton.contentMode = UIViewContentModeRedraw;`强制按钮在边界更改时重绘. (2认同)

Lin*_*han 45

您可以在界面构建器本身中执行此操作.

  1. 选择属性检查器
  2. 将标题类型从普通更改为属性

在此输入图像描述

  1. 设置适当的字体大小和文本对齐方式

在此输入图像描述

  1. 然后选择标题文本并将字体设置为带下划线

在此输入图像描述


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()使该标题带有下划线。


Dur*_*lli 5

对于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)