Created
July 26, 2019 22:53
-
-
Save carson-katri/38971406c184014c39863093e79a5174 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func colored(_ color: UIColor) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.foregroundColor : color]) | |
} | |
func background(_ color: UIColor) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.backgroundColor: color]) | |
} | |
func underline(_ color: UIColor, style: NSUnderlineStyle = .single) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.underlineColor: color, .underlineStyle: style.rawValue]) | |
} | |
func font(_ font: UIFont) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.font: font]) | |
} | |
func shadow(_ shadow: NSShadow) -> NSAttributedString { | |
NSAttributedString(string: self, attributes: [.shadow: shadow]) | |
} | |
var attributed: NSAttributedString { | |
NSAttributedString(string: self) | |
} | |
} | |
extension NSAttributedString { | |
func apply(_ attributes: [NSAttributedString.Key:Any]) -> NSAttributedString { | |
let mutable = NSMutableAttributedString(string: self.string, attributes: self.attributes(at: 0, effectiveRange: nil)) | |
mutable.addAttributes(attributes, range: NSMakeRange(0, (self.string as NSString).length)) | |
return mutable | |
} | |
func colored(_ color: UIColor) -> NSAttributedString { | |
self.apply([.foregroundColor : color]) | |
} | |
func background(_ color: UIColor) -> NSAttributedString { | |
self.apply([.backgroundColor: color]) | |
} | |
func underline(_ color: UIColor, style: NSUnderlineStyle = .single) -> NSAttributedString { | |
self.apply([.underlineColor: color, .underlineStyle: style.rawValue]) | |
} | |
func font(_ font: UIFont) -> NSAttributedString { | |
self.apply([.font: font]) | |
} | |
func shadow(_ shadow: NSShadow) -> NSAttributedString { | |
self.apply([.shadow:shadow]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment