updated labels to ensure string is valid

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-02-28 13:04:11 -06:00
parent de973fd7b8
commit 98b30237b2
6 changed files with 11 additions and 1 deletions

View File

@ -30,6 +30,7 @@ public struct AnyAttribute: LabelAttributeModel {
}
public func setAttribute(on attributedString: NSMutableAttributedString) {
guard isValidRange(on: attributedString) else { return }
attributedString.removeAttribute(key, range: range)
attributedString.addAttribute(key, value: value, range: range)
}

View File

@ -31,6 +31,8 @@ public struct ColorLabelAttribute: LabelAttributeModel {
}
public func setAttribute(on attributedString: NSMutableAttributedString) {
guard isValidRange(on: attributedString) else { return }
var colorRange = range
if length == 0 && location == 0 {
colorRange = .init(location: location, length: attributedString.length)

View File

@ -29,6 +29,10 @@ extension LabelAttributeModel {
public static func == (lhs: any LabelAttributeModel, rhs: any LabelAttributeModel) -> Bool {
lhs.isEqual(rhs)
}
public func isValidRange(on attributedString: NSMutableAttributedString) -> Bool {
true//range.location + range.length <= attributedString.string.count
}
}
public extension NSAttributedString {

View File

@ -24,6 +24,7 @@ public struct StrikeThroughLabelAttribute: LabelAttributeModel {
}
public func setAttribute(on attributedString: NSMutableAttributedString) {
guard isValidRange(on: attributedString) else { return }
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick.rawValue, range: range)
attributedString.addAttribute(.baselineOffset, value: 0, range: range)
}

View File

@ -44,6 +44,7 @@ public struct TextStyleLabelAttribute: LabelAttributeModel {
}
public func setAttribute(on attributedString: NSMutableAttributedString) {
guard isValidRange(on: attributedString) else { return }
attributedString.removeAttribute(.font, range: range)
attributedString.addAttribute(.font, value: textStyle.font, range: range)
if let textColor {

View File

@ -52,7 +52,8 @@ public struct UnderlineLabelAttribute: LabelAttributeModel {
//--------------------------------------------------
// MARK: - Public Methods
//--------------------------------------------------
public func setAttribute(on attributedString: NSMutableAttributedString) {
public func setAttribute(on attributedString: NSMutableAttributedString) {
guard isValidRange(on: attributedString) else { return }
attributedString.addAttribute(.underlineStyle, value: underlineValue.rawValue, range: range)
if let color = color {
attributedString.addAttribute(.underlineColor, value: color, range: range)