Skip to content

Commit

Permalink
Merge pull request #49 from alexfilimon/master
Browse files Browse the repository at this point in the history
Добавил crossOut в StringAttribute
  • Loading branch information
LastSprint authored Nov 26, 2019
2 parents a2db0c0 + 220dfa2 commit 53676a3
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion Utils/Utils/String/String+Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,38 @@ public enum StringAttribute {
case foregroundColor(UIColor)
/// Text aligment
case aligment(NSTextAlignment)
/// Text crossing out
case crossOut(style: CrossOutStyle)

/// Figma friendly case means that lineSpacing = lineHeight - font.lineHeight
/// This case provide possibility to set both `font` and `lineSpacing`
/// First parameter is Font and second parameter is lineHeight property from Figma
/// For more details see [#14](https://github.com/surfstudio/iOS-Utils/issues/14)
case lineHeight(CGFloat, font: UIFont)
}

// MARK: - Nested types

extension StringAttribute{
/// Enum for configuring style of crossOut text
public enum CrossOutStyle {
case single
case double

var coreValue: NSUnderlineStyle {
switch self {
case .double:
return NSUnderlineStyle.double
case .single:
return NSUnderlineStyle.single
}
}
}
}

// MARK: - StringAttribute extension

extension StringAttribute {
var attributeKey: NSAttributedString.Key {
switch self {
case .lineSpacing, .aligment, .lineHeight:
Expand All @@ -38,6 +63,8 @@ public enum StringAttribute {
return NSAttributedString.Key.font
case .foregroundColor:
return NSAttributedString.Key.foregroundColor
case .crossOut:
return NSAttributedString.Key.strikethroughStyle
}
}

Expand All @@ -55,18 +82,23 @@ public enum StringAttribute {
return value
case .lineHeight(let lineHeight, let font):
return lineHeight - font.lineHeight
case .crossOut(let style):
return style.coreValue.rawValue
}
}
}

public extension String {
// MARK: - String extension

public extension String {
/// Apply attributes to string and returns new attributes string
func with(attributes: [StringAttribute]) -> NSAttributedString {
return NSAttributedString(string: self, attributes: attributes.toDictionary())
}
}

// MARK: - Private array extension

private extension Array where Element == StringAttribute {
func normalizedAttributes() -> [StringAttribute] {
var result = [StringAttribute](self)
Expand All @@ -83,6 +115,8 @@ private extension Array where Element == StringAttribute {
}
}

// MARK: - Public array extension

public extension Array where Element == StringAttribute {
func toDictionary() -> [NSAttributedString.Key: Any] {
var resultAttributes = [NSAttributedString.Key: Any]()
Expand Down

0 comments on commit 53676a3

Please sign in to comment.