Created
April 10, 2019 14:54
-
-
Save st-small/2f490fdb16498ec8fec58cd0c691cf03 to your computer and use it in GitHub Desktop.
AttributedString
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
import UIKit | |
import PlaygroundSupport | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
// text | |
let quote = "Haters gonna hate" | |
// font | |
let font = UIFont.systemFont(ofSize: 72) | |
// shadow | |
let shadow = NSShadow() | |
shadow.shadowColor = UIColor.black | |
shadow.shadowBlurRadius = 5 | |
// paragraph | |
let paragraphStyle = NSMutableParagraphStyle() | |
paragraphStyle.alignment = .center | |
paragraphStyle.firstLineHeadIndent = 5.0 | |
let attributes: [NSAttributedString.Key: Any] = [ | |
.font: font, | |
.foregroundColor: UIColor.red, | |
.shadow: shadow, | |
.paragraphStyle: paragraphStyle | |
] | |
var attributedQuote = NSAttributedString(string: quote, attributes: attributes) | |
// html | |
let html = """ | |
<html> | |
<body> | |
<b> | |
<i> | |
<u> | |
<a href="https://www.apple.com"> | |
<p style="color: blue; font-size:72px;">This is<br> blue!</p> | |
</a> | |
</u> | |
</i> | |
</b> | |
</body> | |
</html> | |
""" | |
let data = Data(html.utf8) | |
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) { | |
attributedQuote = attributedString | |
} | |
let label = UILabel() | |
label.attributedText = attributedQuote | |
label.numberOfLines = 0 | |
label.sizeToFit() | |
let view = UIView(frame: CGRect(x: 0, y: 0, width: 700, height: 500)) | |
view.backgroundColor = UIColor.white | |
view.addSubview(label) | |
PlaygroundPage.current.liveView = view |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment