如何在UIButton的prepareForInterfaceBuilder上检索titleLabel?

时间:2015-10-24 02:14:11

标签: ios xcode uibutton interface-builder

我已将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)。

1 个答案:

答案 0 :(得分:0)

你可以直接将Attibuted字符串设置为UIButton的Lable

library(data.table)
setDT(df1)[, index:= cumsum(c(TRUE,abs(diff(as.Date(date, '%d-%m-%Y')))!=1))]
head(df1, 12)
#          date torn_count torn_dpi index
# 1: 31-12-2014          1    0.001     1
# 2: 28-12-2014          3    0.003     2
# 3: 27-12-2014          1    0.001     2
# 4: 22-12-2014          1    0.001     3
# 5: 21-12-2014          1    0.001     3
# 6: 20-12-2014          1    0.001     3
# 7: 19-12-2014          1    0.001     3
# 8: 18-12-2014          1    0.001     3
# 9: 17-12-2014          3    0.003     3
#10: 15-12-2014          2    0.002     4
#11: 14-12-2014          1    0.001     4
#12: 13-12-2014          1    0.002     4