5

I have a NSAttributedString that was loaded from a RTF file, so it already holds several font-attributes for different ranges. Now I want to adapt the font size to the screensize of the device, but when I add a whole new font attribute with a new size, the other fonts disappear.

Is there a way to change only the font size for the whole string?

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Patrick
  • 552
  • 5
  • 17
  • Iterate the fonts attribute like (https://stackoverflow.com/questions/56021846/toggle-selectedrange-attributes-in-uitextview/56056187#56056187 or https://stackoverflow.com/questions/43723345/nsattributedstring-change-the-font-overall-but-keep-all-other-attributes), when found, recreate the same font with different pointsize and apply it to the range. – Larme May 12 '19 at 19:40

1 Answers1

10

If you only want to change the size of any given font found in the attributed string then you can do:

let newStr = someAttributedString.mutableCopy() as! NSMutableAttributedString
newStr.beginEditing()
newStr.enumerateAttribute(.font, in: NSRange(location: 0, length: newStr.string.utf16.count)) { (value, range, stop) in
    if let oldFont = value as? UIFont {
        let newFont = oldFont.withSize(20) // whatever size you need
        newStr.addAttribute(.font, value: newFont, range: range)
    }
}
newStr.endEditing()

print(newStr)

This will keep all other attributes in place.

If you want to replace all fonts in a given attributed string with a single font of a given size but keep all other attributes such as bold and italic, see: NSAttributedString, change the font overall BUT keep all other attributes?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Quick question: is the `remoteAttribute` call really necessary? Always thought that `addAttribute` would be enough here. – Paulo Mattos May 12 '19 at 23:02
  • 1
    @PauloMattos Good question. I suppose it isn't now that you point it out. Since the call to `addAttribute` is for the same attribute in the same range, the call to `removeAttribute` is unnecessary. – rmaddy May 12 '19 at 23:05
  • May I know why is it `newStr.string.utf16.count` instead of just `newStr.string.count`? And is it a good idea to always use the former when getting string count? – CyberMew Mar 25 '22 at 10:37