简体   繁体   中英

How to Underline text and color of uibutton in iOS

I have one simple query, i need to underline UIButton text and color. I have under line text for uibutton, however regarding tint color its not working. I have set tint color from xib, its not taking from there.Below is my code.

 NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"Testing"];
[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];
[_helpWithLoginLabel setAttributedTitle:commentString forState:UIControlStateNormal];

There is a separate attribute for textColor ( NSForegroundColorAttributeName ). Here's an example:

NSArray * keys = [[NSArray alloc] initWithObjects:NSForegroundColorAttributeName, NSUnderlineStyleAttributeName, nil];
NSArray * objects = [[NSArray alloc] initWithObjects:self.descriptionTextView.textColor, [NSNumber numberWithInt:NSUnderlineStyleSingle], nil];
NSDictionary * linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
self.descriptionTextView.linkTextAttributes = linkAttributes;

Hope this helps...

In Swift:

let attributes = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
let attributedText = NSAttributedString(string: button.currentTitle!, attributes: attributes)

button.titleLabel?.attributedText = attributedText

Same response as @dequin using literals do get shorter syntax :

NSDictionary * linkAttributes = @{NSForegroundColorAttributeName:button.titleLabel.textColor, NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:button.titleLabel.text attributes:linkAttributes];            
[button.titleLabel setAttributedText:attributedString];

Based on Alfie's answer I concocted this solution:

NSArray * objects = [[NSArray alloc] initWithObjects:self.aButton.titleLabel.textColor, [NSNumber numberWithInt:NSUnderlineStyleSingle], nil];
NSArray * keys = [[NSArray alloc] initWithObjects:NSForegroundColorAttributeName, NSUnderlineStyleAttributeName, nil];

NSDictionary * linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.aButton.titleLabel.text attributes:linkAttributes];

[self.aButton.titleLabel setAttributedText:attributedString];

Try this:-

 extension UIButton {

    func UnderlineTextButton(title: String?, forState state: UIControlState)
    {
        self.setTitle(title, for: .normal)
        self.setAttributedTitle(self.attributedString(), for: .normal)
    }

    private func attributedString() -> NSAttributedString? {
        let attributes = [
            NSFontAttributeName : UIFont.systemFont(ofSize: 12.0),
            NSForegroundColorAttributeName : UIColor.black,
            NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue
            ] as [String : Any]
        let attributedString = NSAttributedString(string: self.currentTitle!, attributes: attributes)
        return attributedString
    }
}

Uses:-

button.UnderlineTextButton(title: "Click Here", forState:
UIControlState.normal)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM