【问题标题】:Objective C - NSAttributedString extend Attributes?Objective C - NSAttributedString 扩展属性?
【发布时间】:2011-07-21 22:49:04
【问题描述】:

当用户输入我的文本文件时,我希望新字符继承之前的属性。

我目前从前一个字符中获取属性并将其添加为新范围的新属性?我怎样才能让它扩展之前的属性字符串呢?

// What I have
"a"{attribute1 (0,1)} "b"{attribute2 (1,1)} "c"{attribute3(2,1)}
// What I'm trying to do
"abc" {attribute1 (0,3)}

注意:这必须发生在用户输入的每个字符上,因此当用户输入时,新字符应该继承之前的属性。

编辑: 更多信息

// So I create a new AttributedString, added it to my mutable string
NSAttributedString *newString = [[NSAttributedString alloc] initWithString:USER_INPUT attributes:self.defaultAttributes];
[_mutableAttributedString insertAttributedString:newString atIndex:selectedNSRange.location];

// Is it possible to add the USER_INPUT to the end of my mutable attributedString and extends the range of the previous attribute?

【问题讨论】:

    标签: iphone objective-c nsattributedstring


    【解决方案1】:

    您可以使用replaceCharactersInRange:withString: 方法来执行此操作。如果指定范围的长度为 0,则不会替换任何字符。字符串会自动赋予它之前字符的属性,除非它在开头,在这种情况下,它会被赋予它之后字符的属性。

    NSRange range = NSRangeMake([_mutableAttributedString length],0);
    [_mutableAttributedString replaceCharactersInRange:range withString:USER_INPUT];
    

    【讨论】:

      猜你喜欢
      • 2011-10-10
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多