Edit - This has been marked as duplicate, but as I state below, I am looking for a Swift solution. Everything I've found is written in Objective C.
I am trying to convert HTML into an NSAttributedString, but can't figure how to set the font style and size. All of the examples are in Objective C which I'm not skilled in. How can I set the font style/size to System 15 (as an example)
private func stringFromHtml(string: String) -> NSAttributedString? { do { let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true) if let d = data { let str = try NSAttributedString(data: d, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) return str } } catch { } return nil }
EDIT...... I've tried several ways. I can't get it to work. I'm now getting an error message: Type of expression is ambiguous without more context. It's pointing to the NSAttributedString I've tried the following:
let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ] let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true) if let d = data { let str = try NSAttributedString(data: d, attributes: myAttribute, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
Answers. You could convert NSAttributedString to NSMutableAttributedString , and then set the font attribute . If you want to set font size from Forms project , you could create a new subclass and create custom renderer for the new custom class .
Swift 2 & 3:import UIKit extension UITextView { func increaseFontSize () { self. font = UIFont(name: (self. font?. fontName)!, size: (self.
Add the Font File to Your Xcode Project To add a font file to your Xcode project, select File > Add Files to “Your Project Name” from the menu bar, or drag the file from Finder and drop it into your Xcode project. You can add True Type Font (. ttf) and Open Type Font (. otf) files.
let myString = "Swift Attributed String" let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ] let myAttrString = NSAttributedString(string: myString, attributes: myAttribute) // set attributed text on a UILabel myLabel.attributedText = myAttrString
Font
let myAttribute = [ NSFontAttributeName: UIFont(name: "Chalkduster", size: 18.0)! ]
Shadow
let myShadow = NSShadow() myShadow.shadowBlurRadius = 3 myShadow.shadowOffset = CGSize(width: 3, height: 3) myShadow.shadowColor = UIColor.gray let myAttribute = [ NSShadowAttributeName: myShadow ]
Underline
let myAttribute = [ NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue ]
Textcolor
let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
Background Color
let myAttribute = [ NSBackgroundColorAttributeName: UIColor.yellow ]
In Swift you can use:
let stringWithAttribute = NSAttributedString(string: selectedFilter ?? "", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14.0)])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With