0

这是一个简单的示例自定义按钮,它在 IB 中设置为按钮类:

#import "TestButton.h"
@implementation TestButton

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    UIFont * font = [UIFont systemFontOfSize:8];
    if ( self.setFont )
        self.titleLabel.font = font;
    self.titleLabel.textColor = [UIColor redColor];
}

@end

如果 setFont 为 false,即字体不变,标签文字颜色如预期为红色。但如果它是真的,那么按钮文本颜色就是它在 IB 中设置的任何颜色。

所以问题是这里发生了什么,以及如何更改在 IB 中以编程方式分配的按钮的文本和字体。

如果有人想看看这些特性以及 IBDesignable 有多时髦,请参阅演示项目

4

2 回答 2

0

代替

self.titleLabel.textColor = [UIColor redColor];

[self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
于 2015-03-24T21:45:29.490 回答
0

以下代码用于设置文本:

[self setTitle:@"New Text" forState:UIControlStateNormal];

如果您想在按钮文件本身以外的任何地方设置标题或标题颜色,您可以尝试向按钮添加两个公共属性。一个用于标题 (self.titleString) 的 NSString 属性和一个用于标题颜色的 UIColor 属性。然后,您可以在视图控制器或视图中的任何位置设置标题和/或标题颜色。然后,您将在按钮文件中使用这些属性,例如

[self setTitle:self.titleString forState:UIControlStateNormal];
于 2015-03-24T22:06:52.270 回答