0

Facebookのようなタグ付け機能を行っており、すでにユーザーにタグを付けることができます。このように見せることができます。それはこのコードによって行われます。

NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.textView.text];

for (NSString *word in self.tagNameCollection) {
    [string addColor:[UIColor redColor] substring:word];
    [string addBackgroundColor:[Helpers getFromRGB:135 green:206 blue:250] substring:word];
}

だから、私は NSMutableAttributedString を持っています。NSMutableAttributedString から色やフォントを変更した場所を知ることはできますか? どうすればいいですか?

ここに画像の説明を入力

4

1 に答える 1

2

enumerateAttributesInRange:options:usingBlock:の属性を取得するために使用できますNSAttributedString

例:

[attributedString enumerateAttributesInRange:NSMakeRange(0, [attributedString length])
                                     options:0
                                  usingBlock:^(NSDictionary *attributes, NSRange range, BOOL *stop) {
if ([attributes objectForKey:NSForegroundColorAttributeName])
    NSLog(@"Found ForeGround Color: %@ in range %@", [attributes objectForKey:NSForegroundColorAttributeName], NSStringFromRange(range));
if ([attributes objectForKey:NSFontAttributeName])
    NSLog(@"Found Font: %@ in range %@", [attributes objectForKey:NSFontAttributeName], NSStringFromRange(range));
if ([attributes objectForKey:NSBackgroundColorAttributeName])
    NSLog(@"Found Background Color: %@ in range %@", [attributes objectForKey:NSBackgroundColorAttributeName], NSStringFromRange(range));

}];
于 2015-03-02T09:02:22.883 に答える