Cocoa uibutton字体子类

Cocoa uibutton字体子类,cocoa,uibutton,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 = UI

我的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;
}
效果很好,只是我看不到按钮周围的边框:知道为什么吗?我还需要做些什么来显示按钮边框


谢谢

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

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

然后选择font(按T字母),如下所示:

还可以选择字体后面的文本颜色

结果:

//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];

编辑:

//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];
以编程方式创建按钮:

//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];
结果:

//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];


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

嘿,贾斯汀,你的教程很棒!只是我使用了这么多这种风格的按钮,这样可以更快地更改类,而不是每次都不断更改属性。你知道我需要完成的最后一步吗?@RobW是的,更改类会更快,但复制创建按钮也会足够快。所以你可以这么做。关于编程:据我所知,按钮的类型在创建时无法更改(您需要更改为
uibuttontyperoundredrect
),因此,如果您想通过编程实现这一点,请在代码中以编程方式创建所有按钮,而不使用IB。@RobW查看更新的答案。我添加了如何以编程方式创建按钮的代码,并对其进行了注释。