Underlining text in UIButton

后端 未结 18 1811
终归单人心
终归单人心 2020-11-29 15:56

Can anyone suggest how to underline the title of a UIButton ? I have a UIButton of Custom type, and I want the Title to be underlined, but the Interface Builder does not pr

相关标签:
18条回答
  • 2020-11-29 16:23

    From iOS6 it is now possible to use an NSAttributedString to perform underlining (and anything else attributed strings support) in a much more flexible way:

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

    Note: added this as another answer - as its a totally different solution to my previous one.

    Edit: oddly (in iOS8 at least) you have to underline the first character otherwise it doesn't work!

    so as a workaround, set the first char underlined with clear colour!

        // 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];
    
    0 讨论(0)
  • 2020-11-29 16:25

    Nick's answer is a great, quick way to do this.

    I added support in drawRect for shadows.

    Nick's answer doesn't take into account if your button title has a shadow below the text:

    enter image description here

    But you can move the underline down by the height of the shadow like so:

    CGFloat descender = self.titleLabel.font.descender;
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    CGFloat shadowHeight = self.titleLabel.shadowOffset.height;
    descender += shadowHeight;
    

    Then you'll get something like this:

    enter image description here

    0 讨论(0)
  • 2020-11-29 16:30

    To use interface builder to underline, one has to:

    • Change it to attributed
    • Highlight the text in the Attributes inspector
    • Right click, choose Font and then Underline

    Underline using IB

    Video someone else made https://www.youtube.com/watch?v=5-ZnV3jQd9I

    0 讨论(0)
  • 2020-11-29 16:31

    Nick H247's answer but Swift approach:

    import UIKit
    
    class UnderlineUIButton: UIButton {
    
        override func drawRect(rect: CGRect) {
            super.drawRect(rect)
    
            let textRect = self.titleLabel!.frame
    
            var descender = self.titleLabel?.font.descender
    
            var contextRef: CGContextRef = UIGraphicsGetCurrentContext();
    
            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);
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:34

    For Swift 3 the following extension can be used:

    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)
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:35

    In swift

    func underlineButton(button : UIButton) {
    
    var titleString : NSMutableAttributedString = NSMutableAttributedString(string: button.titleLabel!.text!)
    titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(0, button.titleLabel!.text!.utf16Count))
    button.setAttributedTitle(titleString, forState: .Normal)}
    
    0 讨论(0)
提交回复
热议问题