---
title: "[转]How to use NSAttributedString in iOS 6"
author: "re_reference"
date: "2012-12-25T16:09:19Z"
tags: []
categories: ["移动开发"]
slug: "how-to-use-nsattributedstring-in-ios-6"
draft: false
lastmod: "2012-12-25T16:19:27Z"
id: 1753897
url: "https://re-reference.iteye.com/blog/1753897"
---

本文转自：<http://soulwithmobiletechnology.blogspot.com/2012/07/how-to-use-nsattributedstring-in-ios-6.html>，版权归作者所有。


```oc

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)];

```

![](http://3.bp.blogspot.com/-uShhN6pyLac/UBi-fk_ZyGI/AAAAAAAADpY/rETzgxrJEac/s1600/NSAttributedString_Example1.png)



```oc

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)];

```

![](http://1.bp.blogspot.com/-z3jeOKt6acc/UBi-gtGawgI/AAAAAAAADpg/kVhUoBDt6rE/s1600/NSAttributedString_Example2.png)



```oc

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)];

```

![](http://4.bp.blogspot.com/-7nH5LC39lQg/UBi-hf8X9rI/AAAAAAAADpo/A-TOxBQob8k/s1600/NSAttributedString_Example3.png)



```oc

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)];

```

![](http://4.bp.blogspot.com/-f9_opUP91JI/UBi-hlKKqRI/AAAAAAAADpw/l3uAMKt1WEA/s1600/NSAttributedString_Example4.png)


```oc

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)];

```

![](http://1.bp.blogspot.com/-8Km3KUuMO0Y/UBi-ic8zGEI/AAAAAAAADp4/dVVuBN4pS4U/s1600/NSAttributedString_Example5.png)


```oc

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

```

![](http://4.bp.blogspot.com/-Y0dIE-d16Dg/UBi-jPR6hQI/AAAAAAAADqA/ZrFc2VHeu60/s1600/NSAttributedString_Example6.png)



```oc

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)];

```

![](http://1.bp.blogspot.com/-oMsKtHs9D6w/UBi-jvJCwCI/AAAAAAAADqI/dM5uf28WkNk/s1600/NSAttributedString_Example7.png)




```oc

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)];

```

![](http://3.bp.blogspot.com/-whMBRGadLak/UBi-kC-xgpI/AAAAAAAADqQ/yi315E6RXyA/s1600/NSAttributedString_Example8.png)