XZ_iOS之UITextView或UITextField实时修改输入的部分文字的颜色(上)

(都写在一篇博客上面,发现下部分直接没有了,只能写个下篇了)

优化:虽然输入之后的文字的颜色改变了,但是在输入显示的过程中,文字的颜色还是跟前一个文字的文字属性一样的,查找文档发现:textView有一个markedTextStyle属性,这个属性的值是一个字典,字典的key值是下图中所示:

使用如下代码,然而并没有任何作用:
 // 修改高亮时字体颜色
    [textView.markedTextStyle setValue:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
    [textView.markedTextStyle setValue:[UIColor blackColor] forKey:NSBackgroundColorAttributeName];
    [textView.markedTextStyle setValue:[UIFont systemFontOfSize:20.0] forKey:NSFontAttributeName];
那换一种写法试试,使用如下代码,只能修改背景色,字体颜色不起作用。不要问我为什么,因为我也不知道。
NSDictionary *dict = @{
                           NSForegroundColorAttributeName:[UIColor blackColor],
                           NSBackgroundColorAttributeName:[UIColor blackColor],
                           NSFontAttributeName:[UIFont systemFontOfSize:20.0]
                           };
    [textView setMarkedTextStyle:dict];
那我还能怎么办,查找了Stack Overflow,看到也有人遇到了相同的问题,然而并没有找到答案。。。。有哪位好心人知道了可以告诉我一下,哈哈

又有好心人告诉我了,说关键词[]中间不能插入文字,这有什么难的,我们可以使用核心绘图,将关键词转换成图片,再赋值给textView不就行了嘛,贴代码:
+ (UIImage *)makeTextToImage:(NSString *)text font:(CGFloat)fontSize {
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    
    CGFloat height = font.lineHeight;
    
    NSDictionary *attrDict = @{
                               NSFontAttributeName:font,
                               NSForegroundColorAttributeName:kXZMainBgColor,
                               NSBackgroundColorAttributeName:[UIColor greenColor]
                               };
    CGSize stringSize = [text sizeWithAttributes:attrDict];
    
    UIGraphicsBeginImageContextWithOptions(stringSize, NO, 0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetCharacterSpacing(ctx, 10);
    CGContextSetTextDrawingMode (ctx, kCGTextFillStroke);
    CGContextSetRGBFillColor (ctx, 0.1, 0.2, 0.3, 1); // 6
    CGContextSetRGBStrokeColor (ctx, 0, 0, 0, 1);
    
    CGRect rect = CGRectMake(0, 0, stringSize.width , height);
    [text drawInRect:rect withAttributes:attrDict];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
NSMutableAttributedString有一个使用NSTextAttachment创建的方法,而这个attachment里面又有一个image属性,所以,完美的解决了我们的问题:
+ (void)xz_makeWordsAnotherColor:(NSString *)attributeText color:(UIColor *)color view:(UITextView *)textView {
    // 获取当前 textView 属性文本 => 可变的
    NSMutableAttributedString *attrStrM = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
    
    // 创建属性文本
    XZTextAttachment *attachment = [[XZTextAttachment alloc] init];
    attachment.image = [self makeTextToImage:attributeText font: 13.0];
    attachment.chs = attributeText;
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithAttributedString: [NSAttributedString attributedStringWithAttachment:attachment]];
// 记录光标位置
    NSRange range = textView.selectedRange;
    // 将属性文本插入到当前的光标位置
    [attrStrM replaceCharactersInRange:range withAttributedString:attrStr];
    // 设置文本
    textView.attributedText = attrStrM;
    // 恢复光标位置
    NSRange rangeNow = NSMakeRange(range.location + 1, 0);
    
    textView.selectedRange = rangeNow;
}

实现效果如下:


 

XZ_iOS之UITextView或UITextField实时修改输入的部分文字的颜色(上)


附代码地址,欢迎Star:https://github.com/CoderXAndZ/XZTextView2


猜你喜欢

转载自blog.csdn.net/understand_xz/article/details/79625296