uibutton字体子类

时间:2012-06-25 17:23:42

标签: cocoa uibutton

我的UIButton类中有以下方法:

- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
    // Initialization code

    self.titleLabel.font = [UIFont fontWithName:@"Chalkduster" size:16];
    self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;

    self.titleLabel.textColor = [UIColor greenColor];

}
return self;
}

效果很好,除了我看不到按钮周围的边框:任何想法为什么?为了让按钮边框显示,我需要做些什么?

谢谢

1 个答案:

答案 0 :(得分:1)

不要子类按钮在IB中做所有事情会更好。这就是你如何实现你想要的目标:

选择按钮 - >转到属性检查器 - > 类型 - >选择圆角矩形。这将为您提供默认按钮外观。

然后选择 font (按 T 信件),如下所示:

Screenshot

同时选择Font后面的文字颜色。

<强>结果:

Screenshot 2

修改

以编程方式创建按钮:

//create the button with RoundedRect type
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//set the position of the button
button.frame = CGRectMake(100, 150, 100, 30);

//set the button's title
[button setTitle:@"Click Me!" forState:UIControlStateNormal];

//set the button's font
button.titleLabel.font = [UIFont fontWithName:@"Chalkduster" size:16];

//set the button's line break mode
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;

//set the button's text color
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//button.titleLabel.textColor = [UIColor greenColor]; // don't use this because button's text color after clicking it will be blue (default).

//add the button to the view
[self.view addSubview:button];

<强>结果:

Screenshot 3

注意:请勿使用textColor,因为点击按钮后的文字颜色将为默认蓝色。