Ios 逐个字符向NSMutableAttributedString添加属性
我的情况如下: 我有一个Ios 逐个字符向NSMutableAttributedString添加属性,ios,nsattributedstring,nsrange,Ios,Nsattributedstring,Nsrange,我的情况如下: 我有一个nsmutableAttributeString,在文本视图中没有属性。每当用户按下backspace键时,我不希望删除字符,我希望它被删除,就像productivity Suite的“Track Changes”功能一样。我希望用户能够在这之后继续正常键入。我是这样开始的: - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSS
nsmutableAttributeString
,在文本视图中没有属性。每当用户按下backspace键时,我不希望删除字符,我希望它被删除,就像productivity Suite的“Track Changes”功能一样。我希望用户能够在这之后继续正常键入。我是这样开始的:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (text.length == 0 && textView.text.length == 0) return YES;
if (text.length == 0 && !([[textView.text substringFromIndex:textView.text.length - 1] isEqualToString:@" "] || [[textView.text substringFromIndex:textView.text.length - 1] isEqualToString:@"\n"])) {
textView.attributedText = [self strikeText:textView.attributedText];
textView.selectedRange = NSMakeRange(textView.attributedText.length - 1, 0);
return NO;
}
return YES;
}
- (NSAttributedString *)strikeText:(NSAttributedString *)text
{
NSRange range;
NSMutableAttributedString *returnValue = [[NSMutableAttributedString alloc] initWithAttributedString:text];
if (![text attribute:NSStrikethroughStyleAttributeName atIndex:text.length - 1 effectiveRange:&range]) {
NSLog(@"%@", NSStringFromRange(range));
NSLog(@"%@", [text attribute:NSStrikethroughStyleAttributeName atIndex:text.length - 1 effectiveRange:&range]);
[returnValue addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(text.length - 1, 1)];
[returnValue addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(text.length - 1, 1)];
}
else {
[returnValue addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(range.location - 1, 1)];
[returnValue addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(range.location - 1, 1)];
}
[returnValue removeAttribute:NSStrikethroughStyleAttributeName range:NSMakeRange(returnValue.length, 1)];
return returnValue;
}
然而,不管我怎么想,我都无法对这种情况保持清醒的头脑。此代码不起作用,或部分起作用。attribute:atIndex:effectiveRange:
返回的值始终为零,与该属性是否实际存在无关。有效范围超出了我的文本范围
请帮我一把。在您的
删除文本:
方法中,您只检查了AttributeString的末尾。如果要检查最后一个字符,应在text.length-2
中进行检查,前提是文本足够长。另外,你的removeAttribute在最后的方法没有太多意义太我
关于如何重用委托协议中的范围以仅删除所需字符的简单方法:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
// check if your replacement is going to be empty, therefor deleting
if ([text length] == 0) {
// don't strike spaces and newlines, could use NSCharacterSet here
NSString *textToDelete = [textView.text substringWithRange:range];
if (![@[ @" ", @"\n" ] containsObject:textToDelete]) {
textView.attributedText = [self textByStrikingText:textView.attributedText inRange:range];
}
textView.selectedRange = NSMakeRange(range.location, 0);
return NO;
}
return YES;
}
- (NSAttributedString *)textByStrikingText:(NSAttributedString *)text inRange:(NSRange)range
{
NSMutableAttributedString *strickenText = [[NSMutableAttributedString alloc] initWithAttributedString:text];
[strickenText addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
[strickenText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
return strickenText;
}
边缘情况可能会更多,但这是一种简单的方法,可以满足您的需要。您应该尝试:
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello. That is a test"];
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(2,1)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(8,2)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(18,2)];
和属性在此处描述:
不幸的是,您的代码也有问题。我希望光标位于贯穿文本的末尾。如果在某些文本被删除后键入其他内容,则新插入的文本也会被删除。我在第一部分解释了属性:atIndex:effectiveRange:出现问题的原因。给出的代码不应该是完整的解决方案,而应该是您继续使用的方向。