0

我的 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;
}

效果很好,除了我看不到按钮周围的边框:有什么想法吗?我需要做些什么才能使按钮边框显示?

谢谢

4

1 回答 1

1

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

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

然后像这样选择字体(按T字母):

截屏

还要选择字体后面的文本颜色。

结果:

截图 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];

结果:

截图 3

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

于 2012-06-25T21:02:46.813 回答