Andrew Ebling
Andrew Ebling

Reputation: 10283

Apply attributes from one NSAttributedString to another

Browsing Attributed String Programming Guide and the documentation for NSAttributedString and NSMutableAttributedString, I can't see a straightforward way to take all the attributes from one string and apply them to another.

The only possibility I've found so far is to to use -[NSAttributedString enumerateAttributesInRange:options:usingBlock:] but that seems like a very long way around.

Am I missing something? Surely there must be a cleaner solution?

What I'm ultimately aiming to achieve is to take an attributed string from a static label I've configured in a Storyboard, change it to contain dynamic info and apply certain attributes over certain ranges.

Upvotes: 1

Views: 408

Answers (1)

Ryan Huang
Ryan Huang

Reputation: 181

Use - (NSDictionary *)attributesAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange to get the attributes at a specific index (e.g. 0 for the first character in string). Then use this attribute in - (void)setAttributes:(NSDictionary *)attributes range:(NSRange)aRange to set this attributes for all characters in range.

Upvotes: 2

Related Questions