归因字符串失去函数内的属性

时间:2017-06-21 14:55:28

标签: ios swift nsattributedstring nsmutableattributedstring

我有一个我创建的属性字符串:

let myMutableString = NSMutableAttributedString(
            string:  "Father order number:",
            attributes: [NSFontAttributeName:UIFont(
                name: "Helvetica Neue",
                size: 18.0)!])

        myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGray, range: NSRange(location: 0, length: myMutableString.length-1))

        label1.attributedText = myMutableString

这很好用,并且属性显示在模拟器上。但是,当我尝试将属性字符串放入这样的函数时,在进行函数调用时,属性不会显示。

函数调用:

let myMutableString:NSMutableAttributedString! = getDarkGrayHelveticaAttributedString(str: "Father order number:")

功能使用:

label1.attributedText = myMutableString

功能:

func getDarkGrayHelveticaAttributedString(str: String) -> NSMutableAttributedString {
    var myMutableString = NSMutableAttributedString(
        string:  str,
        attributes: [NSFontAttributeName:UIFont(
            name: "Helvetica Neue",
            size: 18.0)!])

    return myMutableString
}

0 个答案:

没有答案