首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在UIButton中为文本加下划线

在UIButton中为文本加下划线
EN

Stack Overflow用户
提问于 2010-04-13 21:46:35
回答 14查看 100.1K关注 0票数 150

有没有人能给UIButton的标题加下划线?我有一个自定义类型的UIButton,我希望标题带下划线,但界面生成器没有提供任何这样做的选项。

在Interface Builder中,当您为按钮选择字体选项时,它提供了选择None、Single、Double、Color的选项,但这些选项都没有提供对按钮上的标题的任何更改。

感谢您的帮助。

EN

回答 14

Stack Overflow用户

回答已采纳

发布于 2010-10-02 00:40:57

UIUnderlinedButton.h

代码语言:javascript
复制
@interface UIUnderlinedButton : UIButton {

}


+ (UIUnderlinedButton*) underlinedButton;
@end

UIUnderlinedButton.m

代码语言:javascript
复制
@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
票数 79
EN

Stack Overflow用户

发布于 2013-11-14 13:13:09

它使用属性字符串非常简单

创建具有设置属性的字典并应用于属性化字符串。然后,您可以将属性字符串设置为uibutton中的attibutedtitle或uilabel中的属性文本。

代码语言:javascript
复制
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];
票数 29
EN

Stack Overflow用户

发布于 2015-06-27 03:21:29

这是我的函数,在Swift 1.2中工作。

代码语言:javascript
复制
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)
}

更新Swift 3.0扩展:

代码语言:javascript
复制
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)
    }
}
票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2630004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档