您当前的位置: 首页 >  ui

培根芝士

暂无认证

  • 0浏览

    0关注

    446博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

UITextView实现输入标签高亮

培根芝士 发布时间:2019-12-26 16:40:56 ,浏览量:0

设定标签以#开始和空格结尾,在输入框中实现标签以高亮颜色显示。如果使用textView.attributedText来设定高亮颜色的话,会导致中文输入异常。

使用textView.textStorage可以实现正常显示和输入。

- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length>0) {
        [textView.textStorage addAttributes:@{NSForegroundColorAttributeName:kTextColor} range:NSMakeRange(0, textView.text.length)];
        NSString *content = textView.text;
        NSInteger iLocation = 0;
        while (true) {
            NSRange r = [content rangeOfString:@"(#.*? )" options:NSRegularExpressionSearch range:NSMakeRange(iLocation, content.length-iLocation)];
            if (r.location == NSNotFound) {
                break;
            }
            [textView.textStorage addAttributes:@{NSForegroundColorAttributeName:kMarkColor} range:r];
            iLocation = r.location+r.length;
        }
    }
}

 

关注
打赏
1660824269
查看更多评论
立即登录/注册

微信扫码登录

0.0386s