DKHTMLAttributedString
This pod can helps developers handling HTML strings without any effort. Just pass a String
object to given method and you'll get an NSAttributedString
object that you can display inside an UITextView
or an UILabel
.
This library is a Swift
port of NSAttributedString-DDHTML
and, since this: "it's not intended to support full HTML rendering. Instead it provides a quick, efficent and light-weight implementation for leveraging attributed strings when utilizing native UIKit interface elements."
Requirements
DKHTMLAttributedString requires a minimum iOS deployment target of iOS 9.0
.
Setup
CocoaPods
One of the easiest ways to integrate DKHTMLAttributedString in your project is to use CocoaPods:
-
Add the following line to your
Podfile
:pod 'DKHTMLAttributedString'
-
In your project directory, run
pod update
-
You should now be done.
Usage
import DKHTMLAttributedString
let htmlString = "<b>Hello</b> <i>World!</i>"
let attributedString = DKHTMLAttributedString.attributedString(from: htmlString)
self.textView.attributedText = attributedString
Full method
open class func attributedString(from htmlString: String?,
normalFont: UIFont? = nil,
boldFont: UIFont? = nil,
italicFont: UIFont? = nil,
imageMap: [String: UIImage]? = nil
) -> NSAttributedString?
Supported Tags
1. <b>
2. <strong>
3. <i>
4. <em>
5. <u>
6. <strike>
7. <br>
8. <p>
align
: Alignment of text, e.g.align="center"
- Available values:
left
,center
,right
,justify
- Available values:
lineBreakMode
: How to handle text which doesn't fit horizontally in the view- Available values: WordWrapping, CharWrapping, Clipping, TruncatingHead, TruncatingTail, TruncatingMiddle
firstLineHeadIndent
headIndent
hyphenationFactor
lineHeightMultiple
lineSpacing
maximumLineHeight
minimumLineHeight
paragraphSpacing
paragraphSpacingBefore
tailIndent
9. <img>
src
: key inimageMap
parameterwidth
: pxheight
: px
License
MIT License. See LICENSE
file for further information.