自定义导航标题 - SWIFT

时间:2015-05-05 23:49:17

标签: swift nsattributedstring uinavigationitem

我想自定义导航标题但遇到问题。 " String与NSObject"不同。有人能指出我正确的方向吗?我的代码如下,

let font = UIFont(name: "HelveticaNeue", size: 15.0)
let textFont = [NSFontAttributeName: font]
let navText = [NSAttributedString(string: "MY STRING HERE", attributes: textFont)]

var navString = UILabel()
navString.appendAttributedString(navText)
self.navigationItem.titleView = navString

更新:我能够使用以下代码解决问题,

    var navString: NSString = "MY STRING HERE"
    var completedNavString = NSMutableAttributedString()

    completedNavString = NSMutableAttributedString(string: navString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])

    var navLabel = UILabel()
    navLabel.attributedText = completedNavString
    navLabel.sizeToFit()
    self.navigationItem.titleView = navLabel

1 个答案:

答案 0 :(得分:0)

var navString = UILabel()
navString.appendAttributedString(navText)

你不能追加" UILabel的任何东西。试试navString.attributedText = navText