Skip to content

Instantly share code, notes, and snippets.

@kmdarshan
Created August 21, 2019 20:10
Show Gist options
  • Save kmdarshan/431f579bb204fc81560bdcc325e3ab95 to your computer and use it in GitHub Desktop.
Save kmdarshan/431f579bb204fc81560bdcc325e3ab95 to your computer and use it in GitHub Desktop.
Using attributed text in Swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let html = """
<html>
<body>
<p style="color: blue;
font-size: 20px;
">
Drag the blue handles to choose the section of your clip that will speed up or slow down.</p>
<p style="display:block; line-height: 50px; background-color: #05ffb0;"><br></p>
<p style="display:block; line-height: 1px; background-color: #05ffb0;"><br></p>
<p style="color: blue;
font-size: 20px;
">
You can also move the blue handles on the clip in the timeline.
</p>
</body>
</html>
"""
let data = Data(html.utf8)
let rect = CGRect(x: 10, y: 100, width: (self.view.frame.size .width) - 100, height: 300)
let label = UILabel(frame: rect)
label.backgroundColor = UIColor.red
label.numberOfLines = 0
if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
label.attributedText = attributedString
}
self.view .addSubview(label)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment