我的 UIToolbar
中有一个 UIBarButtonItem
,标题为 Done。现在我想将字体从默认更改为 "Trebuchet MS" 加粗。我该怎么做?
最佳答案
准确的说,可以按照下面的方式来完成
[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName,
[UIColor greenColor], NSForegroundColorAttributeName,
nil]
forState:UIControlStateNormal];
或者使用对象字面量语法:
[buttonItem setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:26.0],
NSForegroundColorAttributeName: [UIColor greenColor]
} forState:UIControlStateNormal];
为方便起见,这里是 Swift 实现:
buttonItem.setTitleTextAttributes([
NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedStringKey.foregroundColor: UIColor.green],
for: .normal)
关于ios - 更改 UIBarButtonItem 的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8849913/