UILabel与文本通过 [英] UILabel with text struck through

查看:117
本文介绍了UILabel与文本通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 UILabel ,其中的文字如下所示

I want to create a UILabel in which the text is like this

我该如何做?

推荐答案

Objective-C

iOS 6.0> UILabel 支持 NSAttributedString

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
                        value:@2
                        range:NSMakeRange(0, [attributeString length])];

Swift

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "Your String here")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))

定义

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange

参数列表:

:指定属性名称的字符串。属性键可以由另一个框架提供,也可以是您定义的自定义键。有关如何查找系统提供的属性键的信息,请参阅NSAttributedString类参考中的概述部分。

name : A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.

value :属性值

aRange :指定属性/值对应用的字符范围。

aRange : The range of characters to which the specified attribute/value pair applies.

然后

yourLabel.attributedText = attributeString;






6.0版本,您需要第三方组件执行此操作。
其中一个是 TTTAttributedLabel ,另一个是 OHAttributedLabel


For lesser than iOS 6.0 versions you need 3-rd party component to do this. One of them is TTTAttributedLabel, another is OHAttributedLabel.

这篇关于UILabel与文本通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆