Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #41 from nodes-ios/feature/html-to-nsattributedstring
Browse files Browse the repository at this point in the history
Added extension for html -> NSAttributedString
  • Loading branch information
chriscombs committed Jun 28, 2017
2 parents cf56889 + e996e9e commit 313da46
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Codemine.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
293490EE1C6CAFD500E8305E /* Application.swift in Sources */ = {isa = PBXBuildFile; fileRef = 293490ED1C6CAFD500E8305E /* Application.swift */; };
293490F01C6CAFF200E8305E /* Then.swift in Sources */ = {isa = PBXBuildFile; fileRef = 293490EF1C6CAFF200E8305E /* Then.swift */; };
296831491DD5EC670002FE5A /* DispatchTime+Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 296831481DD5EC670002FE5A /* DispatchTime+Utilities.swift */; };
838A0F971F03F57E00469143 /* String+HTML.swift in Sources */ = {isa = PBXBuildFile; fileRef = 838A0F961F03F57E00469143 /* String+HTML.swift */; };
83A5BEBC1D981F3500C74312 /* UIImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A5BEBB1D981F3500C74312 /* UIImageTests.swift */; };
83A5BEBD1D98215F00C74312 /* UIImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A5BEBB1D981F3500C74312 /* UIImageTests.swift */; };
83A5BEBE1D98216000C74312 /* UIImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A5BEBB1D981F3500C74312 /* UIImageTests.swift */; };
Expand Down Expand Up @@ -128,6 +129,7 @@
293490ED1C6CAFD500E8305E /* Application.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Application.swift; sourceTree = "<group>"; };
293490EF1C6CAFF200E8305E /* Then.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Then.swift; sourceTree = "<group>"; };
296831481DD5EC670002FE5A /* DispatchTime+Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "DispatchTime+Utilities.swift"; sourceTree = "<group>"; };
838A0F961F03F57E00469143 /* String+HTML.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+HTML.swift"; sourceTree = "<group>"; };
83A5BEBB1D981F3500C74312 /* UIImageTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageTests.swift; sourceTree = "<group>"; };
83A5BEBF1D98226800C74312 /* add.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = add.png; sourceTree = "<group>"; };
83A5BEC31D98228300C74312 /* alert.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = alert.png; sourceTree = "<group>"; };
Expand Down Expand Up @@ -252,6 +254,7 @@
291272CA1C75EEB500FB1BBD /* UIImage+Utilities.swift */,
291272CC1C75EECB00FB1BBD /* UIView+Utilities.swift */,
296831481DD5EC670002FE5A /* DispatchTime+Utilities.swift */,
838A0F961F03F57E00469143 /* String+HTML.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -637,6 +640,7 @@
291272BB1C75EC2C00FB1BBD /* String+Range.swift in Sources */,
291272C91C75EE9A00FB1BBD /* NSURL+AssetSize.swift in Sources */,
296831491DD5EC670002FE5A /* DispatchTime+Utilities.swift in Sources */,
838A0F971F03F57E00469143 /* String+HTML.swift in Sources */,
291272C31C75EDE300FB1BBD /* CGRect+Utilities.swift in Sources */,
291272CB1C75EEB500FB1BBD /* UIImage+Utilities.swift in Sources */,
291272CF1C75F32200FB1BBD /* Operators.swift in Sources */,
Expand Down
39 changes: 39 additions & 0 deletions Codemine/Extensions/String+HTML.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// String+HTML.swift
// Codemine
//
// Created by Chris on 28/06/2017.
// Copyright © 2017 Nodes. All rights reserved.
//

import UIKit

public 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 {
print(error)
}
return nil
}

extension UILabel {
public convenience init(htmlString: String) {
self.init()
numberOfLines = 0
lineBreakMode = .byWordWrapping
attributedText = htmlString.attributedHTMLString
}
}

extension String {
public var attributedHTMLString: NSAttributedString? {
return stringFromHtml(string: self)
}
}

0 comments on commit 313da46

Please sign in to comment.