Bagaimana cara menggarisbawahi a UILabel
di Swift? Saya mencari yang Objective-C tetapi tidak bisa membuatnya bekerja di Swift.
ios
swift
overriding
uilabel
nsattributedstring
Esqarrouth
sumber
sumber
NSAttributedString
?Jawaban:
Anda dapat melakukan ini menggunakan NSAttributedString
Contoh:
let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue] let underlineAttributedString = NSAttributedString(string: "StringWithUnderLine", attributes: underlineAttribute) myLabel.attributedText = underlineAttributedString
EDIT
Untuk memiliki atribut yang sama untuk semua teks dari satu UILabel, saya sarankan Anda untuk membuat subclass UILabel dan mengganti teks, seperti itu:
Swift 4.2.0
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.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 } } }
Dan Anda meletakkan teks Anda seperti ini:
@IBOutlet weak var label: UnderlinedLabel! override func viewDidLoad() { super.viewDidLoad() label.text = "StringWithUnderLine" }
TUA:
Swift (2.0 hingga 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:
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 } } }
sumber
UTF16
hitungan alih-alih jumlah karakter saat membuat textRange AndaNSRange
required init?(coder: NSCoder) { super.init(coder: coder) self.addUnderline() // your code }
Swift 5 & 4.2 satu liner:
label.attributedText = NSAttributedString(string: "Text", attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue])
Swift 4 satu liner:
label.attributedText = NSAttributedString(string: "Text", attributes: [.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])
Swift 3 satu liner:
label.attributedText = NSAttributedString(string: "Text", attributes: [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])
sumber
Jika Anda mencari cara untuk melakukan ini tanpa warisan:
Cepat 5
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: attributedString.length)) attributedText = attributedString } } }
Cepat 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)) 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) } }
sumber
UTF16
hitungan alih-alih jumlah karakter saat membuatNSRange
Cepat 5:
1- Buat ekstensi String untuk mendapatkan atributedText
extension String { var underLined: NSAttributedString { NSMutableAttributedString(string: self, attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue]) } }
2- Gunakan itu
Pada tombol:
button.setAttributedTitle(yourButtonTitle.underLined, for: .normal)
Pada Label:
Atau versi Stoyboard
sumber
Hanya sedikit perbaikan untuk jawaban Shlome di Swift 4 dan Xcode 9 .
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) } }
sumber
UTF16
hitungan alih-alih jumlah karakter saat membuatNSRange
Anda dapat menggarisbawahi
UILabel
teks menggunakan Interface Builder.Ini link dari jawaban saya: Menambahkan atribut garis bawah ke teks parsial UILabel di storyboard
sumber
Jawaban yang Sama di Swift 4.2
Untuk 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 } } }
Hubungi UILabel seperti di bawah ini
Untuk 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) } } }
Hubungi UIButton seperti di bawah ini
Saya melihat jawaban di atas dan beberapa di antaranya adalah nilai teks yang tidak dibuka secara paksa . Saya akan menyarankan untuk mendapatkan nilai dengan membuka bungkusnya dengan aman. Ini akan menghindari crash jika nilai nihil. Semoga ini membantu :)
sumber
UTF16
hitungan alih-alih jumlah karakter saat membuatNSRange
Swift 4, 4.2 dan 5.
@IBOutlet weak var lblUnderLine: UILabel!
Saya perlu menggarisbawahi teks tertentu dalam UILabel. Jadi, temukan range dan atur atribut.
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
Keluaran
sumber
Garis bawahi ke beberapa string dalam satu kalimat.
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 } } }
Gunakan dengan cara ini.
lbl.text = "By continuing you agree to our Terms of Service and Privacy Policy." lbl.underlineMyText(range1: "Terms of Service", range2: "Privacy Policy.")
sumber
Swift 4 perubahan. Ingat untuk menggunakan NSUnderlineStyle.styleSingle.rawValue alih-alih 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
`
sumber
Anda juga dapat menggunakan ini jika Anda ingin mencapai setengah dari label seperti yang digarisbawahi: - // Untuk Swift 4.0+
let attributesForUnderLine: [NSAttributedString.Key: Any] = [ .font: UIFont(name: AppFont.sourceSansPro_Regular, size: 12) ?? UIFont.systemFont(ofSize: 11), .foregroundColor: UIColor.blue, .underlineStyle: NSUnderlineStyle.single.rawValue] let attributesForNormalText: [NSAttributedString.Key: Any] = [ .font: UIFont(name: AppFont.sourceSansPro_Regular, size: 12) ?? UIFont.systemFont(ofSize: 11), .foregroundColor: AppColors.ColorText_787878] let textToSet = "Want to change your preferences? Edit Now" let rangeOfUnderLine = (textToSet as NSString).range(of: "Edit Now") let rangeOfNormalText = (textToSet as NSString).range(of: "Want to change your preferences?") let attributedText = NSMutableAttributedString(string: textToSet) attributedText.addAttributes(attributesForUnderLine, range: rangeOfUnderLine) attributedText.addAttributes(attributesForNormalText, range: rangeOfNormalText) yourLabel.attributedText = attributedText
sumber
Jawaban di atas menyebabkan kesalahan dalam lingkungan build saya.
Ini tidak berfungsi di Swift 4.0:
attributedText.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
Coba ini sebagai gantinya:
attributedText.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
semoga ini membantu seseorang.
sumber
// Versi Swift 4
let attributedString = NSMutableAttributedString(string: "Your Text Here", attributes: [NSAttributedStringKey.underlineStyle : true]) self.yourlabel.attributedText = attributedString
sumber
Untuk Swift 2.3
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, forState: .Normal) } }
dan di ViewController
@IBOutlet var yourButton: UIButton!
di
ViewDidLoad
Metode atau di fungsi Anda, tulis sajaitu akan menggarisbawahi judul tombol Anda
sumber
Kelas untuk menyetel dan menghapus garis bawah untuk UIbuttons untuk Swift 5. Saya harap ini membantu
import Foundation import UIKit class UiUtil { static let underlineThickness = 2 class func removeUnderlineFromButton( _ button:UIButton ) { if let str = button.titleLabel?.attributedText { let attributedString = NSMutableAttributedString( attributedString: str ) attributedString.removeAttribute(.underlineStyle, range: NSRange.init(location: 0, length: attributedString.length)) button.setAttributedTitle(attributedString, for: .normal) } } class func setUnderlineFromButton( _ button:UIButton ) { if let str = button.titleLabel?.attributedText { let attributedStringUnderline = NSMutableAttributedString( attributedString: str ) attributedStringUnderline.addAttribute( NSAttributedString.Key.underlineStyle, value: underlineThickness, range: NSRange.init(location: 0, length: attributedStringUnderline.length) ) button.setAttributedTitle(attributedStringUnderline, for: .normal) } } }
sumber