Skip to content

Instantly share code, notes, and snippets.

@lhoward
Created November 4, 2020 22:21
Show Gist options
  • Select an option

  • Save lhoward/f8e3d5b4e5e6c1d2c64e82284e23c248 to your computer and use it in GitHub Desktop.

Select an option

Save lhoward/f8e3d5b4e5e6c1d2c64e82284e23c248 to your computer and use it in GitHub Desktop.
import SwiftUI
public protocol TextView {
var font: UIFont? { get set }
func font(_ font: Font?) -> Self
var textAlignment: NSTextAlignment { get set }
func multilineTextAlignment(_ alignment: TextAlignment) -> Self
var lineBreakMode: NSLineBreakMode { get set }
func truncationMode(_ mode: Text.TruncationMode) -> Self
var textColor: UIColor { get set }
func foregroundColor(_ color: Color?) -> Self
var backgroundColor: UIColor { get set }
func backgroundColor(_ color: Color?) -> Self
}
public extension TextView {
func font(_ font: Font?) -> Self {
var view = self
if let font = font {
view.font = UIFont.with(font: font)
} else {
view.font = nil
}
return view
}
func multilineTextAlignment(_ alignment: TextAlignment) -> Self {
var view = self
switch alignment {
case .center:
view.textAlignment = .center
case .leading:
view.textAlignment = .left
case .trailing:
view.textAlignment = .right
}
return view
}
func truncationMode(_ mode: Text.TruncationMode) -> Self {
var view = self
switch mode {
case .head:
view.lineBreakMode = .byTruncatingHead
case .middle:
view.lineBreakMode = .byTruncatingMiddle
case .tail:
view.lineBreakMode = .byTruncatingTail
default:
view.lineBreakMode = .byClipping
}
return view
}
func foregroundColor(_ color: Color?) -> Self {
var view = self
if let color = color {
view.textColor = UIColor(color)
} else {
view.textColor = .label
}
return view
}
func backgroundColor(_ color: Color?) -> Self {
var view = self
if let color = color {
view.backgroundColor = UIColor(color)
} else {
view.backgroundColor = .clear
}
return view
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment