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

在此处输入图像描述

我该怎么做?当文本很小时,行也应该很小.

推荐答案

SWIFT 5 UPDATE CODE

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

然后:

yourLabel.attributedText = attributeString

To make some part of string to strike then provide range

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))

Definition:

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

Parameters List:

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

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

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

然后

yourLabel.attributedText = attributeString;

对于lesser than iOS 6.0 versions,你需要3-rd party component才能做到这一点. 其中一个是TTTAttributedLabel,另一个是OHAttributedLabel.

Ios相关问答推荐

崩溃到初始化react -带网页的原生iOS应用程序

如何在SwiftUI中绑定文本字段,同时仍使用它来存储Collection 夹?

自定义组件中的通用型-无法访问特定于收件箱的属性

Swift:从字符串目录获取带有参数的本地化字符串

更新@PersistableEnum时是否需要执行迁移块?

在工作表上移动图像SwiftUI

XcodeCloud生成失败,返回";ci_pre_xcodeBuild.sh是可执行文件,但退出时返回2个退出代码.";

OBJC @selector 在另一个实现中不起作用

多协议和类继承混乱,Swift

如何从单独的视图更改修饰符的值

如何更改 SwiftUI 弹出视图的大小?

如何擦除 Swift 中的 Core Graphics Context 绘图?

使用 Foundation 解压缩文件

如何在字段包含字典数组的firestore上查询?

我可以使用同一个 Firebase 应用将多个 Flutter 应用添加到一个 Firebase 数据库吗?

检测 iPhone 应用程序中是否存在摄像头?

实现一个将块用作回调的方法

NSURLConnection 的 Xcode 4 警告未使用表达式结果

如何在 Xcode 4 中切换 .h 和 .m

如何为 NSDate 添加一个月?