2 // NSAttributedString+Ext.swift
5 // Created by Pawel Dabrowski on 30/05/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
13 extension NSAttributedString{
16 static func AttributedString(font: UIFont, text: String, lineSpacing: CGFloat, textAlignment: NSTextAlignment) -> NSAttributedString{
18 let paragraphStyle = NSMutableParagraphStyle()
19 paragraphStyle.lineSpacing = 1.0
20 paragraphStyle.lineHeightMultiple = lineSpacing
21 paragraphStyle.alignment = textAlignment
23 let attrString = NSMutableAttributedString(string: text)
24 attrString.addAttribute(NSAttributedStringKey.font, value: font, range: NSMakeRange(0, attrString.length))
25 attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
29 static func AttributedString(font: UIFont, text: String, color: UIColor) -> NSAttributedString{
31 let attrString = NSMutableAttributedString(string: text)
32 attrString.addAttribute(NSAttributedStringKey.font, value: font, range: NSMakeRange(0, attrString.length))
33 attrString.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: NSMakeRange(0, attrString.length))
38 public convenience init(html:String) throws {
40 let htmString = "<html><head><style> " +
42 //"text-align: justify;\n" +
43 "font-size: 11.00pt;\n" +
44 "font-family: '-apple-system';\n" +
45 "line-height: 6px;\n" +
47 "</style></head><body>\(html)</body>" +
51 guard let data = htmString.data(using: .unicode, allowLossyConversion: true) else {
52 throw NSError(domain: "Invalid HTML", code: -500, userInfo: nil)
55 try self.init(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
59 public convenience init(string: String?, font: UIFont?) {
60 self.init(string: string, font: font, color: nil, textAlignment: nil, lineSpacing: nil)
63 public convenience init(string: String?, font: UIFont?, color: UIColor?) {
64 self.init(string: string, font: font, color: color, textAlignment: nil, lineSpacing: nil)
67 public convenience init(string: String?, font: UIFont?, color: UIColor?, textAlignment: NSTextAlignment?) {
68 self.init(string: string, font: font, color: color, textAlignment: textAlignment, lineSpacing: nil)
71 public convenience init(string: String?, font: UIFont?, textAlignment: NSTextAlignment?) {
72 self.init(string: string, font: font, color: nil, textAlignment: textAlignment, lineSpacing: nil)
75 public convenience init(string: String?, font: UIFont?, color: UIColor?, textAlignment: NSTextAlignment?, lineSpacing: CGFloat?) {
77 let style = NSMutableParagraphStyle()
81 style.lineBreakMode = .byWordWrapping;
84 if let value = string{
88 var dict = [NSAttributedStringKey : AnyObject]()
90 dict[NSAttributedStringKey.font] = fnt
94 dict[NSAttributedStringKey.foregroundColor] = clr
97 if let tal = textAlignment{
99 if(tal == .justified){
100 style.firstLineHeadIndent = 0.05;
105 if let ls = lineSpacing{
106 style.lineSpacing = ls
112 dict[NSAttributedStringKey.paragraphStyle] = style
115 self.init(string: str, attributes: dict)