小编Sor*_*lou的帖子

更改UITextView上的属性而不重置attributionText属性

我有一个UITextView被解析并在键入某些字符时更改其属性.文本不会更改,只会更改描述文本格式的属性.

如果我解析每个字符条目,我基本上抓住它text,创建一个具有正确格式的属性字符串,并将attributedTexttextview 的属性设置为我的新属性字符串.这完全打破了自动更正,双倍空格快捷方式和拼写检查.

如果我只在键入某些特殊字符时进行解析,这会更好一点,但是我会得到奇怪的错误,比如每个句子的第二个单词都是大写的.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (text.length == 0) {
        return YES;
    }
    unichar firstCharacterInText = [text characterAtIndex:0];
    if (/* special character */) {
        [self processTextView];
    }
}

- (void) processTextView {
    NSString *text = self.text;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];

    [attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:kFontRegular size:12.0f] range:NSMakeRange(0, text.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor textColor] range:NSMakeRange(0, text.length)];
    // set other properties 
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:有没有办法改变文本视图的文本属性而不重置textview的attributedText属性并打破所有这些方便的功能UITextView

uikit core-text ios

5
推荐指数
2
解决办法
3333
查看次数

标签 统计

core-text ×1

ios ×1

uikit ×1