Merge branch 'mbruce/bugfixes' into 'develop'

added edgeInsets

See merge request BPHV_MIPS/vds_ios!93
This commit is contained in:
Bruce, Matt R 2023-07-20 17:59:24 +00:00
commit 95b23c326d
3 changed files with 33 additions and 34 deletions

View File

@ -33,10 +33,12 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable {
open var textStyle: TextStyle = .defaultStyle { didSet { setNeedsUpdate() }}
open var edgeInsets: UIEdgeInsets { textStyle.edgeInsets }
open var textPosition: TextPosition = .left { didSet { setNeedsUpdate() }}
open var userInfo = [String: Primitive]()
override open var text: String? {
didSet {
attributes = nil
@ -114,6 +116,13 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable {
shouldUpdateView = true
setNeedsUpdate()
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
open override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: edgeInsets))
}
//--------------------------------------------------
// MARK: - Overrides
@ -134,7 +143,12 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable {
//set the attributed text
attributedText = mutableText
//get accessibility
updateAccessibilityLabel()
//force a drawText
setNeedsDisplay()
}
}
}

View File

@ -20,40 +20,28 @@ extension NSAttributedString {
extension NSMutableAttributedString {
public static func mutableText(for text: String, textStyle: TextStyle, textColor: UIColor, alignment: NSTextAlignment = .left, lineBreakMode: NSLineBreakMode) -> NSMutableAttributedString {
let font = textStyle.font
let startingAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: textColor]
let attributedString = NSMutableAttributedString(string: text, attributes: startingAttributes)
//get the range
let entireRange = NSRange(location: 0, length: attributedString.length)
//set letterSpacing
if textStyle.letterSpacing > 0.0 {
attributedString.addAttribute(.kern, value: textStyle.letterSpacing, range: entireRange)
}
//create the paragraph for specific properties
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment
paragraph.lineBreakMode = lineBreakMode
if textStyle.lineSpacing > 0 {
paragraph.lineSpacing = textStyle.lineSpacing
}
//set lineHeight
if textStyle.lineHeight > 0.0 {
let lineHeight = textStyle.lineHeight
// if textStyle.lineHeight > textStyle.pointSize {
paragraph.maximumLineHeight = lineHeight
paragraph.minimumLineHeight = lineHeight
// paragraph.lineHeightMultiple = lineHeight / textStyle.pointSize
// } else {
// paragraph.lineHeightMultiple = lineHeight / font.pointSize
// }
paragraph.maximumLineHeight = textStyle.lineHeight
}
attributedString.addAttribute( .baselineOffset, value: textStyle.baselineOffset, range: entireRange)
attributedString.addAttribute( .paragraphStyle, value: paragraph, range: entireRange)
return attributedString
//create the attributeArray
var attributes: [NSAttributedString.Key : Any] = [.font: textStyle.font,
.foregroundColor: textColor,
.paragraphStyle: paragraph]
//set letterSpacing
if textStyle.letterSpacing > 0.0 {
attributes[.kern] = textStyle.letterSpacing
}
return NSMutableAttributedString(string: text, attributes: attributes)
}
}

View File

@ -29,8 +29,7 @@ public struct TextStyle: Equatable, RawRepresentable {
public let lineHeight: CGFloat
public let letterSpacing: CGFloat
public let fontFace: Font
public let lineSpacing: CGFloat
public let baselineOffset: CGFloat
public let edgeInsets: UIEdgeInsets
public init?(rawValue: String) {
guard let style = TextStyle.textStyle(for: rawValue) else { return nil }
@ -39,18 +38,16 @@ public struct TextStyle: Equatable, RawRepresentable {
self.lineHeight = style.lineHeight
self.letterSpacing = style.letterSpacing
self.fontFace = style.fontFace
self.lineSpacing = style.lineSpacing
self.baselineOffset = style.baselineOffset
self.edgeInsets = style.edgeInsets
}
public init(rawValue: String, fontFace: Font, pointSize: CGFloat = 0.0, lineHeight: CGFloat = 0.0, letterSpacing: CGFloat = 0.0, lineSpacing: CGFloat = 0.0, baselineOffset: CGFloat = 0.0) {
public init(rawValue: String, fontFace: Font, pointSize: CGFloat = 0.0, lineHeight: CGFloat = 0.0, letterSpacing: CGFloat = 0.0, edgeInsets: UIEdgeInsets = .zero) {
self.rawValue = rawValue
self.fontFace = fontFace
self.pointSize = pointSize
self.lineHeight = lineHeight
self.letterSpacing = letterSpacing
self.lineSpacing = lineSpacing
self.baselineOffset = baselineOffset
self.edgeInsets = edgeInsets
}
public var isBold: Bool {