Last active
July 12, 2023 08:43
-
-
Save carson-katri/d398764a76a9b525e4a34e360cb70187 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 foregroundColor(_ 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 foregroundColor(_ 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