Ausgabe
Alles, was ich brauche, ist, alle Attribute von zu durchlaufen NSAttributedString
und ihre Schriftgröße zu erhöhen. Bisher bin ich an dem Punkt angelangt, an dem ich Attribute erfolgreich durchlaufen und manipulieren kann, aber ich kann nicht zurück in NSAttributedString
. Die Zeile, die ich auskommentiert habe, funktioniert bei mir nicht. Wie speichere ich zurück?
NSAttributedString *attrString = self.richTextEditor.attributedText;
[attrString enumerateAttributesInRange: NSMakeRange(0, attrString.string.length)
options:NSAttributedStringEnumerationReverse usingBlock:
^(NSDictionary *attributes, NSRange range, BOOL *stop) {
NSMutableDictionary *mutableAttributes = [NSMutableDictionary dictionaryWithDictionary:attributes];
UIFont *font = [mutableAttributes objectForKey:NSFontAttributeName];
UIFont *newFont = [UIFont fontWithName:font.fontName size:font.pointSize*2];
[mutableAttributes setObject:newFont forKey:NSFontAttributeName];
//Error: [self.richTextEditor.attributedText setAttributes:mutableAttributes range:range];
//no interfacce for setAttributes:range:
}];
Lösung
So etwas sollte funktionieren:
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;
An diesem Punkt res
hat eine neue attributierte Zeichenfolge, bei der alle Schriftarten doppelt so groß sind wie ihre ursprüngliche Größe.
Beantwortet von – rmaddy
Antwort geprüft von – Timothy Miller (FixError Admin)