本文转自:[url]http://soulwithmobiletechnology.blogspot.com/2012/07/how-to-use-nsattributedstring-in-ios-6.html[/url],版权归作者所有。
[img]http://3.bp.blogspot.com/-uShhN6pyLac/UBi-fk_ZyGI/AAAAAAAADpY/rETzgxrJEac/s1600/NSAttributedString_Example1.png[/img]
[img]http://1.bp.blogspot.com/-z3jeOKt6acc/UBi-gtGawgI/AAAAAAAADpg/kVhUoBDt6rE/s1600/NSAttributedString_Example2.png[/img]
[img]http://4.bp.blogspot.com/-7nH5LC39lQg/UBi-hf8X9rI/AAAAAAAADpo/A-TOxBQob8k/s1600/NSAttributedString_Example3.png[/img]
[img]http://4.bp.blogspot.com/-f9_opUP91JI/UBi-hlKKqRI/AAAAAAAADpw/l3uAMKt1WEA/s1600/NSAttributedString_Example4.png[/img]
[img]http://1.bp.blogspot.com/-8Km3KUuMO0Y/UBi-ic8zGEI/AAAAAAAADp4/dVVuBN4pS4U/s1600/NSAttributedString_Example5.png[/img]
[img]http://4.bp.blogspot.com/-Y0dIE-d16Dg/UBi-jPR6hQI/AAAAAAAADqA/ZrFc2VHeu60/s1600/NSAttributedString_Example6.png[/img]
[img]http://1.bp.blogspot.com/-oMsKtHs9D6w/UBi-jvJCwCI/AAAAAAAADqI/dM5uf28WkNk/s1600/NSAttributedString_Example7.png[/img]
[img]http://3.bp.blogspot.com/-whMBRGadLak/UBi-kC-xgpI/AAAAAAAADqQ/yi315E6RXyA/s1600/NSAttributedString_Example8.png[/img]
infoString=@"This is an example of Attributed String";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
NSInteger _stringLength=[infoString length];
UIColor *_black=[UIColor blackColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, _stringLength)];
[img]http://3.bp.blogspot.com/-uShhN6pyLac/UBi-fk_ZyGI/AAAAAAAADpY/rETzgxrJEac/s1600/NSAttributedString_Example1.png[/img]
UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:3.0] range:NSMakeRange(0, _stringLength)];
[img]http://1.bp.blogspot.com/-z3jeOKt6acc/UBi-gtGawgI/AAAAAAAADpg/kVhUoBDt6rE/s1600/NSAttributedString_Example2.png[/img]
UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-3.0] range:NSMakeRange(0, _stringLength)];
[img]http://4.bp.blogspot.com/-7nH5LC39lQg/UBi-hf8X9rI/AAAAAAAADpo/A-TOxBQob8k/s1600/NSAttributedString_Example3.png[/img]
UIColor *_red=[UIColor redColor];
UIColor *_green=[UIColor greenColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_green range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-3.0] range:NSMakeRange(0, _stringLength)];
[img]http://4.bp.blogspot.com/-f9_opUP91JI/UBi-hlKKqRI/AAAAAAAADpw/l3uAMKt1WEA/s1600/NSAttributedString_Example4.png[/img]
UIColor *_green=[UIColor greenColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
NSShadow *shadowDic=[[NSShadow alloc] init];
[shadowDic setShadowBlurRadius:5.0];
[shadowDic setShadowColor:[UIColor grayColor]];
[shadowDic setShadowOffset:CGSizeMake(0, 3)];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_green range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSShadowAttributeName value:shadowDic range:NSMakeRange(0, _stringLength)];
[img]http://1.bp.blogspot.com/-8Km3KUuMO0Y/UBi-ic8zGEI/AAAAAAAADp4/dVVuBN4pS4U/s1600/NSAttributedString_Example5.png[/img]
UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSKernAttributeName value:[NSNumber numberWithInt:5] range:NSMak
[img]http://4.bp.blogspot.com/-Y0dIE-d16Dg/UBi-jPR6hQI/AAAAAAAADqA/ZrFc2VHeu60/s1600/NSAttributedString_Example6.png[/img]
UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0, _stringLength)];
[img]http://1.bp.blogspot.com/-oMsKtHs9D6w/UBi-jvJCwCI/AAAAAAAADqI/dM5uf28WkNk/s1600/NSAttributedString_Example7.png[/img]
UIColor *_blue=[UIColor blueColor];
UIColor *_blueL=[UIColor colorWithRed:0 green:0 blue:0.5 alpha:0.7];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_blue range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSBackgroundColorAttributeName value:_blueL range:NSMakeRange(0, 20)];
[img]http://3.bp.blogspot.com/-whMBRGadLak/UBi-kC-xgpI/AAAAAAAADqQ/yi315E6RXyA/s1600/NSAttributedString_Example8.png[/img]