label button等设置不同颜色的标题

简单的一个属性实现label每个字的颜色都不相同,现在只是提供静态的,就是要提供一个范围

 

    NSString *df=@"this is test!";

    

    NSMutableAttributedString *attriString =[[NSMutableAttributedStringalloc] initWithString:df];

    NSRange range2=[df rangeOfString:@"is"];

 

    

    NSArray *colors=[NSArrayarrayWithObjects:[UIColorredColor],[UIColorredColor],[UIColoryellowColor],[UIColorgreenColor],nil];

    

    [attriString addAttribute:NSForegroundColorAttributeName value:[colors objectAtIndex:1] range:range2];    

    UIButton *la=[[UIButtonalloc] initWithFrame:CGRectMake(100,200,300,80)];

    [la setBackgroundColor:[UIColorwhiteColor]];

    [la setTitle:df forState:UIControlStateNormal];

    //[la setAttributedText:attriString];

    [la setAttributedTitle:attriString forState:UIControlStateNormal];

    

    [self.view addSubview:la];

效果图

 

 

posted @ 2013-05-31 14:28  xiaoxiaoxigua  阅读(695)  评论(0编辑  收藏  举报