iOS NSAttributedString所有文本属性详解(多图)

转载 2016年08月31日 08:39:58

转自 http://blog.csdn.net/hello_hwc/article/details/46731991 

前言:这篇文章算是整理吧,因为最近逛Stackoverflow的时候,发现自己这部分掌握的不是很好。


一个简单的例子

绘制不同颜色不同字体的一个AttributeString,如图 
这里写图片描述 
代码

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> * Label = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> alloc]initWithFrame:CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>)];
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.view</span> addSubview:Label];
    NSMutableAttributedString * firstPart = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wenchen"</span>];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * firstAttributes = @{ NSFontAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> boldSystemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>],NSForegroundColorAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIColor</span> redColor],};
    [firstPart setAttributes:firstAttributes range:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,firstPart<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span>)];
    NSMutableAttributedString * secondPart = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" Huang"</span>];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * secondAttributes = @{NSFontAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> systemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>],NSForegroundColorAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIColor</span> blueColor],};
    [secondPart setAttributes:secondAttributes range:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,secondPart<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span>)];
    [firstPart appendAttributedString:secondPart];
    Label<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.attributedText</span> = firstPart;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

先看看所有的Key

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-constant" style="box-sizing: border-box;">NSFontAttributeName</span>; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>字体,value是<span class="hljs-constant" style="box-sizing: border-box;">UIFont</span>对象
<span class="hljs-constant" style="box-sizing: border-box;">NSParagraphStyleAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>绘图的风格(居中,换行模式,间距等诸多风格),value是<span class="hljs-constant" style="box-sizing: border-box;">NSParagraphStyle</span>对象
<span class="hljs-constant" style="box-sizing: border-box;">NSForegroundColorAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 文字颜色,value是<span class="hljs-constant" style="box-sizing: border-box;">UIFont</span>对象
<span class="hljs-constant" style="box-sizing: border-box;">NSBackgroundColorAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 背景色,value是<span class="hljs-constant" style="box-sizing: border-box;">UIFont</span>
<span class="hljs-constant" style="box-sizing: border-box;">NSLigatureAttributeName</span>; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>  字符连体,value是<span class="hljs-constant" style="box-sizing: border-box;">NSNumber</span>
<span class="hljs-constant" style="box-sizing: border-box;">NSKernAttributeName</span>; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 字符间隔
<span class="hljs-constant" style="box-sizing: border-box;">NSStrikethroughStyleAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>删除线,value是<span class="hljs-constant" style="box-sizing: border-box;">NSNumber</span>
<span class="hljs-constant" style="box-sizing: border-box;">NSUnderlineStyleAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>下划线,value是<span class="hljs-constant" style="box-sizing: border-box;">NSNumber</span>
<span class="hljs-constant" style="box-sizing: border-box;">NSStrokeColorAttributeName</span>; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>描绘边颜色,value是<span class="hljs-constant" style="box-sizing: border-box;">UIColor</span>
<span class="hljs-constant" style="box-sizing: border-box;">NSStrokeWidthAttributeName</span>; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>描边宽度,value是<span class="hljs-constant" style="box-sizing: border-box;">NSNumber</span>
<span class="hljs-constant" style="box-sizing: border-box;">NSShadowAttributeName</span>; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>阴影,value是<span class="hljs-constant" style="box-sizing: border-box;">NSShadow</span>对象
<span class="hljs-constant" style="box-sizing: border-box;">NSTextEffectAttributeName</span>; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>文字效果,value是<span class="hljs-constant" style="box-sizing: border-box;">NSString</span>
<span class="hljs-constant" style="box-sizing: border-box;">NSAttachmentAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>附属,value是<span class="hljs-constant" style="box-sizing: border-box;">NSTextAttachment</span> 对象
<span class="hljs-constant" style="box-sizing: border-box;">NSLinkAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>链接,value是<span class="hljs-constant" style="box-sizing: border-box;">NSURL</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">or</span> <span class="hljs-constant" style="box-sizing: border-box;">NSString</span>
<span class="hljs-constant" style="box-sizing: border-box;">NSBaselineOffsetAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>基础偏移量,value是<span class="hljs-constant" style="box-sizing: border-box;">NSNumber</span>对象
<span class="hljs-constant" style="box-sizing: border-box;">NSUnderlineColorAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>下划线颜色,value是<span class="hljs-constant" style="box-sizing: border-box;">UIColor</span>对象
<span class="hljs-constant" style="box-sizing: border-box;">NSStrikethroughColorAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>删除线颜色,value是<span class="hljs-constant" style="box-sizing: border-box;">UIColor</span>
<span class="hljs-constant" style="box-sizing: border-box;">NSObliquenessAttributeName</span>; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>字体倾斜
<span class="hljs-constant" style="box-sizing: border-box;">NSExpansionAttributeName</span>; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>字体扁平化
<span class="hljs-constant" style="box-sizing: border-box;">NSVerticalGlyphFormAttributeName</span>;<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>垂直或者水平,value是 <span class="hljs-constant" style="box-sizing: border-box;">NSNumber</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>表示水平,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>垂直</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>

字体,颜色,背景色

涉及到的属性 
- NSFontAttributeName 
- NSForegroundColorAttributeName 
- NSBackgroundColorAttributeName

效果 
这里写图片描述

代码

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> * Label = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> alloc]initWithFrame:CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>)];
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.view</span> addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wenchen"</span>];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * attris = @{NSForegroundColorAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIColor</span> whiteColor],NSBackgroundColorAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIColor</span> grayColor],NSFontAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> boldSystemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span>]};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,mutableAttriStr<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span>)];
    Label<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.attributedText</span> = mutableAttriStr;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

下划线

涉及到的两个属性

  • NSUnderlineStyleAttributeName
  • NSUnderlineColorAttributeName

效果 
这里写图片描述

代码

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> * Label = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> alloc]initWithFrame:CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>)];
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.view</span> addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wenchen"</span>];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * attris = @{NSFontAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> boldSystemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>],NSForegroundColorAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIColor</span> redColor],                          NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),                             NSUnderlineColorAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIColor</span> blueColor],};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,mutableAttriStr<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span>)];
    Label<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.attributedText</span> = mutableAttriStr;
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

描边

涉及到的两个属性

  • NSStrokeColorAttributeName
  • NSStrokeWidthAttributeName

效果 
这里写图片描述

代码

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> * Label = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> alloc]initWithFrame:CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>)];
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.view</span> addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wenchen"</span>];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * attris = @{NSForegroundColorAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIColor</span> whiteColor],NSStrokeColorAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIColor</span> greenColor],NSStrokeWidthAttributeName:@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,mutableAttriStr<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span>)];
    Label<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.attributedText</span> = mutableAttriStr;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

附属属性(例如图片)

涉及到的属性

  • NSAttachmentAttributeName

效果 
这里写图片描述 
代码

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> * Label = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> alloc]initWithFrame:CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>)];
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.view</span> addSubview:Label];
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建Attachment Str</span>
    NSTextAttachment * attach = [[NSTextAttachment alloc] init];
    attach<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.image</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIImage</span> imageNamed:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"memoAccess"</span>];
    attach<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.bounds</span> = CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>);
    NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:attach];
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//添加</span>
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wenchen"</span>];
    [mutableAttriStr appendAttributedString:imageStr];
    Label<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.attributedText</span> = mutableAttriStr;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

绘制风格

为什么要红字?因为这个属性真的很重要。

涉及到的属性

  • NSMutableParagraphStyle

    效果(看起来很奇怪,仅仅为了展示某些功能) 
    这里写图片描述

    代码

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">TestView</span> : <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">UIView</span></span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span>

<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@implementation</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">TestView</span></span>
-(instancetype)initWithFrame:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">CGRect</span>)frame{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> initWithFrame:frame];
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>;
    }
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.opaque</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NO</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>;
}

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// An empty implementation adversely affects performance during animation.</span>
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)drawRect:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">CGRect</span>)rect {
    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"The anthor of this blog is wenchenhuang"</span>];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.alignment</span> = NSTextAlignmentRight;
    paragraphStyle<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.headIndent</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4.0</span>;
    paragraphStyle<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.lineBreakMode</span> = NSLineBreakByCharWrapping;
    paragraphStyle<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.lineSpacing</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.0</span>;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * attributes = @{NSParagraphStyleAttributeName:paragraphStyle};
    [attributeStr setAttributes:attributes range:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, attributeStr<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span>)];
    [attributeStr drawInRect:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.bounds</span>];
}
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul>
<code class="hljs fix has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-attribute" style="box-sizing: border-box;">    TestView  *test </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> [[TestView alloc] initWithFrame:CGRectMake(100, 100,100, 60)];
    [self.view addSubview:test];</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

阴影

涉及到的属性

  • NSShadowAttributeName

效果 
这里写图片描述

<code class="hljs rsl has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">  UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>)];
    [self.view addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wenchen"</span>];
    NSShadow * <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">shadow</span> = [[NSShadow alloc] init];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">shadow</span>.shadowColor = [UIColor blueColor];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">shadow</span>.shadowBlurRadius = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.0</span>;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">shadow</span>.shadowOffset = CGSizeMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.0</span>);
    NSDictionary * attris = @{NSShadowAttributeName:<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">shadow</span>};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,mutableAttriStr.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">length</span>)];
    Label.attributedText = mutableAttriStr;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

文字效果

相关属性

  • NSTextEffectAttributeName 
    这里写图片描述

代码

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> * Label = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> alloc]initWithFrame:CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>)];
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.view</span> addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wenchen"</span>];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * attris = @{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,mutableAttriStr<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span>)];
    Label<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.attributedText</span> = mutableAttriStr;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

链接

相关属性

  • NSLinkAttributeName

例子

点击打开链接

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">ViewController</span> ()<<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">UITextViewDelegate</span>></span>

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span>

<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@implementation</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">ViewController</span></span>



- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)viewDidLoad {
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> viewDidLoad];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITextView</span> *textView = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITextView</span> alloc] initWithFrame:CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>)];
    textView<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.scrollEnabled</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NO</span>;
    textView<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.editable</span> = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NO</span>;
    textView<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.textContainer</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.lineFragmentPadding</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
    textView<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.textContainerInset</span> = UIEdgeInsetsMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);
    textView<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.delegate</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>;
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.view</span> addSubview:textView];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wenchen"</span>];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * attris = @{NSLinkAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> URLWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://www.baidu.com"</span>]};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,mutableAttriStr<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span>)];
    textView<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.attributedText</span> = mutableAttriStr;
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Do any additional setup after loading the view, typically from a nib.</span>
}
- (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span>)textView:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITextView</span> *)textView shouldInteractWithURL:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> *)url inRange:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span>)characterRange
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li></ul>

文字连体

涉及到的属性

  • NSLigatureAttributeName

举例 
NSLigatureAttributeName 属性位@(0) 和@(1) 
这里写图片描述

这里写图片描述


字符间隔

相关属性

  • NSKernAttributeName

举例 字符间距拉大到4 
这里写图片描述 
对比下,默认的 
这里写图片描述

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> * Label = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UILabel</span> alloc]initWithFrame:CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">120</span>)];
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.view</span> addSubview:Label];
    NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Wenchen"</span>];
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * attris = @{NSKernAttributeName:@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>),
                              NSFontAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> systemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>]};
    [mutableAttriStr setAttributes:attris range:NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,mutableAttriStr<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span>)];
    Label<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.attributedText</span> = mutableAttriStr;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

baseline基础偏移量

相关属性 
- NSBaselineOffsetAttributeName

效果 
对比下偏移量为0 和20 

代码

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * attris = @{NSBaselineOffsetAttributeName:@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>),
                              NSFontAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> systemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>]};</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

字体倾斜

相关属性

  • NSObliquenessAttributeName 
    效果 
    这里写图片描述

代码

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * attris = @{NSObliquenessAttributeName:@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.5</span>),
                              NSFontAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> systemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>]};</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

字体扁平化

相关属性

  • NSExpansionAttributeName

效果 
这里写图片描述

代码

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> * attris = @{NSExpansionAttributeName:@(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.0</span>),
                              NSFontAttributeName:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIFont</span> systemFontOfSize:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>]};</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

相关文章推荐

iOS NSAttributedString所有文本属性详解(多图)

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK详解专栏 http://blog.csdn.net/column/details/huan...

NSAttributedString文本属性设置的使用

使用NSAttributedString进行文本属性的设置。 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12 NSFontAttributeName NSMutabl...

iOS-详解文本属性attributes,设置段落格式,空心字体

1.NSKernAttributeName: @10 调整字句 kerning 字句调整   2.NSFontAttributeName : [UIFont systemFontOfSiz...

iOS学习 详解文本属性Attributes

1.NSKernAttributeName: @10 调整字句 kerning 字句调整               2.NSFontAttributeName : [UIFont systemFo...

iOS- 详解文本属性Attributes

1.NSKernAttributeName: @10 调整字句 kerning 字句调整   2.NSFontAttributeName : [UIFont systemFon...

iOS- 详解文本属性Attributes-IOS

.NSKernAttributeName: @10 调整字句 kerning 字句调整               2.NSFontAttributeName : [UIFont systemFon...
  • mc0720
  • mc0720
  • 2015年03月09日 15:07
  • 132

iOS 富文本 文字分别设置 文本属性 Attributes 文字样式 程序开发 效果图

iOS 富文本 文字分别设置 文本属性 Attributes 文字样式  想要做自己的效果,那就把下边的字典组合,字典组合要是不会,我帮不了你了 // 建一个小文本用来测试 UILa...

iOS开发富文本NSAttributedString属性

// // NSAttributedString.h // UIKit // // Copyright (c) 2011-2015 Apple Inc. All rights r...

给iOS开发新手送点福利,简述文本属性Attributes的用法

文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont sy...

iOS 【UIKit-文本属性 Attributes】

文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFo...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS NSAttributedString所有文本属性详解(多图)
举报原因:
原因补充:

(最多只允许输入30个字)