【问题标题】:NSMutableAttributedString to know where color is changed on which textNSMutableAttributedString 知道在哪个文本上更改了颜色
【发布时间】:2015-03-02 08:48:40
【问题描述】:

我正在做像 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 中的颜色和字体吗?我可以知道该怎么做吗?

【问题讨论】:

  • enumerateAttributesInRange:options:usingBlock:

标签: ios nsmutableattributedstring


【解决方案1】:

您可以使用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));

}];

【讨论】:

  • 谢谢。好像没问题。我是否总是需要保留 NSMutableAttributedString 并在我的 uitextview 中发生更改时进行更新?我可以从我的 uitextview 中获取 NSMutableAttributedString 吗?如果我这样做,NSMutableAttributedString 的属性就消失了。 NSMutableAttributedString *muStr = [[NSMutableAttributedString alloc]initWithString:self.inputToolBarViewInDetail.textView.text];
  • 如果您想知道更改,您可能必须保留上一个的引用,或者在修改文本时检查UITextViewDelegate方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-06
  • 2013-10-17
相关资源
最近更新 更多