如何连接NSAttributedStrings

如何连接NSAttributedStrings

本文介绍了如何连接NSAttributedStrings?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在合并字符串之前,我需要搜索一些字符串并设置一些属性,因此让NSStrings->连接它们->使NSAttributedString不是一个选择,有没有办法将attributedString连接到另一个attributedString?

I need to search some strings and set some attributes prior to merging the strings, so having NSStrings -> Concatenate them -> Make NSAttributedString is not an option, is there any way to concatenate attributedString to another attributedString?

推荐答案

我建议您使用@Linuxios建议的单个可变属性字符串,这是另一个示例:

I'd recommend you use a single mutable attributed string a @Linuxios suggested, and here's another example of that:

NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init];

NSString *plainString = // ...
NSDictionary *attributes = // ... a dictionary with your attributes.
NSAttributedString *newAttString = [[NSAttributedString alloc] initWithString:plainString attributes:attributes];

[mutableAttString appendAttributedString:newAttString];

但是,仅出于获得所有选项的目的,您还可以创建单个可变属性字符串,该字符串是由包含已放在一起的输入字符串的格式化NSString组成的.然后,您可以使用addAttributes: range:将事实之后的属性添加到包含输入字符串的范围中.我还是建议采用前一种方式.

However, just for the sake of getting all the options out there, you could also create a single mutable attributed string, made from a formatted NSString containing the input strings already put together. You could then use addAttributes: range: to add the attributes after the fact to the ranges containing the input strings. I recommend the former way though.

这篇关于如何连接NSAttributedStrings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:38