NSFontAttributedString worked before XCode 6.1

后端 未结 2 1284
后悔当初
后悔当初 2020-11-29 13:41
let timeFont = [NSFontAttributeName:UIFont(name: \"Voyage\", size: 20.0)]
var attrString3 = NSAttributedString(\"(Time)\", attributes : timeFont); // <--- compile         


        
2条回答
  •  Happy的楠姐
    2020-11-29 14:37

    Xcode 6.1 comes with Swift 1.1 that supports constructors that can fail. UIFont initialisation can fail and return nil. Also use string: when creating NSAttributedString:

    if let font = UIFont(name: "Voyage", size: 20.0) {
        let timeFont = [NSFontAttributeName:font]
        var attrString3 = NSAttributedString(string: "(Time)", attributes : timeFont)
    }
    

提交回复
热议问题