About iOS Rich Text usage

Source: Internet
Author: User

By using rich text to make a string of different fonts, different word spacing, different colors of the settings

1. The following is the instance code

I encapsulated a method that can be called directly

-(void) Label: (UILabel *) label Andwithtext: (NSString *) text Andfont: (uifont *) font Andleftcolor: (Uicolor *) Leftcolor Andrightcolor: (Uicolor *) Rightcolor Andnumber: (long) Number andleftrangenummber: (Nsinteger) leftnumber{

Create Rich Text

nsmutableattributedstring * labelstring = [[Nsmutableattributedstring alloc] Initwithstring:[nsstring stringwithformat:@ "%@", text]];

Rich-Text fonts

[Labelstring addattribute:nsfontattributename Value:font range:nsmakerange (0, labelstring.length)];

Font color to the left of rich text

[Labelstring addattribute:nsforegroundcolorattributename Value:kscriptgraycolor range:NSMakeRange (0, LeftNumber)];

Font color to the right of rich text

[Labelstring addattribute:nsforegroundcolorattributename Value:kscriptblackcolor range:NSMakeRange (LeftNumber, Labelstring.length-leftnumber)];

Set word spacing to the left of rich text

Cfnumberref num = cfnumbercreate (kcfallocatordefault,kcfnumbersint8type,&number);

[Labelstring AddAttribute: (ID) kctkernattributename value: (__bridge ID) num range:nsmakerange (0,leftnumber-2)];

Cfrelease (num);

[Label Setattributedtext:labelstring];

}

2. For example, if you want to create a string with rich text, you can call the method directly.

[Self Label:self.hospitalLabel andwithtext:@ "hospital: 123" Andfont:[uifont fontwithname:kfontpingfangmedium Size: Kfitfontsize (+)] Andleftcolor:kscriptgraycolor andrightcolor:kscriptblackcolor andnumber:20.0f Andleftrangenummber:3];

3. The implementation of the specific example image is as follows:

About iOS Rich Text usage

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.