ios - 如何创建带删除线文本的 UILabel?

标签 ios swift cocoa-touch uilabel strikethrough

我想创建一个UILabel,其中的文本是这样的

enter image description here

我该怎么做?当文本很小时,线条也应该很小。

最佳答案

SWIFT 5 更新代码

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
    attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSRange(location: 0, length: attributeString.length))

然后:

yourLabel.attributedText = attributeString

让字符串的某些部分被击打然后提供范围

let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)

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

参数列表:

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

value : 与名称关联的属性值。

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

然后

yourLabel.attributedText = attributeString;

对于 低于 iOS 6.0 的版本,您需要第 3 方组件 来执行此操作。 其中之一是 TTTAttributedLabel , 另一个是 OHAttributedLabel .

关于ios - 如何创建带删除线文本的 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44930816/

相关文章:

ios - 如何防止 subview 重叠其父级ScrollView?

ios - 在另一个 UIViewController 中调用函数

iphone - 类似 Instagram 评论的富 TextView

ios - 使用自定义动画在 View 的两种状态之间进行动画处理

ios - 使用 Facebook Connect 登录后如何转到我的 IOS 应用程序的主视图

ios - Swift:滚动时标签和图像的 UICollectionView 单元格重复

iphone - 使用 XCode Instruments 解释 iPhone 开发内存警告的新功能,需要指导

ios - 如何对 UIPageViewController 处理的页面内的容器进行动画处理,并且该动画与页面滑动(页面更改)相关?

ios - 将 NSOperationQueue 与 NSURLSession 一起使用以在调用之间引入依赖性是一种不好的方法吗?

iphone - iOS iPhone 5 选择正确的 Storyboard