Skip to content

Commit

Permalink
Merge pull request #75 from surfstudio/IDPT-715-add-baselineoffset
Browse files Browse the repository at this point in the history
IDPT-715 Add baseLine offset and add method .add(attribute)
  • Loading branch information
gregoryvit authored Apr 12, 2021
2 parents 9ae32d5 + aafea02 commit d209af0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Utils/Utils/String/String+Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public enum StringAttribute {
case crossOut(style: CrossOutStyle)
/// Text line break mode
case lineBreakMode(NSLineBreakMode)
/// Text base line offset (vertical)
case baselineOffset(CGFloat)

/// Figma friendly case means that lineSpacing = lineHeight - font.lineHeight
/// This case provide possibility to set both `font` and `lineSpacing`
Expand All @@ -49,6 +51,17 @@ extension StringAttribute {
return NSUnderlineStyle.single
}
}

init?(with style: NSUnderlineStyle) {
switch style {
case .double:
self = .double
case .single:
self = .single
default:
return nil
}
}
}
}

Expand All @@ -67,6 +80,8 @@ extension StringAttribute {
return NSAttributedString.Key.foregroundColor
case .crossOut:
return NSAttributedString.Key.strikethroughStyle
case .baselineOffset:
return NSAttributedString.Key.baselineOffset
}
}

Expand All @@ -88,7 +103,54 @@ extension StringAttribute {
return style.coreValue.rawValue
case .lineBreakMode(let value):
return value
case .baselineOffset(let value):
return value
}
}
}

// MARK: - Public static methods

public extension StringAttribute {

// init from attributs array
static func from(dictionary: [NSAttributedString.Key: Any]) -> [StringAttribute] {
var stringAttributedArray = [StringAttribute]()

if let baselineOffset = dictionary[.baselineOffset] as? CGFloat {
stringAttributedArray.append(.baselineOffset(baselineOffset))
}

if let strikethroughStyle = dictionary[.strikethroughStyle] as? NSUnderlineStyle,
let crossOutStyle = CrossOutStyle(with: strikethroughStyle) {
stringAttributedArray.append(.crossOut(style: crossOutStyle))
}

if let foregroundColor = dictionary[.foregroundColor] as? UIColor {
stringAttributedArray.append(.foregroundColor(foregroundColor))
}

if let font = dictionary[.font] as? UIFont {
stringAttributedArray.append(.font(font))
}

if let kernValue = dictionary[.kern] as? CGFloat {
stringAttributedArray.append(.kern(kernValue))
}

if let value = dictionary[.paragraphStyle],
let mutableParagraphStyle = value as? NSMutableParagraphStyle {
stringAttributedArray.append(.aligment(mutableParagraphStyle.alignment))
stringAttributedArray.append(.lineBreakMode(mutableParagraphStyle.lineBreakMode))

if let font = dictionary[.font] as? UIFont {
stringAttributedArray.append(.lineHeight(mutableParagraphStyle.lineSpacing, font: font))
} else {
stringAttributedArray.append(.lineSpacing(mutableParagraphStyle.lineSpacing))
}
}

return stringAttributedArray
}
}

Expand Down
10 changes: 10 additions & 0 deletions Utils/Utils/String/StringBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ public class StringBuilder {
return self
}

/// Method for adding global attributes with NSAttributedString
/// - Parameter globalAttributes: attributes to apply
@discardableResult
public func add(with nsAttribute: NSAttributedString) -> StringBuilder {
let attributes = nsAttribute.attributes(at: 0, effectiveRange: nil)
let stringAttributes = StringAttribute.from(dictionary: attributes)
self.add(.string(nsAttribute.string), with: stringAttributes)
return self
}

/// Method for adding text block to attributed
/// - Parameters:
/// - block: block to add
Expand Down

0 comments on commit d209af0

Please sign in to comment.