Construct and display text with styles

Source: Internet
Author: User

1. Problem:

You want to be able to directly display rich text in the UI group, instead of creating a separate UI component for each format. For example, if you want to display a sentence in a uilabel, only one word in the sentence is in bold.

 

2. Rich text is a good thing! Many programmers need to display various style strings in a line of text in the UI build.

The best way to construct an attribute string is to use the initwithstring: Method of the nsmutableattributedstring class and pass an nsstring to this method. This will create an attribute string without any attributes. Then, the setattributes: range: Method of the nsmutableattributedstring class is used to set attributes for different parts of the string. This method has two parameters:

    Setattributes(Attribute)

It is a dictionary. The keys around the dictionary are character attributes. The value of each key depends on the Key itself. The following are some important keys that can be set in the dictionary:

Nsfontattributename: the value of this key is an instance of uifont, used to define the font of the specified string range.

Nsforegroundcolorattributename: the value of this key is of the uicolor type, used to define the color of the specified string range.

Nsbackgroundcolorattributename: the value of this key is of the uicolor type, used to define the background color of the specified string range.

Nsshadowattributename: the value of this key must be an instance of nsshadow to define the shadow of a specified string range.

   Range
Is the nsange type, used to specify the starting point and length of the attribute applied to the string.

(Note: to view all the different keys that can be passed in the above method, you can view the nsmutableattributedstring class introduction in the Apple online documentation .)

3. Example:

The "iOS" attribute dictionary can be constructed using the following code:

NSDictionary *attributesForFirstWord = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],NSForegroundColorAttributeName : [UIColor redColor],NSBackgroundColorAttributeName : [UIColor blackColor]};

The "SDK" uses the following attributes:

NSShadow *shadow = [[NSShadow alloc] init];shadow.shadowColor = [UIColor darkGrayColor];shadow.shadowOffset = CGSizeMake(4.0f, 4.0f);NSDictionary *attributesForSecondWord = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],NSForegroundColorAttributeName : [UIColor whiteColor],NSBackgroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow};

The following code not only creates a label, but also sets the text attribute:

- (NSAttributedString *) attributedText{NSString *string = @"iOS SDK";NSMutableAttributedString *result = [[NSMutableAttributedString alloc]initWithString:string];NSDictionary *attributesForFirstWord = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],NSForegroundColorAttributeName : [UIColor redColor],NSBackgroundColorAttributeName : [UIColor blackColor]};NSShadow *shadow = [[NSShadow alloc] init];shadow.shadowColor = [UIColor darkGrayColor];shadow.shadowOffset = CGSizeMake(4.0f, 4.0f);NSDictionary *attributesForSecondWord = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],NSForegroundColorAttributeName : [UIColor whiteColor],NSBackgroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow};/* Find the string "iOS" in the whole string and sets its attribute */[result setAttributes:attributesForFirstWordrange:[string rangeOfString:@"iOS"]];/* Do the same thing for the string "SDK" */[result setAttributes:attributesForSecondWordrange:[string rangeOfString:@"SDK"]];return [[NSAttributedString alloc] initWithAttributedString:result];}- (void)viewDidLoad{[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];self.label = [[UILabel alloc] init];self.label.backgroundColor = [UIColor clearColor];self.label.attributedText = [self attributedText];[self.label sizeToFit];self.label.center = self.view.center;[self.view addSubview:self.label];}

 

Construct and display text with styles

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.