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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top