2015-07-04 15 views
5

Ich möchte den Zeichenabstand im Navigationsleistentitel ändern. Ich versuche den folgenden Code zu verwenden.Ändern des Zeichenabstands in der Navigationsleiste titel swift

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



     self.navigationItem.title = attributedString 

Dies wird die folgenden Fehler geben: ‚string‘

kann keinen Wert vom Typ ‚NSMutableAttributedString‘ auf einen Wert von Typ zuweisen

Kann mir jemand dabei helfen oder eine andere Möglichkeit vorschlagen, den Zeichenabstand im Navigationsleistentitel in swift zu ändern?

Antwort

13

Sie können die attributierte Zeichenfolge nicht direkt festlegen.

Sie können durch das Ersetzen 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

Die Antwort von Ashish Kakkad haben einen Lichtausfall für Swift 2. es einen Trick tun, ist ein Fehler der Umwandlung mit NSString.

So ist der richtige Code ist:

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 
Verwandte Themen