updated color range to set the length if current range is 0.0

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-11-07 08:19:55 -06:00
parent e0540a68ce
commit 8b60364d63

View File

@ -23,7 +23,7 @@ public struct ColorLabelAttribute: LabelAttributeModel {
//--------------------------------------------------
// MARK: - Initializer
//--------------------------------------------------
public init(location: Int, length: Int, color: UIColor = .black, isForegroundColor: Bool = true) {
public init(location: Int = 0, length: Int = 0, color: UIColor = .black, isForegroundColor: Bool = true) {
self.location = location
self.length = length
self.color = color
@ -31,8 +31,12 @@ public struct ColorLabelAttribute: LabelAttributeModel {
}
public func setAttribute(on attributedString: NSMutableAttributedString) {
var colorRange = range
if length == 0 && location == 0 {
colorRange = .init(location: location, length: attributedString.length)
}
let attributeKey = isForegroundColor ? NSAttributedString.Key.foregroundColor : NSAttributedString.Key.backgroundColor
attributedString.removeAttribute(attributeKey, range: range)
attributedString.addAttribute(attributeKey, value: color, range: range)
attributedString.removeAttribute(attributeKey, range: colorRange)
attributedString.addAttribute(attributeKey, value: color, range: colorRange)
}
}