简体   繁体   English

逐个字符地向 NSMutableAttributedString 添加属性

[英]Add attributes to NSMutableAttributedString character by character

Here's my situation:这是我的情况:

I have an NSMutableAttributedString with no attributes in a text view.我有一个NSMutableAttributedString在文本视图中没有属性。 Whenever the user presses the backspace key, I do not want a character to be deleted, I want it to be struck through, just like the "Track Changes" feature of productivity suites.每当用户按下退格键时,我不希望删除一个字符,我希望它被删除,就像生产力套件的“跟踪更改”功能一样。 I want the user to be able to continue typing normally after that.我希望用户能够在此之后继续正常打字。 Here's how I started out:这是我开始的方式:

- (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;


}

However, no matter how hard I think, I can't wrap my head around the situation.然而,无论我怎么想,我都无法理解这种情况。 This code doesn't work, or works partially.此代码不起作用,或部分起作用。 The value returned by attribute: atIndex: effectiveRange: is always nil, doesn't matter if the attribute actually exists or not. attribute: atIndex: effectiveRange:返回的值attribute: atIndex: effectiveRange:始终为零,与该属性是否实际存在无关。 The effective range is out of bounds of the text I have.有效范围超出了我所拥有的文本范围。

Please help me out here.请帮帮我。

In your strikeText: method you're only checking the very end of your attributedString.在您的strikeText:方法中,您只检查attributedString 的末尾。 If you want to check the last character you should check from text.length -2 , assuming that text is long enough.如果你想检查最后一个字符,你应该检查text.length -2 ,假设文本足够长。 Also you're removeAttribute in the end of the method does not make much sense too me.此外,您在方法末尾的 removeAttribute 对我来说也没有多大意义。

A simple approach on how you can reuse the range from the Delegate-Protocol to strike only the characters you need:关于如何重用 Delegate-Protocol 范围的简单方法来仅攻击您需要的字符:

- (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;
}

There might be more edge cases but this is a simple approach that does what you want.可能有更多的边缘情况,但这是一种简单的方法,可以满足您的需求。

You should try:你应该试试:

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)];

And attributes are described here: https://developer.apple.com/documentation/foundation/nsattributedstringkey?language=objc属性在这里描述: https : //developer.apple.com/documentation/foundation/nsattributedstringkey?language=objc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM