Reputation: 205
How do i change the color of my underline in a label? I want only the underline to change color, and not the entire text.
I have used this code to get the underline:
let underlineAttribute = [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue]
let underlineAttributedString = NSAttributedString(string: "\(nearSavings[indexPath.row]) ,-", attributes: underlineAttribute)
cell.detailTextLabel?.attributedText = underlineAttributedString
But i cant find the code, to set the underline color. Anyone who can help?
Upvotes: 5
Views: 6830
Reputation: 804
Swift 5 Solution
class UnderlinedLabel: UILabel {
override var text: String? {
didSet {
guard let text = text else { return }
let textColor: UIColor = .black
let underLineColor: UIColor = .lightGray
let underLineStyle = NSUnderlineStyle.single.rawValue
let labelAtributes:[NSAttributedString.Key : Any] = [
NSAttributedString.Key.foregroundColor: textColor,
NSAttributedString.Key.underlineStyle: underLineStyle,
NSAttributedString.Key.underlineColor: underLineColor
]
self.attributedText = NSAttributedString(string: text, attributes: labelAtributes)
}
}
}
Usage: Just give class UnderlinedLabel to your label in the storyboard
Upvotes: 2
Reputation: 2131
Another solution could be to add a single line border under the label, that acts as an underline.
Get reference to the label
@IBOutlet weak var myLabel: UILabel!
Add the border under the label
let labelSize = myLabel.frame.size
let border = CALayer()
let w = CGFloat(2.0)
border.borderColor = UIColor.yellow.cgColor // <--- Here the underline color
border.frame = CGRect(x: 0, y: labelSize.height - w, width: labelSize.width, height: labelSize.height)
border.borderWidth = w
myLabel.layer.addSublayer(border)
myLabel.layer.masksToBounds = true
Note: with this workaround you underline the whole label. If you need to partially undlerline the text this solution is not appropiate
Upvotes: 2
Reputation: 2468
You must use NSAttributedString with an array of attributes as [NSAttributedStringKey : Any].
Sample code:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Colored Underline Label
let labelString = "Underline Label"
let textColor: UIColor = .blue
let underLineColor: UIColor = .red
let underLineStyle = NSUnderlineStyle.styleSingle.rawValue
let labelAtributes:[NSAttributedStringKey : Any] = [
NSAttributedStringKey.foregroundColor: textColor,
NSAttributedStringKey.underlineStyle: underLineStyle,
NSAttributedStringKey.underlineColor: underLineColor
]
let underlineAttributedString = NSAttributedString(string: labelString,
attributes: labelAtributes)
myLabel.attributedText = underlineAttributedString
}
}
Upvotes: 11
Reputation: 607
The NSAttributedStringKey.underlineColor
attribute does what you want:
let underlineAttributes = [
NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue,
NSAttributedStringKey.underlineColor: UIColor.orange
] as [NSAttributedStringKey : Any]
let underlineAttributedString = NSAttributedString(string: "Test", attributes: underlineAttributes)
This will set the underline color to orange whereas the text color will remain black.
Upvotes: 1