我需要通过编程方式设置UIButton标题UILabel的字体大小。
当前回答
这会很有帮助
button.titleLabel.font = [UIFont fontWithName:@"YOUR FONTNAME" size:12.0f]
其他回答
button.titleLabel.font = [UIFont systemFontOfSize:size];
应该帮助
通过这种方式,您可以设置fontSize并在一个类中处理它。
1. 创建了UIButton的扩展,并添加了以下代码:
- (void)awakeFromNib{
[super awakeFromNib];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont fontWithName:@"font"
size:self.titleLabel.font.pointSize]];
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
}
2.1在代码中创建UIButton
现在如果你在你的代码中创建了一个UIButton, #import extension of yourUIButton '并创建按钮。
2.2在界面生成器中创建按钮
如果你在界面构建器中创建了UIButton,选择UIButton,转到标识检查器,并为UIButton添加创建的扩展类。
objective - c:
[button.titleLabel setFont: [button.titleLabel.font fontWithSize: sizeYouWant]];
迅速:
button.titleLabel?.font = button.titleLabel?.font.fontWithSize(sizeYouWant)
只会改变字体大小。
检查自定义字体名称是否添加了“目标成员资格”复选框。这应该会有所帮助。
button.titleLabel.font = <whatever font you want>
对于那些想知道为什么他们的短信不显示的人,如果你这样做了
button.titleLabel.text = @"something";
它不会显示,你需要做:
[button setTitle:@"My title" forState:UIControlStateNormal]; //or whatever you want the control state to be
推荐文章
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- Swift:声明一个空字典
- 为什么ARC仍然需要@autoreleasepool ?
- 在成功提交我的应用程序后,“太多符号文件”
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 如何从UIImage (Cocoa Touch)或CGImage (Core Graphics)获取像素数据?
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- Xcode 4挂在“附加到(应用程序名称)”
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式