[英]Justify text in iOS (Swift or Objective-C) without breaking tracking/kerning/letter spacing
[英]How to set kerning (spacing between characters) on UINavigationBar title - Swift or Objective-C
我的導航欄大部分是根據自己的喜好定制的,但我正在嘗試使用NSKernAttributeName
增加字距。 我正在使用外觀代理將導航欄設置為白色文本和自定義字體,但是當我嘗試添加字距時,它不會生效。
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], NSFontAttributeName,
[NSNumber numberWithFloat:2.0], NSKernAttributeName, nil]];
我是否需要執行其他操作才能在標題標簽中添加一些不太常見的屬性,如字距調整?
根據文檔, UINavigationBar
的titleTextAttributes
僅允許您指定字體,文本顏色,文本陰影顏色和文本陰影偏移。
如果要使用其他屬性,可以使用所需的NSAttributedString
創建UILabel
,並將其設置為控制器的navigationItem
的titleView
。
例如:
UILabel *titleLabel = [UILabel new];
NSDictionary *attributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0],
NSKernAttributeName: @2};
titleLabel.attributedText = [[NSAttributedString alloc] initWithString:self.navigationItem.title attributes:attributes];
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;
我已經嘗試了許多不同的方法來實現這一點,並發現你只能更改UINavigationBar的字體,文本顏色,文本陰影顏色和文本陰影偏移量,如上面的@JesúsA。Alvarez所說。
我已經在Swift中轉換了代碼並且它可以工作:
let titleLabel = UILabel()
let attributes: NSDictionary = [
NSFontAttributeName:UIFont(name: "HelveticaNeue-Light", size: 20),
NSForegroundColorAttributeName:UIColor.whiteColor(),
NSKernAttributeName:CGFloat(2.0)
]
let attributedTitle = NSAttributedString(string: "UINavigationBar Title", attributes: attributes as? [String : AnyObject])
titleLabel.attributedText = attributedTitle
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel
上面的答案更新為Swift 4
我創建了一個超類,我定義了這個方法,你可以從你想要的每個子類調用它:
func setNavigationTitle(_ title: String, kern: CGFloat) {
let titleLabel = UILabel()
let attributes = [
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 18),
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.kern: kern] as [NSAttributedStringKey : Any]
let attributedTitle = NSAttributedString(string: title, attributes: attributes)
titleLabel.attributedText = attributedTitle
titleLabel.sizeToFit()
self.navigationItem.titleView = titleLabel
}
我將上面的答案轉換為UIViewController擴展,以便將其整理掉。
斯威夫特3
extension UIViewController {
func setUpCustomTitleView(kerning: CGFloat) {
let titleLabel = UILabel()
guard let customFont = UIFont(name: "Montserrat-SemiBold", size: 18) else { return }
let attributes = [NSForegroundColorAttributeName: UIColor.gray,
NSFontAttributeName: customFont,
NSKernAttributeName: kerning] as [String : Any]
guard let title = title else { return }
let attributedTitle = NSAttributedString(string: title, attributes: attributes)
titleLabel.attributedText = attributedTitle
titleLabel.sizeToFit()
navigationItem.titleView = titleLabel
}
}
從視圖控制器的viewDidLoad()調用擴展函數。
setUpCustomTitleView(kerning: 2.0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.