[英]How to change font size of only one BarButtonItem?
我在另一个线程中找到了此代码,但是我只想更改一个按钮的字体大小。
if let font = UIFont(name: "AvenirNext", size: 15) {
oneBarButtonItem.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)}
那么我必须在哪里放置代码,这正确吗?
您需要将此代码放入ViewController
viewDidLoad
方法中。
override func viewDidLoad() {
super.viewDidLoad()
let font = UIFont.systemFontOfSize(20);
barButtonItem.setTitleTextAttributes([NSFontAttributeName: font], forState:UIControlState.Normal)
}
您的代码可能无法正常工作,因为您没有名为AvenirNext
字体。
更新
Swift 4 / iOS 11的工作代码:
override func viewDidLoad() {
super.viewDidLoad()
let font = UIFont.systemFont(ofSize: 20)
barButtonItem.setTitleTextAttributes([NSAttributedStringKey.font.rawValue: font], forState: .normal)
}
对于目标C,
[barbutton setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:13.0],
NSForegroundColorAttributeName: [UIColor whiteColor]
} forState:UIControlStateNormal];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.