ios 向故事板中的 UILabel 属性字符串添加下划线失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25099736/
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-31 01:40:07  来源:igfitidea点击:

Adding underline to UILabel attributed string from the storyboard fails

iosobjective-cuilabelnsattributedstring

提问by Katedral Pillon

  1. From the storyboard I select the UILabel in question
  2. Then in Attribute Inspector > Label > [I choose] Attributed
  3. Also in Attribute Inspector > Label > Text> [I select the content]
  4. Then I click on the font icon and choose underline
  1. 从故事板中,我选择了有问题的 UILabel
  2. 然后在属性检查器 > 标签 > [我选择] 属性
  3. 同样在属性检查器>标签>文本>[我选择内容]
  4. 然后我点击字体图标并选择下划线

Basically, any change that I select from the Fonts window that pops up does not take effect.

基本上,我从弹出的字体窗口中选择的任何更改都不会生效。

Has anyone ever successfully add underline from the storyboard?

有没有人成功地从故事板中添加下划线?

Note: I already know how to do this in code. I want to avoid adding code.

注意:我已经知道如何在代码中做到这一点。我想避免添加代码。

回答by Abdullah Saeed

Here is the solution in storyboard: Open the Attribute Inspector (make sure label is selected), Change the dropdown value from 'Plain' to 'Attributed'. Now a a small text editor will be visible under the font of the label. Select the text of the label, right click and change the font to 'underline'.

这是故事板中的解决方案:打开属性检查器(确保选择了标签),将下拉值从“Plain”更改为“Attributed”。现在一个小的文本编辑器将在标签的字体下可见。选择标签的文本,右键单击并将字体更改为“下划线”。

I also have attached a screenshot and successfully underlined a text using storyboard in XCode 7.0.1enter image description here

我还附上了截图并在 XCode 7.0.1 中使用故事板成功地为文本加了下划线在此处输入图片说明

回答by Uladzimir

Underline your text in TextEdit(cmd-U) and copy-paste it in Attribute Inspector > Label > Text > Attributed.

在 TextEdit(cmd-U) 中为您的文本加下划线并将其复制粘贴到 Attribute Inspector > Label > Text > Attributed。

回答by Iris Veriris

Select label -> Attribute editor -> Title = Attributed

选择标签 -> 属性编辑器 -> 标题 = 属性

Select the text of the label in the text view in the editor -> Right click -> Font -> check Underline

在编辑器中的文本视图中选择标签的文本-> 右键单击​​-> 字体-> 选中下划线

If the change is not visible -> resize the label

如果更改不可见 -> 调整标签大小

回答by Sajid Zeb

You can make text underline from storyboard like this. But when you change the text programatically it will override and underline will gone. So if you have to change the text on runtime. Do this.

您可以像这样从情节提要中为文本添加下划线。但是当您以编程方式更改文本时,它将覆盖并且下划线将消失。因此,如果您必须在运行时更改文本。做这个。

For Swift 3

对于 Swift 3

 let text = myLabel.text
 let textRange = NSMakeRange(0, (text?.characters.count)!)
 let attributedText = NSMutableAttributedString(string: text!)
 attributedText.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
 myLabel.attributedText = attributedText

For Swift 4

对于 Swift 4

  let text = myLabel.text
  let textRange = NSRange(location: 0, length: (text?.count)!)
  let attributedText = NSMutableAttributedString(string: text!)
  attributedText.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
  myLabel.attributedText = attributedText

回答by smileBot

Leave it set to plain. Make all of your changes. Then change it to attributed.

让它设置为普通。进行所有更改。然后将其更改为归因。

回答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.

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

To do something like the following, I have posted an example in How to make an underlined text in UILabel?

要执行以下操作,我在如何在 UILabel 中制作带下划线的文本中发布了一个示例

enter image description here

在此处输入图片说明