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

View File

@ -20,40 +20,28 @@ extension NSAttributedString {
extension NSMutableAttributedString { extension NSMutableAttributedString {
public static func mutableText(for text: String, textStyle: TextStyle, textColor: UIColor, alignment: NSTextAlignment = .left, lineBreakMode: NSLineBreakMode) -> 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 //create the paragraph for specific properties
let entireRange = NSRange(location: 0, length: attributedString.length)
//set letterSpacing
if textStyle.letterSpacing > 0.0 {
attributedString.addAttribute(.kern, value: textStyle.letterSpacing, range: entireRange)
}
let paragraph = NSMutableParagraphStyle() let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment paragraph.alignment = alignment
paragraph.lineBreakMode = lineBreakMode paragraph.lineBreakMode = lineBreakMode
if textStyle.lineSpacing > 0 {
paragraph.lineSpacing = textStyle.lineSpacing
}
//set lineHeight //set lineHeight
if textStyle.lineHeight > 0.0 { if textStyle.lineHeight > 0.0 {
let lineHeight = textStyle.lineHeight paragraph.maximumLineHeight = textStyle.lineHeight
// if textStyle.lineHeight > textStyle.pointSize {
paragraph.maximumLineHeight = lineHeight
paragraph.minimumLineHeight = lineHeight
// paragraph.lineHeightMultiple = lineHeight / textStyle.pointSize
// } else {
// paragraph.lineHeightMultiple = lineHeight / font.pointSize
// }
} }
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 lineHeight: CGFloat
public let letterSpacing: CGFloat public let letterSpacing: CGFloat
public let fontFace: Font public let fontFace: Font
public let lineSpacing: CGFloat public let edgeInsets: UIEdgeInsets
public let baselineOffset: CGFloat
public init?(rawValue: String) { public init?(rawValue: String) {
guard let style = TextStyle.textStyle(for: rawValue) else { return nil } guard let style = TextStyle.textStyle(for: rawValue) else { return nil }
@ -39,18 +38,16 @@ public struct TextStyle: Equatable, RawRepresentable {
self.lineHeight = style.lineHeight self.lineHeight = style.lineHeight
self.letterSpacing = style.letterSpacing self.letterSpacing = style.letterSpacing
self.fontFace = style.fontFace self.fontFace = style.fontFace
self.lineSpacing = style.lineSpacing self.edgeInsets = style.edgeInsets
self.baselineOffset = style.baselineOffset
} }
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.rawValue = rawValue
self.fontFace = fontFace self.fontFace = fontFace
self.pointSize = pointSize self.pointSize = pointSize
self.lineHeight = lineHeight self.lineHeight = lineHeight
self.letterSpacing = letterSpacing self.letterSpacing = letterSpacing
self.lineSpacing = lineSpacing self.edgeInsets = edgeInsets
self.baselineOffset = baselineOffset
} }
public var isBold: Bool { public var isBold: Bool {