updated FontLabelAttribute for Kerning/line spacing/ line height
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
64d9ca3777
commit
e46017f315
@ -24,21 +24,59 @@ public struct FontLabelAttribute: LabelAttributeModel {
|
|||||||
public var length: Int
|
public var length: Int
|
||||||
public var style: TypographicalStyle
|
public var style: TypographicalStyle
|
||||||
public var color: UIColor
|
public var color: UIColor
|
||||||
|
public var textPosition: TextPosition
|
||||||
|
public var lineBreakMode: NSLineBreakMode
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Initializer
|
// MARK: - Initializer
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
public init(location: Int, length: Int, style: TypographicalStyle, color: UIColor = .black) {
|
public init(location: Int, length: Int, style: TypographicalStyle, color: UIColor = .black, textPosition: TextPosition = .left, lineBreakMode: NSLineBreakMode = .byWordWrapping) {
|
||||||
self.location = location
|
self.location = location
|
||||||
self.length = length
|
self.length = length
|
||||||
self.style = style
|
self.style = style
|
||||||
self.color = color
|
self.color = color
|
||||||
|
self.textPosition = textPosition
|
||||||
|
self.lineBreakMode = lineBreakMode
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setAttribute(on attributedString: NSMutableAttributedString) {
|
public func setAttribute(on attributedString: NSMutableAttributedString) {
|
||||||
|
|
||||||
attributedString.removeAttribute(.font, range: range)
|
attributedString.removeAttribute(.font, range: range)
|
||||||
attributedString.removeAttribute(.foregroundColor, range: range)
|
attributedString.removeAttribute(.foregroundColor, range: range)
|
||||||
attributedString.addAttribute(.font, value: style.font, range: range)
|
attributedString.addAttribute(.font, value: style.font, range: range)
|
||||||
attributedString.addAttribute(.foregroundColor, value: color, range: range)
|
attributedString.addAttribute(.foregroundColor, value: color, range: range)
|
||||||
|
setStyleAttributes(attributedString)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func setStyleAttributes(_ attributedString: NSMutableAttributedString) {
|
||||||
|
//set letterSpacing
|
||||||
|
if style.letterSpacing > 0.0 {
|
||||||
|
attributedString.removeAttribute(.kern, range: range)
|
||||||
|
attributedString.addAttribute(.kern, value: style.letterSpacing, range: range)
|
||||||
|
}
|
||||||
|
|
||||||
|
//set lineHeight
|
||||||
|
if style.lineHeight > 0.0 {
|
||||||
|
let lineHeight = style.lineHeight
|
||||||
|
let adjustment = lineHeight > style.font.lineHeight ? 2.0 : 1.0
|
||||||
|
let baselineOffset = (lineHeight - style.font.lineHeight) / 2.0 / adjustment
|
||||||
|
let paragraph = NSMutableParagraphStyle().with {
|
||||||
|
$0.maximumLineHeight = lineHeight
|
||||||
|
$0.minimumLineHeight = lineHeight
|
||||||
|
$0.alignment = textPosition.textAlignment
|
||||||
|
$0.lineBreakMode = lineBreakMode
|
||||||
|
}
|
||||||
|
attributedString.removeAttribute(.baselineOffset, range: range)
|
||||||
|
attributedString.removeAttribute(.paragraphStyle, range: range)
|
||||||
|
attributedString.addAttribute(.baselineOffset, value: baselineOffset, range: range)
|
||||||
|
attributedString.addAttribute(.paragraphStyle, value: paragraph, range: range)
|
||||||
|
|
||||||
|
} else if textPosition != .left {
|
||||||
|
let paragraph = NSMutableParagraphStyle().with {
|
||||||
|
$0.alignment = textPosition.textAlignment
|
||||||
|
$0.lineBreakMode = lineBreakMode
|
||||||
|
}
|
||||||
|
attributedString.removeAttribute(.paragraphStyle, range: range)
|
||||||
|
attributedString.addAttribute(.paragraphStyle, value: paragraph, range: range)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user