ios 如何在 UILabel 中制作带下划线的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13122207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:59:00  来源:igfitidea点击:

How to make an underlined text in UILabel?

ioscocoa-touchtextuilabelunderline

提问by Dev

How can I make an underlined text in UILabel?

如何在 中制作带下划线的文本UILabel

I had tried by making a UILabelwith height 0.5fand place it under the text. This line label is not visible when I am running my app in iPhone 4.3and iPhone 4.3 simulator, but it is visible in iPhone 5.0 simulatoronwards. Why?

我曾尝试制作UILabel高度为0.5f 的一个并将其放在文本下方。当我在iPhone 4.3iPhone 4.3 模拟器中运行我的应用程序时,此行标签不可见,但在iPhone 5.0 模拟器中可见。为什么?

How can I do this?

我怎样才能做到这一点?

回答by Paresh Navadiya

Objective-C

目标-C

iOS 6.0 > version

iOS 6.0 > 版本

UILabelsupports NSAttributedString

UILabel支持 NSAttributedString

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Hello Good Morning"];
[attributeString addAttribute:NSUnderlineStyleAttributeName
                        value:[NSNumber numberWithInt:1]
                        range:(NSRange){0,[attributeString length]}];

Swift

迅速

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "Hello Good Morning")
attributeString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length))

Definition:

定义

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

Parameters List:

Parameters List:

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.

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

value: The attribute value associated with name.

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

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

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

Now use like this:

现在像这样使用:

yourLabel.attributedText = [attributeString copy];


iOS 5.1.1 < version

iOS 5.1.1 < 版本

You needs 3 party attributed Labelto display attributed text:

您需要3 party attributed Label显示attributed text

1) Refer TTTAttributedLabellink. Its best third party attributedLabelto displayattributed text.

1) 参考TTTAttributedLabel链接。它的最好的第三方attributedLabeldisplaytext

2) refer OHAttributedLabelfor third party attributedLabel

2)为第三方参考OHAttributedLabelattributedLabel

回答by Anil Gupta

I am using Xcode 9 and iOS 11. To make the UILabel with an underline beneath it. You can use both 1. Using code 2. Using xib

我正在使用 Xcode 9 和 iOS 11。使 UILabel 下方带有下划线。您可以同时使用 1. 使用代码 2. 使用 xib

Using code:

使用代码:

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"I am iOS Developer"];
[attributeString addAttribute:NSUnderlineStyleAttributeName
                        value:[NSNumber numberWithInt:1]
                        range:(NSRange){0,[attributeString length]}];
lblAttributed.attributedText = attributeString;

Using Xib: enter image description here

使用 Xib: 在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

We can also use for button too.

我们也可以用于按钮。

回答by Mick

The Designer Way - Highly Customisable Version

设计师之道 - 高度可定制的版本

I personally prefer to customise my design(s) as much as possible. Sometimes, you will find that using the built-in underline tool is not easily customisable.

我个人更喜欢尽可能地定制我的设计。有时,您会发现使用内置的下划线工具并不容易定制。

Here is how I do it:

这是我如何做到的:

1First I create a UIView for the underline. You can then choose your background color, either in IB or programmatically. Constraints are key here.

1首先,我为下划线创建了一个 UIView。然后,您可以在 IB 中或以编程方式选择背景颜色。约束是这里的关键。

Create Your Underline view

创建您的下划线视图

2Then, you can customise the underline as much as you like. For example, to adapt the width of the underline for better visual effect, create an outlet connection to a constraint in Interface Builder

2然后,您可以根据需要自定义下划线。例如,为了适应下划线的宽度以获得更好的视觉效果,在 Interface Builder 中创建一个到约束的出口连接

enter image description here

在此处输入图片说明

3You can then easily programmatically customise your underline view. For example here, to give you an idea we will make the underline 20 px wider than the title "Breads":

3然后,您可以轻松地以编程方式自定义下划线视图。例如在这里,为了给您一个想法,我们将使下划线比标题“面包”宽 20 像素:

var originalString: String = "Breads"
let myString: NSString = originalString as NSString
let size: CGSize = myString.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14.0)])
self.widthUnderlineTitle.constant = size.width + 20

Results:

Results:

enter image description here

在此处输入图片说明

回答by Nenad M

Take a look at TTTAttributedLabel. If you're allowed to use 3d-party components just replace your UILabels with TTTAttributedLabel where you need it (drop-in replacement). Works with iOS < 6.0!

看看TTTAttributedLabel。如果你被允许使用 3d 方组件,只需在你需要的地方用 TTTAttributedLabel 替换你的 UILabels(直接替换)。适用于 iOS < 6.0!

回答by Juan Boero

Swift extension on UILabel:

UILabel 上的 Swift 扩展:

Still needs improvement, any thoughts are welcome:

仍然需要改进,欢迎任何想法:

extension UILabel{

    func underLine(){    
        if let textUnwrapped = self.text{
            let underlineAttribute = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
            let underlineAttributedString = NSAttributedString(string: textUnwrapped, attributes: underlineAttribute)
            self.attributedText = underlineAttributedString   
        }   
    }
}

One of the cons i can think of right now is that it will need to be called each time text changes, and there is no actual way of turning it off...

我现在能想到的缺点之一是每次文本更改时都需要调用它,并且没有实际的方法将其关闭...

回答by Ashish

Swift 4.0

斯威夫特 4.0

var attributeString = NSMutableAttributedString(string: "Hello Good Morning")
attributeString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSRange)

回答by Sergio

Swift 5

斯威夫特 5

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: yourLabel.text ?? "")
attributeString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: NSMakeRange(0, attributeString.length))
yourLabel.attributedText = attributeString