如何创建带有删除线文本的UILabel? [英] How can I create a UILabel with strikethrough text?

查看:466
本文介绍了如何创建带有删除线文本的UILabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

How can I do this? When the text is small, the line should also be small.

推荐答案

SWIFT 4更新代码

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "Your Text")
    attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, 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])];

快速

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

Parameters List:

名称:指定属性名称的字符串.属性键可以由另一个框架提供,也可以是您定义的自定义键.有关在哪里可以找到系统提供的属性键的信息,请参阅《 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 : The attribute value associated with name.

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

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

然后

yourLabel.attributedText = attributeString;


对于lesser than iOS 6.0 versions,您需要3-rd party component才能执行此操作. 其中一个是 TTTAttributedLabel ,另一个是


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天全站免登陆