iOS label文字新增中劃線和下劃線
阿新 • • 發佈:2019-02-12
新增中劃線:
<code class="objectivec"> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UILabel</span> * strikeLabel = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UILabel</span> alloc] initWithFrame:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGRectMake</span>(<span class="hljs-number" style="color: rgb(0, 102, 102);">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">30</span>))]; <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *textStr = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> stringWithFormat:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"%@元"</span>, primeCost]; <span class="hljs-comment" style="color: rgb(136, 0, 0);">//中劃線</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDictionary</span> *attribtDic = @{<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSStrikethroughStyleAttributeName</span>: [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNumber</span> numberWithInteger:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSUnderlineStyleSingle</span>]}; <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableAttributedString</span> *attribtStr = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableAttributedString</span> alloc]initWithString:textStr attributes:attribtDic]; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 賦值</span> strikeLabel<span class="hljs-variable" style="color: rgb(102, 0, 102);">.attributedText</span> = attribtStr; [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.view</span> addSubview:strikeLabel];</code>
新增下劃線:
<code class="objectivec"> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">UILabel</span> *underlineLabel = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">UILabel</span> alloc] initWithFrame:(<span class="hljs-built_in" style="color: rgb(102, 0, 102);">CGRectMake</span>(<span class="hljs-number" style="color: rgb(0, 102, 102);">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102);">30</span>))]; <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> *textStr = [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSString</span> stringWithFormat:<span class="hljs-string" style="color: rgb(0, 136, 0);">@"%@元"</span>, primeCost]; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 下劃線</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSDictionary</span> *attribtDic = @{<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSUnderlineStyleAttributeName</span>: [<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSNumber</span> numberWithInteger:<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSUnderlineStyleSingle</span>]}; <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableAttributedString</span> *attribtStr = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSMutableAttributedString</span> alloc]initWithString:textStr attributes:attribtDic]; <span class="hljs-comment" style="color: rgb(136, 0, 0);">//賦值</span> underlineLabel<span class="hljs-variable" style="color: rgb(102, 0, 102);">.attributedText</span> = attribtStr; [<span class="hljs-keyword" style="color: rgb(0, 0, 136);">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102);">.view</span> addSubview:underlineLabel];</code>