diff --git a/TIUIKitCore/Sources/ViewText/BaseTextAttributes.swift b/TIUIKitCore/Sources/ViewText/BaseTextAttributes.swift new file mode 100644 index 00000000..e51c6abc --- /dev/null +++ b/TIUIKitCore/Sources/ViewText/BaseTextAttributes.swift @@ -0,0 +1,47 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the Software), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import UIKit.UIFont +import UIKit.UIColor + +/// Base set of attributes to configure appearance of text. +open class BaseTextAttributes { + + /// Text font. + public let font: UIFont + /// Text color. + public let color: UIColor + /// Text alignment. + public let alignment: NSTextAlignment + + /// Memberwise initializer. + /// + /// - Parameters: + /// - font: Text font. + /// - color: Text color. + /// - alignment: Text alignment. + public init(font: UIFont, color: UIColor, alignment: NSTextAlignment = .natural) { + self.font = font + self.color = color + self.alignment = alignment + } +} diff --git a/TIUIKitCore/Sources/ViewText/ViewText.swift b/TIUIKitCore/Sources/ViewText/ViewText.swift new file mode 100644 index 00000000..da7f0822 --- /dev/null +++ b/TIUIKitCore/Sources/ViewText/ViewText.swift @@ -0,0 +1,33 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the Software), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import Foundation.NSAttributedString + +/// Enum that describes text with appearance options. +/// +/// - string: Regular string with common and often-used text attributes. +/// - attributedString: Attributed string. +public enum ViewText { + + case string(String, textAttributes: BaseTextAttributes) + case attributedString(NSAttributedString) +} diff --git a/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/UIButton+ViewTextConfigurable.swift b/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/UIButton+ViewTextConfigurable.swift new file mode 100644 index 00000000..de0e5e5f --- /dev/null +++ b/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/UIButton+ViewTextConfigurable.swift @@ -0,0 +1,121 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the Software), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import UIKit.UIButton + +extension UIButton: ViewTextConfigurable { + + public var textFont: UIFont? { + get { + return titleLabel?.font + } + set { + titleLabel?.font = newValue + } + } + + public var titleColor: UIColor? { + get { + return currentTitleColor + } + set { + setTitleColor(newValue, for: []) + } + } + + public var textAlignment: NSTextAlignment { + get { + return contentHorizontalAlignment.textAlignment + } + set { + contentHorizontalAlignment = .init(textAlignment: newValue) + } + } + + public var text: String? { + get { + return currentTitle + } + set { + setTitle(newValue, for: []) + } + } + + public var attributedText: NSAttributedString? { + get { + return currentAttributedTitle + } + set { + setAttributedTitle(newValue, for: []) + } + } +} + +private extension UIControl.ContentHorizontalAlignment { + + init(textAlignment: NSTextAlignment) { + switch textAlignment { + case .left: + self = .leading + + case .right: + self = .trailing + + case .center: + self = .center + + case .justified: + self = .fill + + case .natural: + self = .leading + + @unknown default: + self = .leading + } + } + + var textAlignment: NSTextAlignment { + switch self { + case .left: + return .left + + case .right: + return .right + + case .center: + return .center + + case .fill: + return .justified + + case .leading: + return .natural + + case .trailing: + return .right + + @unknown default: + return .natural + } + } +} diff --git a/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/UILabel+ViewTextConfigurable.swift b/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/UILabel+ViewTextConfigurable.swift new file mode 100644 index 00000000..8418ffa4 --- /dev/null +++ b/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/UILabel+ViewTextConfigurable.swift @@ -0,0 +1,44 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the Software), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import UIKit.UILabel + +extension UILabel: ViewTextConfigurable { + + public var textFont: UIFont? { + get { + return font + } + set { + font = newValue + } + } + + public var titleColor: UIColor? { + get { + return textColor + } + set { + textColor = newValue + } + } +} diff --git a/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/UITextField+ViewTextConfigurable.swift b/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/UITextField+ViewTextConfigurable.swift new file mode 100644 index 00000000..eaa2ce79 --- /dev/null +++ b/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/UITextField+ViewTextConfigurable.swift @@ -0,0 +1,44 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the Software), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import UIKit.UITextField + +extension UITextField: ViewTextConfigurable { + + public var textFont: UIFont? { + get { + return font + } + set { + font = newValue + } + } + + public var titleColor: UIColor? { + get { + return textColor + } + set { + textColor = newValue + } + } +} diff --git a/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/ViewTextConfigurable.swift b/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/ViewTextConfigurable.swift new file mode 100644 index 00000000..ee2b5f9a --- /dev/null +++ b/TIUIKitCore/Sources/ViewText/ViewTextConfigurable/ViewTextConfigurable.swift @@ -0,0 +1,43 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the Software), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import UIKit.UIFont +import UIKit.UIColor + +/// Protocol that represents text object with appearance attributes. +public protocol ViewTextConfigurable: AnyObject { + + /// Font of text object. + var textFont: UIFont? { get set } + + /// Text color of text object. + var titleColor: UIColor? { get set } + + /// Text alignment of text object. + var textAlignment: NSTextAlignment { get set } + + /// Text itself of text object. + var text: String? { get set } + + /// Attributed text of text object. + var attributedText: NSAttributedString? { get set } +}