A uilabel different parts show different colors

Source: Internet
Author: User

Let's take a look directly at it:

Requirements: is the table cell inside the state label, the front "state:" is black, the back of the status value is red, they are on the same label, how to do it?

Answer: Really is the meeting is not difficult, the difficult person will not ah, use rich text, easy to fix.

Crap not much to say, directly on the code:

1 //synthesize Rich text based on state values2-(Nsmutableattributedstring *) getstatestring: (NSString *) state{3 4     //the string after the composition5NSString *fullstr = [NSString stringWithFormat:@"Status:%@", State];6 7     //range of Status values8Nsrange range =[Fullstr rangeofstring:state];9     Ten     //Color of Status value display OneUicolor *color = [Uicolor kt_colorwithhex:0xff1e00Andalpha:1]; A      -     //convert a synthesized string to Rich text -Nsmutableattributedstring *attributedstr =[[Nsmutableattributedstring alloc] initwithstring:fullstr]; the  -     //add an attribute to the corresponding range - [Attributedstr Addattributes:@{nsforegroundcolorattributename:color} Range:range]; -      +     returnAttributedstr; -}

Then assign the rich text directly to the label's Attributedtext

1 self.stateLabel.attributedText = [self getstatestring:@ " not received order "];

A uilabel different parts show different colors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.