Reputation: 1257
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];
Upvotes: 10
Views: 16302
Reputation: 636
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)
Upvotes: 1
Reputation: 926
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];
Upvotes: 5
Reputation: 6697
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];
Upvotes: 7
Reputation: 10215
In Swift:
let attributes = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
let attributedText = NSAttributedString(string: button.currentTitle!, attributes: attributes)
button.titleLabel?.attributedText = attributedText
Upvotes: 10
Reputation: 17104
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...
Upvotes: 7