<a target="_blank" href="http://my.oschina.net/joanfen/blog/145184#">?</a>
1
2
3
<code>uilabel *mylabel = [[uilabel alloc] initwithframe:cgrectmake(40, 40, 120, 44)];</code>
<code> </code>
<code>[self.view addsubview:mylabel];</code>
①、设置默认文本
<code>nsstring *text = @</code><code>"标签文本"</code><code>;</code>
<code>mylabel.text = text;</code>
②、设置标签文本(此属性是ios6.0之后才出现,如若不是必要,不建议使用此属性)
4
5
6
7
<code>nsstring *text = @</code><code>"其实没什么"</code><code>;</code>
<code>nsmutableattributedstring *attributestring = [[nsmutableattributedstring alloc] initwithstring:text];</code>
<code>[attributestring setattributes:@{nsforegroundcolorattributename : [uicolor redcolor], nsfontattributename : [uifont systemfontofsize:17]} range:nsmakerange(2, 1)];</code>
<code>mylabel.attributedtext = attributestring;</code>
8
9
10
11
12
13
14
<code>nsstring *keyword = @</code><code>"开源"</code><code>;</code>
<code>nsstring *result = @</code><code>"开源中国社区"</code><code>;</code>
<code>// 设置标签文字</code>
<code>nsmutableattributedstring *attritutestring = [[nsmutableattributedstring alloc] initwithstring:result];</code>
<code>// 获取标红的位置和长度</code>
<code>nsrange range = [result rangeofstring:keyword];</code>
<code>// 设置标签文字的属性</code>
<code>[attritutestring setattributes:@{nsforegroundcolorattributename : [uicolor redcolor], nsfontattributename : [uifont systemfontofsize:17]} range:range];</code>
<code>// 显示在label上</code>
<code>label.attributedtext = attritutestring;</code>
③、设置字体,如果是使用②中的文本,那在设置attributestring的属性时已经设置过font了和textcolor了,直接使用①设置文本时设置文本时,设置字体方法
<code>mylabel.font = [uifont systemfontofsize:13];</code>
④、设置颜色
<code>mylabel.textcolor = [uicolor bluecolor];</code>
⑤、设置对齐方式
<code>mylabel.textalignment = nstextalignmentcenter;</code><code>//居中</code>
<code>nstextalignmentleft</code><code>//左对齐</code>
<code>nstextalignmentcenter</code><code>//居中</code>
<code>nstextalignmentright </code><code>//右对齐</code>
<code>nstextalignmentjustified</code><code>//最后一行自然对齐</code>
<code>nstextalignmentnatural</code><code>//默认对齐脚本</code>
nstextalignmentjustified和 nstextalignmentnatural用的时候会报错,程序崩溃,暂时不知道什么时候可以使用,希望知道的指教一下,感激不尽。
⑥、文字剪裁方式
<code>nslinebreakbywordwrapping = 0,</code><code>//以空格为边界,保留单词</code>
<code>nslinebreakbycharwrapping, </code><code>//保留整个字符</code>
<code>nslinebreakbyclipping, </code><code>//简单剪裁,到边界为止</code>
<code>nslinebreakbytruncatinghead, </code><code>//按照"……文字"显示</code>
<code>nslinebreakbytruncatingtail, </code><code>//按照"文字……文字"显示</code>
<code>nslinebreakbytruncatingmiddle</code><code>//按照"文字……"显示</code>
<code>mylabel.linebreakmode = nslinebreakbytruncatinghead;</code>
⑦、设置label enabled属性
如果设置为no,则文字颜色会变暗,表明其是不可用的,默认值为yes。
<code>mylabel.enabled = no;</code>
二、匹配label上的文字
①、是否根据文本宽度改变字体大小
<code>mylabel.adjustsfontsizetofitwidth = yes;</code>
<code>//假设文字内容为@"曾在月光之下望烟花,曾共看夕阳渐降下",label长度为200,则一行显示不下,若设置此属性为yes,则会降低字体大小,以显示全部内容。</code>
前后对比:
②、改变字母之间的间距来适应label大小
<code>//当这个属性是yes,标签可能改变标签文本的字母间距,以使该文本更适合标签的边界内。此属性的字符串,而不管当前行的行的裁剪模式。该属性的默认值是no。</code>
<code>mylabel.adjustsletterspacingtofitwidth = no;</code>
<code>//个人使用了一下,没发现有什么区别,不知道具体是什么时候发挥作用。</code>
③、设置对齐基线
<code>mylabel.adjustsfontsizetofitwidth = yes;</code><code>//调整基线位置需将此属性设置为yes</code>
<code>mylabel.baselineadjustment = uibaselineadjustmentalignbaselines;</code>
此属性有三个值可选
<code>uibaselineadjustmentalignbaselines</code><code>//文本最上端与label中线对齐,默认值</code>
<code>uibaselineadjustmentaligncenters </code><code>//文本中线与label中线对齐</code>
<code>uibaselineadjustmentnone </code><code>//文本最下端与label中线对齐</code>
④、最小字体大小,当字体小于这个最小值时无效,显示此属性值
ios6.0之前:minimumfontsize
ios6.0之后:minimumscalefactor
<code>mylabel.minimumscalefactor = 10.0;</code><code>//默认值为0,为当前字体大小</code>
<code>mylabel.numberoflines = 2;</code><code>//label行数</code>
⑥、高亮
<code>mylabel.highlighted = yes;</code><code>//是否高亮</code>
<code>mylabel.highlightedtextcolor = [uicolor redcolor];</code><code>//高亮颜色;此属性在设置按钮的titlelabel时,无论highlighted是yes还是no,在按钮按下时标题都显示此高亮颜色</code>
⑦、阴影
<code>mylabel.shadowcolor = [uicolor graycolor];</code><code>//阴影颜色,默认为nil</code>
<code>mylabel.shadowoffset = cgsizemake(1, 1);</code><code>//阴影的偏移点</code>
三、label位置
①、计算uilabel 随字体多行后的高度
<code>cgrect result,bounds;</code>
<code>bounds = cgrectmake(0, 0,200, 300);</code>
<code>heightlabel = [mylabel textrectforbounds:bounds limitedtonumberoflines:20];</code><code>//计算20行后的label的frame</code>
<code>nslog(@</code><code>"%f"</code><code>,heightlabel.size.height);</code>
②、绘制text到指定区域
<code>- (</code><code>void</code><code>)drawtextinrect:(cgrect)rect</code>
<code>//需要重载此方法,然后由子类调用,重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了</code>
<code></code>