diff --git a/TIUIKitCore/Sources/ViewText/BaseTextAttributes.swift b/TIUIKitCore/Sources/ViewText/BaseTextAttributes.swift index e51c6abc..d9d5a26e 100644 --- a/TIUIKitCore/Sources/ViewText/BaseTextAttributes.swift +++ b/TIUIKitCore/Sources/ViewText/BaseTextAttributes.swift @@ -45,3 +45,13 @@ open class BaseTextAttributes { self.alignment = alignment } } + +public extension BaseTextAttributes { + + /// Configures text appearance of given ViewTextConfigurable instance. + /// + /// - Parameter view: ViewTextConfigurable instance to configure with BaseTextAttributes. + func configureBaseApperance(of view: ViewTextConfigurable) { + view.configureBaseAppearance(with: self) + } +} diff --git a/TIUIKitCore/Sources/ViewText/ViewText.swift b/TIUIKitCore/Sources/ViewText/ViewText.swift index da7f0822..b84ea370 100644 --- a/TIUIKitCore/Sources/ViewText/ViewText.swift +++ b/TIUIKitCore/Sources/ViewText/ViewText.swift @@ -20,7 +20,7 @@ // THE SOFTWARE. // -import Foundation.NSAttributedString +import UIKit /// Enum that describes text with appearance options. /// @@ -31,3 +31,61 @@ public enum ViewText { case string(String, textAttributes: BaseTextAttributes) case attributedString(NSAttributedString) } + +public extension ViewText { + + /// Convenient initializer for .string case with default alignment parameter. + /// + /// - Parameters: + /// - string: Text to use. + /// - font: Font to use. + /// - color: Color to use. + /// - alignment: Alignment to use. Default is natural. + init(string: String, font: UIFont, color: UIColor, alignment: NSTextAlignment = .natural) { + self = .string(string, textAttributes: BaseTextAttributes(font: font, + color: color, + alignment: alignment)) + } + + /// Attributed string created using text attributes. + var attributedString: NSAttributedString { + switch self { + case let .string(title, textAttributes): + + let paragraphStyle = NSMutableParagraphStyle() + paragraphStyle.alignment = textAttributes.alignment + + let attributes: [NSAttributedString.Key: Any] = [ + .font: textAttributes.font, + .foregroundColor: textAttributes.color, + .paragraphStyle: paragraphStyle + ] + + return NSAttributedString(string: title, attributes: attributes) + + case .attributedString(let attributedTitle): + return attributedTitle + } + } + + /// Method that calculates size of view text using given max width and height arguments. + /// + /// - Parameters: + /// - maxWidth: The width constraint to apply when computing the string’s bounding rectangle. + /// - maxHeight: The width constraint to apply when computing the string’s bounding rectangle. + /// - Returns: Returns the size required to draw the text. + func size(maxWidth: CGFloat = CGFloat.greatestFiniteMagnitude, + maxHeight: CGFloat = CGFloat.greatestFiniteMagnitude) -> CGSize { + + return attributedString.boundingRect(with: CGSize(width: maxWidth, height: maxHeight), + options: [.usesLineFragmentOrigin, .usesFontLeading], + context: nil).size + } + + /// Configures given ViewTextConfigurable instance. + /// + /// - Parameter view: ViewTextConfigurable instance to configure with ViewText. + func configure(view: ViewTextConfigurable) { + view.configure(with: self) + } +} diff --git a/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/ViewTextConfigurable.swift b/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/ViewTextConfigurable.swift index ee2b5f9a..dfd7256c 100644 --- a/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/ViewTextConfigurable.swift +++ b/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/ViewTextConfigurable.swift @@ -41,3 +41,29 @@ public protocol ViewTextConfigurable: AnyObject { /// Attributed text of text object. var attributedText: NSAttributedString? { get set } } + +public extension ViewTextConfigurable { + + /// Configures text and text appearance of view using ViewText object. + /// + /// - Parameter viewText: ViewText object with text and text appearance. + func configure(with viewText: ViewText) { + switch viewText { + case let .string(text, textAttributes): + self.text = text + self.configureBaseAppearance(with: textAttributes) + + case .attributedString(let attributedString): + self.attributedText = attributedString + } + } + + /// Configures text appearance of view. + /// + /// - Parameter baseTextAttributes: Set of attributes to configure appearance of text. + func configureBaseAppearance(with baseTextAttributes: BaseTextAttributes) { + textFont = baseTextAttributes.font + titleColor = baseTextAttributes.color + textAlignment = baseTextAttributes.alignment + } +}