我将UIButton
细分为子类,并具有以下prepareForInterfaceBuilder
函数:
- (void)prepareForInterfaceBuilder {
NSString *string = [self titleForState:UIControlStateNormal];
NSAttributedString *attrString = [[NSAttributedString alloc]
initWithString:string
attributes:@{
NSKernAttributeName : @4,
NSForegroundColorAttributeName : [UIColor whiteColor]
}];
[self setAttributedTitle:attrString forState:UIControlStateNormal];
}
在Interface Builder中查看时,它会崩溃,因为即使我在Interface Builder中将“testing”设置为标题,字符串变量也将返回nil。我也尝试通过调用
self.titleLabel.text
来检索文本,但这也导致了nil
。如何在此函数中正确检索titleLabel的文本?
当前使用Xcode 7.1(7B91b)。
最佳答案
您可以直接将属性字符串设置为UIButton标签
NSString *string = @"Your text here";
NSAttributedString *attrString = [[NSAttributedString alloc]
initWithString:string
attributes:@{
NSKernAttributeName : @4,
NSForegroundColorAttributeName : [UIColor whiteColor]
}];
[yourCustomButton setAttributedTitle:attrString forState:UIControlStateNormal];
关于ios - 如何在UIButton的prepareForInterfaceBuilder上检索titleLabel?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33313842/