Search code examples
iosswiftcore-datansattributedstringnsmutableattributedstring

Swift replacement font size in NSMutableAttributedString


I have some attributes:

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

I'm using those attributes in a text label

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

Then I'm adding those attributes in CoreData allMurableAttributedString and have something like this:

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";
}

How can i change the font size of my attribute allMurableAttributedString when I call it?


Solution

  • to remove Font settings from NSMutableAttributedString I'm used this code

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

    then i can add new Attributes

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