Reputation: 7031
I subclassed UIButton
and have this following prepareForInterfaceBuilder
function:
- (void)prepareForInterfaceBuilder {
NSString *string = [self titleForState:UIControlStateNormal];
NSAttributedString *attrString = [[NSAttributedString alloc]
initWithString:string
attributes:@{
NSKernAttributeName : @4,
NSForegroundColorAttributeName : [UIColor whiteColor]
}];
[self setAttributedTitle:attrString forState:UIControlStateNormal];
}
It's crashing when viewing in Interface Builder since the string variable is returning nil even though I have "testing" set as the title in Interface Builder. I have also tried retrieving the text by calling self.titleLabel.text
, but that's also resulting in nil
.
How do I properly retrieve the titleLabel's text in this function? Currently using Xcode 7.1 (7B91b).
Upvotes: 2
Views: 158
Reputation: 2999
you can set directly Attibuted string to UIButton's Lable
NSString *string = @"Your text here";
NSAttributedString *attrString = [[NSAttributedString alloc]
initWithString:string
attributes:@{
NSKernAttributeName : @4,
NSForegroundColorAttributeName : [UIColor whiteColor]
}];
[yourCustomButton setAttributedTitle:attrString forState:UIControlStateNormal];
Upvotes: 0