Looping Through NSAttributedString Attributes to Increase Font SIze Looping Through NSAttributedString Attributes to Increase Font SIze ios ios

Looping Through NSAttributedString Attributes to Increase Font SIze


Something like this should work:

NSMutableAttributedString *res = [self.richTextEditor.attributedText mutableCopy];[res beginEditing];__block BOOL found = NO;[res enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, res.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {    if (value) {        UIFont *oldFont = (UIFont *)value;        UIFont *newFont = [oldFont fontWithSize:oldFont.pointSize * 2];        [res removeAttribute:NSFontAttributeName range:range];        [res addAttribute:NSFontAttributeName value:newFont range:range];        found = YES;    }}];if (!found) {    // No font was found - do something else?}[res endEditing];self.richTextEditor.attributedText = res;

At this point res has a new attributed string with all fonts being twice their original size.


Create an NSMutableAttributedString from your original attributed string before you start. On each iteration of the loop, call addAttribute:value:range: on the mutable attributed string (this will replace the old attributes in that range).


Here is a Swift port of maddy's answer (which works really well for me!). It's wrapped in a little extension.

import UIKitextension NSAttributedString {    func changeFontSize(factor: CGFloat) -> NSAttributedString {        guard let output = self.mutableCopy() as? NSMutableAttributedString else {            return self        }        output.beginEditing()        output.enumerateAttribute(NSAttributedString.Key.font,                                  in: NSRange(location: 0, length: self.length),                                  options: []) { (value, range, stop) -> Void in            guard let oldFont = value as? UIFont else {                return            }            let newFont = oldFont.withSize(oldFont.pointSize * factor)            output.removeAttribute(NSAttributedString.Key.font, range: range)            output.addAttribute(NSAttributedString.Key.font, value: newFont, range: range)        }        output.endEditing()        return output    }}