Two colors for UILabel TEXT

前端 未结 3 1295
轮回少年
轮回少年 2020-12-03 16:10

I want to set two colors for UILabel\'s text. I tried TTTRegexAttributedLabel, but it is throwing unknown type error.

I tried following co

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 16:57

    you can set text color with pattern image like bellow..

            [yourLable setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImageName"]]];
    

    and also set different color with this bellow code.. please check tutorial with detail mate..

    NSString *test = @"Hello. That is a test attributed string.";
    
     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.
         */
    
    
    
        //Lets choose the colors we want to have in our string
        CGColorRef _orange=[UIColor orangeColor].CGColor;
        CGColorRef _green=[UIColor greenColor].CGColor;
        CGColorRef _red=[UIColor redColor].CGColor;
        CGColorRef _blue=[UIColor blueColor].CGColor;    
    
    
    
    
        //Lets have our string with first 20 letters as orange
        //next 20 letters as green
        //next 20 as red
        //last remaining as blue
        CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 20),kCTForegroundColorAttributeName, _orange);
        CFAttributedStringSetAttribute(attrString, CFRangeMake(20, 20),kCTForegroundColorAttributeName, _green);
        CFAttributedStringSetAttribute(attrString, CFRangeMake(40, 20),kCTForegroundColorAttributeName, _red);    
        CFAttributedStringSetAttribute(attrString, CFRangeMake(60, _stringLength-61),kCTForegroundColorAttributeName, _blue);
    

    for more information see this tutorial....

    coretext-tutorial-for-ios-part

    i hope this help you...

提交回复
热议问题