在iOS中更改句子的部分文本的颜色 [英] Change color a part text of sentence in iOS

查看:38
本文介绍了在iOS中更改句子的部分文本的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
TTT归因于标签多色字体帮助

我如何在iPhone中喜欢这张照片?有人帮我.我应该使用Core Animation还是Text Core或其他? http://s7.postimage.org/qgbhw7c3v/Capture.png

How can I do like this picture in iphone? Anyone help me. Should I use Core Animation or Text Core or something else? http://s7.postimage.org/qgbhw7c3v/Capture.png

推荐答案

    NSString *test = @"How can i Do that? (just only one string and don't devided into 2 part)";

    CFStringRef string =  (CFStringRef) test;
    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CFAttributedStringReplaceString (attrString,CFRangeMake(0, 0), string);



    /*
     Note: we could have created CFAttributedStringRef which is non mutable, then we would have to give all its
     attributes right when we create it. We can change them if we use mutable form of CFAttributeString.
     */




    CGColorRef _red=[UIColor redColor].CGColor;

    CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 11),kCTForegroundColorAttributeName, _red);    

注意 =>在项目中添加CoreText框架并导入.

Note => Add CoreText framework in the project and import it.

这篇关于在iOS中更改句子的部分文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆