NSMutableAttributedString

时间:2016-04-22 21:12:03

标签: ios swift core-data nsattributedstring nsmutableattributedstring

我有一些属性:

var myAttributeMainText = [ NSFontAttributeName: UIFont(name: "Arial", size: 24.0)! ]
var myAttributeUpperText = [ NSBaselineOffsetAttributeName: 8, NSFontAttributeName: UIFont(name: "Arial", size: 14.0)! ]

我在文本标签中使用这些属性

var allMurableAttributedString = NSMutableAttributedString(string: "")
var userAttribute = NSMutableAttributedString(string: "USER", attributes: myAttributeMainText)
var newAttribute = NSMutableAttributedString(string: "NEW", attributes: myAttributeUpperText)
allMurableAttributedString.appendAttributedString(userAttribute)
allMurableAttributedString.appendAttributedString(newAttribute)

result

然后我在CoreData allMurableAttributedString中添加这些属性并具有以下内容:

USER{
    NSFont = "<UICTFont: 0x79feec20> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 0.00pt";
}

NEW{
    NSBaselineOffset = 6;
    NSFont = "<UICTFont: 0x79fed8e0> font-family: \"Thonburi\"; font-weight: normal; font-style: normal; font-size: 10.00pt";
}

当我调用它时,如何更改属性allMurableAttributedString的字体大小?

1 个答案:

答案 0 :(得分:0)

从NSMutableAttributedString中删除字体设置我使用了此代码

allMurableAttributedString.removeAttribute(NSFontAttributeName, range: NSMakeRange(0, 7))

然后我可以添加新的属性

allMurableAttributedString.setAttributes(myAttributeMainText, range: NSMakeRange(0, 4))
allMurableAttributedString.setAttributes(myAttributeUpperText, range: NSMakeRange(4, 3))