UILable:显示多种颜色的方法

来源:互联网 发布:回家吃饭软件怎么样 编辑:程序博客网 时间:2024/05/16 12:04

借用别人封装好的类库,用来显示同一个UILable上的多种颜色的字。

类库可以直接在次博客中下载,下载后别忘留言哦。

类库使用:主要用了CoreText里面的东西,所以在使用类库之前需要引用CoreText这个系统框架。

注意,此封装的类库不支持ARC

类库设置好后,开始进行使用此类库里面的方法:

// Do any additional setup after loading the view, typically from a nib.

    /*

     注,此处的color对应的值必须是系统预定义好的,有

     + (UIColor *)blackColor;      // 0.0 white

     + (UIColor *)darkGrayColor;   // 0.333 white

     + (UIColor *)lightGrayColor;  // 0.667 white

     + (UIColor *)whiteColor;      // 1.0 white

     + (UIColor *)grayColor;       // 0.5 white

     + (UIColor *)redColor;        // 1.0, 0.0, 0.0 RGB

     + (UIColor *)greenColor;      // 0.0, 1.0, 0.0 RGB

     + (UIColor *)blueColor;       // 0.0, 0.0, 1.0 RGB

     + (UIColor *)cyanColor;       // 0.0, 1.0, 1.0 RGB

     + (UIColor *)yellowColor;     // 1.0, 1.0, 0.0 RGB

     + (UIColor *)magentaColor;    // 1.0, 0.0, 1.0 RGB

     + (UIColor *)orangeColor;     // 1.0, 0.5, 0.0 RGB

     + (UIColor *)purpleColor;     // 0.5, 0.0, 0.5 RGB

     + (UIColor *)brownColor;      // 0.6, 0.4, 0.2 RGB

     + (UIColor *)clearColor;      // 0.0 white, 0.0 alpha

     否则,程序会crash

     */

    NSString *text =@"Hello <font color=\"red\">core text <font color=\"blue\">world!\nHello <font color=\"lightGray\">core text <font color=\"green\">world!";//[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

    MarkupParser *p = [[[MarkupParseralloc]init ]autorelease];

    NSAttributedString *attString = [p attrStringFromMarkup:text];

    [self.customLabelsetAttString:attString];


解释:customLabel就是封装类库中自定义的CustomLable,我在这是直接拖的控件,如果你使用可以首先实例化出次对象,然后在进行运用。

多颜色主要用到的是HTML标签,MarkupParser的目的就是为了识别此标签。


类库下载


原创粉丝点击