ios 如何快速在 UILabel 下划线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28053334/
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
How to underline a UILabel in swift?
提问by Esqarrouth
How to underline a UILabel
in Swift? I searched the Objective-C ones but couldn't quite get them to work in Swift.
如何UILabel
在 Swift 中给 a 下划线?我搜索了 Objective-C 的那些,但不能完全让它们在 Swift 中工作。
回答by Hamza
You can do this using NSAttributedString
您可以使用NSAttributedString执行此操作
Example:
例子:
let underlineAttribute = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
let underlineAttributedString = NSAttributedString(string: "StringWithUnderLine", attributes: underlineAttribute)
myLabel.attributedText = underlineAttributedString
EDIT
编辑
To have the same attributes for all texts of one UILabel, I suggest you to subclass UILabel and overriding text, like that:
要使一个 UILabel 的所有文本具有相同的属性,我建议您将 UILabel 子类化并覆盖文本,如下所示:
Swift 4.2
斯威夫特 4.2
class UnderlinedLabel: UILabel {
override var text: String? {
didSet {
guard let text = text else { return }
let textRange = NSMakeRange(0, text.count)
let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(NSAttributedString.Key.underlineStyle , value: NSUnderlineStyle.single.rawValue, range: textRange)
// Add other attributes if needed
self.attributedText = attributedText
}
}
}
Swift 3.0
斯威夫特 3.0
class UnderlinedLabel: UILabel {
override var text: String? {
didSet {
guard let text = text else { return }
let textRange = NSMakeRange(0, text.characters.count)
let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
// Add other attributes if needed
self.attributedText = attributedText
}
}
}
And you put your text like this :
你把你的文字是这样的:
@IBOutlet weak var label: UnderlinedLabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = "StringWithUnderLine"
}
OLD:
老的:
Swift (2.0 to 2.3):
斯威夫特(2.0 到 2.3):
class UnderlinedLabel: UILabel {
override var text: String? {
didSet {
guard let text = text else { return }
let textRange = NSMakeRange(0, text.characters.count)
let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
// Add other attributes if needed
self.attributedText = attributedText
}
}
}
Swift 1.2:
斯威夫特 1.2:
class UnderlinedLabel: UILabel {
override var text: String! {
didSet {
let textRange = NSMakeRange(0, count(text))
let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
// Add other attributes if needed
self.attributedText = attributedText
}
}
}
回答by hasan
Swift 5 & 4.2 one liner:
Swift 5 & 4.2 一个班轮:
label.attributedText = NSAttributedString(string: "Text", attributes:
[.underlineStyle: NSUnderlineStyle.single.rawValue])
Swift 4 one liner:
Swift 4 一个班轮:
label.attributedText = NSAttributedString(string: "Text", attributes:
[.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
Swift 3 one liner:
Swift 3 一个班轮:
label.attributedText = NSAttributedString(string: "Text", attributes:
[NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])
回答by Shlomo Koppel
if you are looking for a way to do this without inheritance -
如果您正在寻找一种无需继承即可执行此操作的方法-
swift 3/4
快速 3/4
// in swift 4 - switch NSUnderlineStyleAttributeName with NSAttributedStringKey.underlineStyle
extension UILabel {
func underline() {
if let textString = self.text {
let attributedString = NSMutableAttributedString(string: textString)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: attributedString.length - 1))
attributedText = attributedString
}
}
}
extension UIButton {
func underline() {
let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!)
attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: (self.titleLabel?.text!.characters.count)!))
self.setAttributedTitle(attributedString, for: .normal)
}
}
回答by Elano Vasconcelos
Just a little fix for the Shlome answer in Swift 4and Xcode 9.
只是对Swift 4和Xcode 9 中的 Shlome 答案进行了一点修复。
extension UILabel {
func underline() {
if let textString = self.text {
let attributedString = NSMutableAttributedString(string: textString)
attributedString.addAttribute(NSAttributedStringKey.underlineStyle,
value: NSUnderlineStyle.styleSingle.rawValue,
range: NSRange(location: 0, length: attributedString.length - 1))
attributedText = attributedString
}
}
}
extension UIButton {
func underline() {
let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!)
attributedString.addAttribute(NSAttributedStringKey.underlineStyle,
value: NSUnderlineStyle.styleSingle.rawValue,
range: NSRange(location: 0, length: (self.titleLabel?.text!.count)!))
self.setAttributedTitle(attributedString, for: .normal)
}
}
回答by Rizwan Shaikh
You can underline the UILabel
text using Interface Builder.
您可以UILabel
使用 Interface Builder在文本下划线。
Here is the link of my answer : Adding underline attribute to partial text UILabel in storyboard
这是我的答案的链接:向情节提要中的部分文本 UILabel 添加下划线属性
回答by alegelos
Swift 4:
斯威夫特 4:
1- Createa String extension to get attributedText.
1-创建一个字符串扩展来获取attributedText。
2- Useit
2-使用它
Extension:
延期:
import UIKit
extension String {
func getUnderLineAttributedText() -> NSAttributedString {
return NSMutableAttributedString(string: self, attributes: [.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
}
}
How to use it on buttton:
如何在按钮上使用它:
if let title = button.titleLabel?.text{
button.setAttributedTitle(title.getUnderLineAttributedText(), for: .normal)
}
How to use it on Labels:
如何在标签上使用它:
if let title = label.text{
label.attributedText = title.getUnderLineAttributedText()
}
回答by AbdulRehman Warraich
Same Answer in Swift 4.2
Swift 4.2 中的相同答案
For UILable
对于UILable
extension UILabel {
func underline() {
if let textString = self.text {
let attributedString = NSMutableAttributedString(string: textString)
attributedString.addAttribute(NSAttributedString.Key.underlineStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSRange(location: 0, length: textString.count))
self.attributedText = attributedString
}
}
}
Call for UILabellike below
像下面这样调用UILabel
myLable.underline()
For UIButton
对于UIButton
extension UIButton {
func underline() {
if let textString = self.titleLabel?.text {
let attributedString = NSMutableAttributedString(string: textString)
attributedString.addAttribute(NSAttributedString.Key.underlineStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSRange(location: 0, length: textString.count))
self.setAttributedTitle(attributedString, for: .normal)
}
}
}
Call for UIButtonlike below
像下面这样调用UIButton
myButton.underline()
I looked into above answers and some of them are force unwrapping textvalue. I will suggest to get value by safely unwrapping. This will avoid crash in case of nil value. Hope This helps :)
我查看了上面的答案,其中一些是强制展开文本值。我会建议通过安全拆包来获得价值。这将避免在 nil 值的情况下崩溃。希望这可以帮助 :)
回答by Gurjinder Singh
Swift 4, 4.2 and 5.
斯威夫特 4、4.2 和 5。
@IBOutlet weak var lblUnderLine: UILabel!
I need to underline particular text in UILabel. So, find range and set attributes.
我需要为 UILabel 中的特定文本加下划线。因此,找到范围并设置属性。
let strSignup = "Don't have account? SIGNUP NOW."
let rangeSignUp = NSString(string: strSignup).range(of: "SIGNUP NOW.", options: String.CompareOptions.caseInsensitive)
let rangeFull = NSString(string: strSignup).range(of: strSignup, options: String.CompareOptions.caseInsensitive)
let attrStr = NSMutableAttributedString.init(string:strSignup)
attrStr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.font : UIFont.init(name: "Helvetica", size: 17)! as Any],range: rangeFull)
attrStr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.white,
NSAttributedString.Key.font : UIFont.init(name: "Helvetica", size: 20)!,
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue as Any],range: rangeSignUp) // for swift 4 -> Change thick to styleThick
lblUnderLine.attributedText = attrStr
Output
输出
回答by kalpesh
Underline to multiple strings in a sentence.
给句子中的多个字符串加下划线。
extension UILabel {
func underlineMyText(range1:String, range2:String) {
if let textString = self.text {
let str = NSString(string: textString)
let firstRange = str.range(of: range1)
let secRange = str.range(of: range2)
let attributedString = NSMutableAttributedString(string: textString)
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: firstRange)
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: secRange)
attributedText = attributedString
}
}
}
Use by this way.
通过这种方式使用。
lbl.text = "By continuing you agree to our Terms of Service and Privacy Policy."
lbl.underlineMyText(range1: "Terms of Service", range2: "Privacy Policy.")
回答by venky
Swift 4changes. Remeber to use NSUnderlineStyle.styleSingle.rawValueinstead of NSUnderlineStyle.styleSingle.
斯威夫特 4变化。记住使用NSUnderlineStyle.styleSingle.rawValue而不是NSUnderlineStyle.styleSingle。
'let attributedString = NSAttributedString(string: "Testing")
let textRange = NSMakeRange(0, attributedString.length)
let underlinedMessage = NSMutableAttributedString(attributedString: attributedString)
underlinedMessage.addAttribute(NSAttributedStringKey.underlineStyle,
value:NSUnderlineStyle.styleSingle.rawValue,
range: textRange)
label.attributedText = underlinedMessage
`
`