Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping Through NSAttributedString Attributes to Increase Font SIze

All I need is to loop through all attributes of NSAttributedString and increase their font size. So far I got to the point where I successfully loop through and manipulate attributes but I cannot save back to NSAttributedString. The line I commented out is not working for me. How to save back?

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:   }]; 
like image 904
Vad Avatar asked Oct 15 '13 16:10

Vad


People also ask

What is the default font for NSAttributedString objects?

The default font for NSAttributedString objects is Helvetica 12-point, which may differ from the default system font for the platform.

What is nsmutableattributedstring in Java?

The NSAttributedString type represents a string that has a series of attributes applied uniformly. The companion NSMutableAttributedString type can be used to create attributed strings that have overlapping attributes and whose contents can be modified after creation.

How do I use nsattributedstrings with UIKit?

To create NSAttributedStrings that you can use with UIKit's rendering, you create an instance of the UIStringAttributesclass, set its properties to the attributes that you desire, and then invoke the NSAttributedString constructor with it.

What is the companion nsmutableattributedstringtype for?

The companion NSMutableAttributedStringtype can be used to create attributed strings that have overlapping attributes and whose contents can be modified after creation.


1 Answers

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.

like image 187
rmaddy Avatar answered Sep 29 '22 01:09

rmaddy