add missing ViewTextConfigurable extensions

This commit is contained in:
Ivan Smolin 2020-10-21 22:55:14 +03:00
parent 4af10aa107
commit 64cb766542
3 changed files with 95 additions and 1 deletions

View File

@ -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)
}
}

View File

@ -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 strings bounding rectangle.
/// - maxHeight: The width constraint to apply when computing the strings 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)
}
}

View File

@ -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
}
}