2015-07-04 31 views
5

Gezinme çubuğu başlığında karakter aralığını değiştirmek istiyorum. Aşağıdaki kodu kullanmaya çalışıyorum. Gezinme çubuğundaki karakter aralığını değiştirme title swift

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



     self.navigationItem.title = attributedString 

Bu

aşağıdaki hatayı veriyor: 'string'

tip bir değere türü 'NSMutableAttributedString' değerini atanamaz

Birisi bana bu konuda yardımcı olabilir veya gezinti çubuğu başlığında karakter aralığını değiştirmek için farklı bir yol önerebilir mi?

cevap

13

Öznitelikli dize doğrudan ayarlanamaz.

Sen Ashish Kakkad cevabı Swift 2 için hafif bir başarısızlık var 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

değiştirerek bir hile yapabilirsiniz NSString ile dönüşüm bir hata var.

Yani doğru kod şudur:

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 
İlgili konular