Cocoanetics / DTCoreText

Methods to allow using HTML code with CoreText
BSD 2-Clause "Simplified" License
6.35k stars 1.18k forks source link

Set spacing between lines. #1260

Open 0xifarouk opened 2 years ago

0xifarouk commented 2 years ago

How can I achieve apple spacing between lines?

Exmaple

Code I am using:

public func returnAttributedStringForHTMLString(
    fontFamily: String,
    fontName: String,
    fontSize: CGFloat,
    textColor: UIColor,
    textAlignment: CTTextAlignment
  ) -> NSMutableAttributedString {
    let encodedData = self.data(using: String.Encoding.utf8)!
    let options = [
      DTDefaultFontFamily: fontFamily,
      DTDefaultFontName: fontName,
      DTDefaultFontSize: fontSize,
      DTDefaultTextColor: textColor,
      DTDefaultTextAlignment: NSNumber(value: textAlignment.rawValue),
    ] as [String : Any]
    let builder = DTHTMLAttributedStringBuilder(html: encodedData, options: options, documentAttributes: nil)
    var returnValue: NSAttributedString?
    returnValue = builder?.generatedAttributedString()
    if returnValue != nil {
      // needed to show link highlighting
      let mutable = NSMutableAttributedString(attributedString: returnValue!)
      mutable.removeAttribute(NSAttributedString.Key.foregroundColor, range: NSMakeRange(0, mutable.length))
      return mutable
    } else {
      return NSMutableAttributedString(string: "")
    }
  }

Call site:

    Text(
      AttributedString(
        html.returnAttributedStringForHTMLString(
          fontFamily: "SF Text",
          fontName: "SF Text Regular",
          fontSize: 17,
          textColor: .label,
          textAlignment: .left
        )
      )
    )

Also I want to use default apple font that comes with the system, I am using "SF Text" for fontFamily and "SF Text Regular" for fontName, is this correct? or if there is a way to omit the parameters and automatically use the system font.