Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set font size on NSAttributedString

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) 
like image 681
Martin Muldoon Avatar asked Aug 30 '17 12:08

Martin Muldoon


People also ask

How do I change font size in NSAttributedString?

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 .

How do I change font size in textView in Swift?

Swift 2 & 3:import UIKit extension UITextView { func increaseFontSize () { self. font = UIFont(name: (self. font?. fontName)!, size: (self.

How do I change the font in Xcode?

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.


2 Answers

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 ] 
like image 56
Vikas Rajput Avatar answered Sep 19 '22 13:09

Vikas Rajput


In Swift you can use:

let stringWithAttribute = NSAttributedString(string: selectedFilter ?? "",                                              attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14.0)]) 
like image 29
Osman Avatar answered Sep 20 '22 13:09

Osman