Skip to content

Instantly share code, notes, and snippets.

@alexnikol
Created April 17, 2020 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexnikol/155c4b800ce40670c44d707e7b7d70df to your computer and use it in GitHub Desktop.
Save alexnikol/155c4b800ce40670c44d707e7b7d70df to your computer and use it in GitHub Desktop.
Full Example of Attributed UIlabel
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let mrAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.green, //Text color
.font: UIFont.systemFont(ofSize: 25.0), //Font, Font size
.strikethroughStyle: 3, //Width of strikethrough line
.strikethroughColor: UIColor.red, //Color of strikethrough line
]
let nameAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.white, //Text color
.font: UIFont.systemFont(ofSize: 50.0), //Font, Font size
.backgroundColor: UIColor.yellow, //Background color
.strokeWidth: 5.0, //Word border width
.strokeColor: UIColor.gray, //Word border color
.kern: 20 //Space between letters
]
let lastNameAttributes: [NSAttributedString.Key: Any] = [
.foregroundColor: UIColor.red, //Text color
.font: UIFont.systemFont(ofSize: 30.0, weight: .heavy), //Font, Font size and Font Weight
.underlineStyle: 2, //Width of underline
.underlineColor: UIColor.blue //Color of underline
]
let mr = "Mr."
let name = "John"
let lastName = "Doe"
let mrString = NSMutableAttributedString(string: mr, attributes: mrAttributes)
let nameString = NSAttributedString(string: name, attributes: nameAttributes)
let lastNameString = NSAttributedString(string: lastName, attributes: lastNameAttributes)
mrString.append(nameString)
mrString.append(lastNameString)
let label = UILabel()
label.attributedText = mrString
label.frame = CGRect(x: 100, y: 100, width: 400, height: 400)
self.view.addSubview(label)
self.view.backgroundColor = UIColor.white
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment