NSAttributedStrings を連結するにはどうすればいいですか? 質問する

NSAttributedStrings を連結するにはどうすればいいですか? 質問する

文字列をマージする前に、いくつかの文字列を検索し、いくつかの属性を設定する必要があるため、NSStrings -> それらを連結 -> NSAttributedString を作成するというオプションはありません。attributedString を別の attributedString に連結する方法はありますか?

ベストアンサー1

@Linuxios が提案した単一の変更可能な属性付き文字列を使用することをお勧めします。次にその別の例を示します。

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:を含む範囲に事後的に属性を追加できます。ただし、前者の方法をお勧めします。

おすすめ記事