Change the titleLabel of UIButton which title type is Attributed in Storyboard / Xib file


Issue :
I have use below code to change Title and set underline of UIButton at run time.
[btnTemp setTitle:@"Test" forState:UIControlStateNormal];

I have got below  issues:
1. Underline is not showing
2. Title not change
3. Title color is display default black.

To overcome this issue I have implement below code:

NSDictionary *attribs = @{
                              NSForegroundColorAttributeName: _btnTemp.titleLabel.textColor,
                              NSFontAttributeName: _btnTemp.titleLabel.font,
                              NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)
                              };
NSAttributedString *strAttributed = [[NSAttributedString alloc] initWithString:@"Test" attributes:attribs];

[btnTemp setAttributedTitle:strAttributed forState:UIControlStateNormal];

You may also like

Leave a Reply