2015-07-04 21 views
5

Tôi muốn thay đổi khoảng cách ký tự trong tiêu đề thanh điều hướng. Tôi đang cố gắng sử dụng đoạn mã sau.Thay đổi khoảng cách ký tự trong tiêu đề thanh điều hướng nhanh chóng

let attributedString = NSMutableAttributedString(string: "New Title") 
     attributedString.addAttribute(NSKernAttributeName, value: CGFloat(1.4), range: NSRange(location: 0, length: 9)) 



     self.navigationItem.title = attributedString 

này được đưa ra các lỗi sau: '? Chuỗi'

Không thể gán một giá trị của loại 'NSMutableAttributedString' đến một giá trị kiểu

Ai đó có thể giúp tôi với điều này hoặc đề xuất một cách khác để thay đổi khoảng cách ký tự trong tiêu đề thanh điều hướng nhanh không?

Trả lời

13

Bạn không thể đặt chuỗi được phân bổ trực tiếp.

Bạn có thể làm một thủ thuật bằng cách thay thế titleView

let titleLabel = UILabel() 
let colour = UIColor.redColor() 
let attributes: [NSString : AnyObject] = [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: colour, NSKernAttributeName : 5.0] 
titleLabel.attributedText = NSAttributedString(string: "My String", attributes: attributes) 
titleLabel.sizeToFit() 
self.navigationItem.titleView = titleLabel 

enter image description here

0

Câu trả lời của Ashish Kakkad có một thất bại ánh sáng cho Swift 2. Có một lỗi chuyển đổi với NSString.

Vì vậy, mã chính xác là:

let titleLabel = UILabel() 
let colour = UIColor.redColor() 
let attributes: [String : AnyObject] = [NSFontAttributeName:UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: colour, NSKernAttributeName : 5.0] 
titleLabel.attributedText = NSAttributedString(string: "My String", attributes: attributes) 
titleLabel.sizeToFit() 
self.navigationItem.titleView = titleLabel 
Các vấn đề liên quan