Skip to content

Instantly share code, notes, and snippets.

@d2burke
Last active September 17, 2020 02:33
Show Gist options
  • Save d2burke/feb86dda6832076f4497c9268ee1cde4 to your computer and use it in GitHub Desktop.
Save d2burke/feb86dda6832076f4497c9268ee1cde4 to your computer and use it in GitHub Desktop.
Swift v. SwiftUI Attributed Strings
// Swift Attributed Strings
let quote = "Holy fucking shit"
let firstAttributes: [NSAttributedString.Key: Any] = [.backgroundColor: UIColor.green, NSAttributedString.Key.kern: 10]
let secondAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
let firstString = NSMutableAttributedString(string: "Holy ", attributes: firstAttributes)
let secondString = NSAttributedString(string: "fucking ", attributes: secondAttributes)
let thirdString = NSAttributedString(string: "shit")
firstString.append(secondString)
firstString.append(thirdString)
//SwiftUI Attributed Strings
(
Text("Holy ")
.kerning(10)
.font(.system(size: 18))
+
Text("fucking ")
.foregroundColor(.red)
+
Text("shit")
.font(.custom("Georgia", size: 16))
.fontWeight(.bold)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment