【问题标题】:How to enter different color String in UITextView use different languages如何在 UITextView 中输入不同颜色的字符串使用不同的语言
【发布时间】:2017-11-08 12:04:25
【问题描述】:

我想选择颜色然后改变当前输入的UITextView的文字颜色使用不同的语言

但是我遇到了以下问题

1.找不到最后输入的文字

Demo picture

比如本例中,我还没有确认输入的文本,但是已经执行了方法

- (BOOL)textView:(UITextView *) textView shouldChangeTextInRange: (NSRange)range replacementText: (NSString *)text

我猜是先创建一个字符范围,确认输入词的选择,然后替换

但是,我不需要这个输入字符范围,这会影响我需要更改单词颜色的 NSRange 参数

于是我把color函数改成了textViewDidChange方法,却导致我删除崩溃

2.为什么除英文以外的其他语言不执行方法

- (void)insertText:(NSString *)text

这是我的演示链接https://github.com/xueyefengbao/Demo.git

谁能帮我解决问题或修改我在演示中提到的功能?

非常感谢:)

在 trungduc 的建议下,我更改了代码

还是发现了一些小问题

wrong

correct

不能连续进入

Continuous input error

【问题讨论】:

  • 粘贴文本时您的问题是否包含大小写?
  • @trungduc 如果可以,最好
  • 我没有粘贴就修好了。但是你还需要粘贴,它要复杂得多;)

标签: ios objective-c uitextview nsattributedstring


【解决方案1】:

对于您的问题。

  • 找不到最后输入的文本 - 好像您在删除之前忘记重置 lastRange。但是你不再使用它,所以我们可以忽略它。

  • 为什么除英语以外的其他语言不执行方法 这是因为在您的语言中,在某些情况下,当您输入字符时,它并不总是将字符添加到字符串中。实际上,它用另一个字符替换了最后一个字符。它使您从shouldChangeTextInRange 获得的lastRange 超出了textView 上的当前文本范围。我的解决方案是在使用[attributedString addAttribute:NSForegroundColorAttributeName value:self.currentColor range:self.lastRange];之前,您应该检查并更正self.lastRange

您可以尝试用下面的代码替换您的方法。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if (text.length == 0) {
        self.lastRange = NSMakeRange(0, 0); // Reset lastRange when deleting
        return YES;
    }
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }

    BOOL result = [self doesFit:textView string:text range:range];

    if (result) {
        self.lastRange = NSMakeRange(range.location, text.length);
    }
    return result;
}

- (void)resetCorrectFontStyle:(UITextView *)textView  {

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    paragraphStyle.lineSpacing = (textView.textContainer.size.height - (textView.font.lineHeight)*23)/23;
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithAttributedString:textView.attributedText];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, textView.attributedText.length)];

    // Check if lastRange can't be used with current text, correct location of lastRange
    if (_lastRange.length + _lastRange.location > textView.text.length) {
      _lastRange = NSMakeRange(_lastRange.location - 1, _lastRange.length);
    }

    [attributedString addAttribute:NSForegroundColorAttributeName value:self.currentColor range:self.lastRange];

    _keyboardTextView.attributedText = attributedString;
}

希望这会有所帮助。

【讨论】:

  • 谢谢大家的帮助,但是我发现了一些小问题当我切换到日语,然后输入一个m,它会输入字符,并且不能删除,它不是字符而是一个部分字母
  • @zzx 它与我的语言完美配合,但我不知道日语键盘是如何工作的,所以我认为你必须稍作修改才能使用日语。祝你好运;)
  • 我已经更新了我问题底部的截图,你可以看看
  • @zzx 我认为问题是因为您使用的是attributedText。使用text 可以正常工作,但如果使用text,则无法更改文本颜色。这对你来说将是一个大问题;)
  • 这确实是个大问题,不过我觉得只能用AttributeText来完成我的功能另外我发现连续输入也是个问题(并更新上面的截图)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
相关资源
最近更新 更多