iOS common technology-label Rich text

Source: Internet
Author: User

//
Viewcontroller.m
Label Rich Text
//
Created by Big Huan on 16/1/19.
COPYRIGHT©2016 year BJSXT. All rights reserved.
//

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];


Nsmutableparagraphstyle * paragraph = [[Nsmutableparagraphstyle alloc] init];
paragraph.linespacing = 10;
paragraph.paragraphspacing = 50;
Paragraph.firstlineheadindent = 50;

NSString * examplestring = @ "According to the list published by celebrity charity, the Canadian star team led by Drake will include: NBA two scoring King McGrady, former Lakers three crown members Rick Fox, tennis player Milos Launick and actor and singer Anna. \ Kevin Hart led by the American Star team members will include: Changsi Bilups, "Bug" Boggs, the famous host Nick Cannon, Anthony Anderson. ";

Nsdictionary * dicta = @{nsfontattributename:[uifont systemfontofsize:20],
Nsforegroundcolorattributename:[uicolor Greencolor],
Nsbackgroundcolorattributename:[uicolor Graycolor],
Nsparagraphstyleattributename:paragraph
nsobliquenessattributename:@0.5//Italic
Nsstrokecolorattributename:[uicolor Whitecolor],
nsstrokewidthattributename:@2,//Strokes
nskernattributename:@20,//Word Spacing
nsstrikethroughstyleattributename:@2,//Delete Line
nsunderlinestyleattributename:@1,//Underline
};

nsattributedstring * attribute = [[nsattributedstring alloc] initwithstring:examplestring attributes:dicta];

UILabel * label = [[UILabel alloc] Initwithframe:cgrectmake (All, Cgrectgetwidth (self.view.bounds)-40, 500)];
Label.attributedtext = attribute;
Label.backgroundcolor = [Uicolor Bluecolor];
Label.numberoflines = 0;
[Self.view Addsubview:label];
}
@end
/****************************************************************/

/****************************************************************/

//
Viewcontroller.m
Label Rich Text 2
//
Created by Ma C on 16/1/19.
COPYRIGHT©2016 year BJSXT. All rights reserved.
//

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
[Self example1];
[Self example2];
[Self example4];
}

-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}

-(void) Example2 {
NSString * string = @ "@ Big Huan, come out to play!" ";

nsmutableattributedstring * attributestring = [[nsmutableattributedstring alloc]initwithstring:string];

Nsrange Range1 = {0, 3};

Nsdictionary *dict1 = @{
Nsforegroundcolorattributename:[uicolor Bluecolor],
Nsfontattributename:[uifont Systemfontofsize:25],
};

[AttributeString Addattributes:dict1 Range:range1];

Nsdictionary *dict2 = @{
Nsforegroundcolorattributename:[uicolor Greencolor],
Nsfontattributename:[uifont Systemfontofsize:22],
};

nsattributedstring * string2 = [[Nsattributedstring alloc] initwithstring:@ "Good da! [Attributes:dict2];

[AttributeString Appendattributedstring:string2];
UILabel * label = [[UILabel alloc]initwithframe:cgrectmake (Cgrectgetwidth (self.view.frame)-20, 30)];

Label.attributedtext = attributestring;
Label.textalignment = Nstextalignmentcenter;
Label.backgroundcolor = [Uicolor Yellowcolor];
[Self.view Addsubview:label];
}
/*
How to use:

Set multiple properties for text in a range
-(void) SetAttributes: (nsdictionary *) Attrs range: (nsrange) range;

Add a property to a range of text
-(void) AddAttribute: (NSString *) Name value: (ID) value range: (nsrange) range;

Add multiple attributes to a range of text
-(void) AddAttributes: (nsdictionary *) Attrs range: (nsrange) range;

To remove an attribute in a range
-(void) RemoveAttribute: (NSString *) name range: (nsrange) range;

Common Properties and descriptions
Nsfontattributename fonts

Nsparagraphstyleattributename paragraph format

Nsforegroundcolorattributename Font Color

Nsbackgroundcolorattributename background Color

Nsstrikethroughstyleattributename Strikethrough formatting

Nsunderlinestyleattributename underline format

Nsstrokecolorattributename Strikethrough Color

Nsstrokewidthattributename Strikethrough width

Nsshadowattributename Shadow
*/
-(void) example1 {

NSString * examplestring = @ "Jobs---apple";

nsmutableattributedstring * Mutableattribute = [[nsmutableattributedstring alloc]initwithstring:examplestring];

Nsrange Range1 = {0, 3};
Nsrange Range2 = {6, 5};

[Mutableattribute addattribute:nsfontattributename Value:[uifont systemfontofsize:50] range:range1];

Nsdictionary * Dict = @{
Nsforegroundcolorattributename:[uicolor Magentacolor],
Nsbackgroundcolorattributename:[uicolor Cyancolor]
};
[Mutableattribute addattributes:dict Range:range2];

UILabel * label = [[UILabel Alloc]initwithframe:cgrectmake (Ten, self.view.frame.size.width-20, 60)];

Label.attributedtext = Mutableattribute;
Label.backgroundcolor = [Uicolor Graycolor];
Label.textalignment = Nstextalignmentcenter;
[Self.view Addsubview:label];
}

-(void) Example3 {
NSString * string = @ "12345";
nsmutableattributedstring * Mutableattibute = [[nsmutableattributedstring alloc]initwithstring:string];
Nsrange range1 = Nsmakerange (0, 3);
[Mutableattibute addattribute:nsfontattributename Value:[uifont systemfontofsize:23] range:range1];
UILabel * label = [[UILabel Alloc]initwithframe:cgrectmake (Ten, Cgrectgetwidth (Self.view.frame), 50)];
Label.textcolor = [Uicolor Redcolor];
Label.textalignment = Nstextalignmentcenter;
Label.attributedtext = Mutableattibute;
[Self.view Addsubview:label];
}

-(void) Example4 {
NSString * string = @ "Remember when you said the house was the only castle with the rice-water river continues to run slightly smile childhood dream I know don't cry let the Firefly take you escape the country's ballad forever Rely on Go home back to the original good";
Nsmutableparagraphstyle * paragraph = [[Nsmutableparagraphstyle alloc]init];
paragraph.linespacing = 4;
paragraph.paragraphspacing = 7;
Paragraph.firstlineheadindent = 25;

Nsdictionary * Dict = @{
Nsfontattributename:[uifont Systemfontofsize:20],
Nsparagraphstyleattributename:paragraph,
};

nsmutableattributedstring * Mutableattribute = [[nsmutableattributedstring alloc]initwithstring:string attributes: DICT];

UILabel * label = [[UILabel Alloc]initwithframe:cgrectmake (Ten, Cgrectgetwidth (self.view.frame)-20, 0)];
Label.attributedtext = Mutableattribute;
Label.numberoflines = 0;
[Label SizeToFit];
[Self.view Addsubview:label];
}

@end



iOS common technology-label Rich text

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.