在同一NSAttributedString上应用NSFontAttributeName和NSUnderlineStyleAttributeName

时间:2016-07-24 13:44:09

标签: swift xcode uilabel nsattributedstring uifont

我想将NSFontAttributeNameNSUnderlineStyleAttributeName应用于NSMutableAttributedString。下面是我的代码,但它不起作用。它只应用font属性。如果我删除了font属性,它会应用下划线属性。我不知道如何申请两者。

        let font = UIFont(name: "MyriadPro-Semibold", size: 30)
        let underlineAttributedString = NSMutableAttributedString(string: "Menu")
        let range = NSMakeRange(0, underlineAttributedString.length)

        underlineAttributedString.beginEditing()

        underlineAttributedString.addAttribute(NSFontAttributeName, value: font!, range: range)
        underlineAttributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: range)

        underlineAttributedString.endEditing()

        menuLabel_.attributedText = underlineAttributedString
        menuLabel_.textAlignment = .Center
        menuLabel_.textColor = UIColor.darkGrayColor()

0 个答案:

没有答案