How to change font size of attributed string without changing other attributes?

I have a UITextView whcih holds an attributed string. How do I change just the font size of all the text without changing other attributes like Bold and Italic? Once I change the font size, it also removes font bold and italic. I am using attributedString.addAttributes(attributeName, Range)


Thanks


Raghavendra Rao

Answered by jvarela in 328478022

I always reset all the attributes that deviate from the "normal" attributes every time I set any attributes. Thus, if your text is in bold and italic, I would recommend that you set these attributes too. This is how I do and always works as intended.

Accepted Answer

I always reset all the attributes that deviate from the "normal" attributes every time I set any attributes. Thus, if your text is in bold and italic, I would recommend that you set these attributes too. This is how I do and always works as intended.

Just set the right key; if you use system font:


    var fieldFont = UIFont()
        if bold {
            fieldFont = UIFont.boldSystemFont(ofSize: 0)  // keep standard size, but could define your own
        } else {
            fieldFont = UIFont.systemFont(ofSize: 0)
        }

    attributes[NSAttributedString.Key.font] = fieldFont

Thanks

Can you tell me why this piece of code is not setting bold and italic attributes?


let italicAttribute = [UIFontDescriptor.AttributeName.symbolic:UIFontDescriptorSymbolicTraits.traitItalic, UIFontDescriptor.AttributeName.name: "Chalkboard SE"] as [UIFontDescriptor.AttributeName : Any]

let boldAttribute = [UIFontDescriptor.AttributeName.symbolic:UIFontDescriptorSymbolicTraits.traitBold, UIFontDescriptor.AttributeName.name: "Chalkboard SE"] as [UIFontDescriptor.AttributeName : Any]

let italicDescriptor = UIFontDescriptor().addingAttributes(italicAttribute)

let boldDescriptor = UIFontDescriptor().addingAttributes(boldAttribute)

let comparatorItalicFont = UIFont(descriptor: italicDescriptor, size: fontSize)

let comparatorBoldFont = UIFont(descriptor: boldDescriptor, size: fontSize)

Because the setting of fonts and traits is not trivial. Even though you may ask for a given trait for a specific font, the latter may not support what you asked for. For instance, if you want to use the bold trait with Chalboard SE, you might need to specifically state that you want to use the font that supports that trait. For an example, refer to this post:


https://stackoverflow.com/questions/37242281/unable-to-get-the-uifont-for-chalkboard-se-bold


Moreover, if you read the text for API you are using, it reads:


func addingAttributes([UIFontDescriptor.AttributeName : Any] = [:]) -> UIFontDescriptor

Returns a new font descriptor that is the same as the receiver but with the specified attributes taking precedence over the existing ones.


Thus what you need is:


1. To ensure that you are using the correct font name that does support bold and italic

2. That you create a Dictionary that contains both attributes; what you are doing in your code you are replacing the italic attribute with the bold attribute.

How to change font size of attributed string without changing other attributes?
 
 
Q