Search code examples
iosswiftuilabelnsattributedstringnsmutableattributedstring

adjustsFontSizeToFitWidth does not work properly with NSMutableAttributedString on iOS 14


I need to reduce the text size in order to fit a specific area width. The text contains some different format settings (mainly text color) so I am using NSMutableAttributedString

Then I set adjustsFontSizeToFitWidth for fitting with the bounding rectangle.

testLabel.minimumScaleFactor = 0.1
testLabel.adjustsFontSizeToFitWidth = true
let attributedGreen = NSAttributedString(string: "GREEN", attributes: [NSAttributedString.Key.foregroundColor: UIColor.green])
let attributedYellow = NSAttributedString(string: "YELLOW", attributes: [NSAttributedString.Key.foregroundColor: UIColor.yellow])
let textCombination = NSMutableAttributedString()
textCombination.append(attributedGreen)
textCombination.append(attributedYellow)
textCombination.append(attributedGreen)
textCombination.append(attributedYellow)
testLabel.attributedText = textCombination

enter image description here

The expected result is to have a full visible text and re-scaled to the right font size but it does not happen and the text is truncated.

Everything is working fine if I do not use the attributed string:

testLabel.minimumScaleFactor = 0.1
testLabel.adjustsFontSizeToFitWidth = true     
testLabel.text = "GREENYELLOWGREENYELLOW"

enter image description here

Is that the right way for doing it? How can I properly re-scale the font size?

Extra note: I've just tested it on a device running iOS 13 (instead of iOS 14) and font re-scale with attributed string is working fine.

enter image description here

Could it be an iOS 14 bug? Any workarounds to suggest?

UPDATE 2020/10/26: I've just tested it using iOS 14.0.1 and the issue is still present.


Solution

  • It seems to be a iOS 14 bug openradar.appspot.com/FB8699725 So, for a proper fix, we have to wait the new iOS release, in the meantime I apply the following quick-and-dirty workaround:

    testLabel.minimumScaleFactor = 0.1
    testLabel.adjustsFontSizeToFitWidth = true
    
    let attributedGreen = NSAttributedString(string: "GREEN", attributes: [NSAttributedString.Key.foregroundColor: UIColor.green])
    let attributedYellow = NSAttributedString(string: "YELLOW", attributes: [NSAttributedString.Key.foregroundColor: UIColor.yellow])
    let attributedWorkaroud = NSAttributedString(string: ".", attributes: [NSAttributedString.Key.foregroundColor: UIColor(red: 1, green: 1, blue: 1, alpha: 0)])
    let textCombination = NSMutableAttributedString()
    textCombination.append(attributedGreen)
    textCombination.append(attributedYellow)
    textCombination.append(attributedGreen)
    textCombination.append(attributedYellow)
    textCombination.append(attributedWorkaroud)
    testLabel.attributedText = textCombination
    

    I've added a transparent dot at the end of the string. It produces a little misalignment but at least the word is readable.

    UPDATE 2020/12/17 Fixed in iOS 14.2