Imagine I have this json:
I could convert Json to string and show it by below code:
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return NSAttributedString() }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return NSAttributedString()
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
but I can’t display the image in json. How can I do that?
Related
I tried the following code and getting the Warning :
=== AttributeGraph: cycle detected through attribute 682680 ===
extension :
extension Data {
var html2AttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print("error:", error)
return nil
}
}
var html2String: String { html2AttributedString?.string ?? "" }
}
extension StringProtocol {
var html2AttributedString: NSAttributedString? {
Data(utf8).html2AttributedString
}
var html2String: String {
self.html2AttributedString?.string ?? ""
}
}
swiftui :
description = "<p class=\"uitk-flex-item uitk-flex-basis-full_wi......."
Text(description.html2String)
I am not understanding why the warning is coming because after the warning my codes behaviour is very strange. Have someone any Idae?
Here is my code which is used to convert HTML string to attributed string.
extension UILabel {
func setHTMLFromString(text: String) {
let modifiedFont = NSString(format:"<span style=\"font-family: \(self.font!.fontName); font-size: \(self.font!.pointSize)\">%#</span>" as NSString, text)
let attrStr = try! NSAttributedString(
data: modifiedFont.data(using: String.Encoding.unicode.rawValue, allowLossyConversion: true)!,
options: [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
self.attributedText = attrStr
}
}
What I want to achieve is to translate the text with attributes applied to it. Below is the code for translation that I'm using to translate the simple string.
extension UIViewController {
func translate(text:String, to:TranslateLanguage, compltion: #escaping ((Bool,String?)->()) ) {
let options = TranslatorOptions(sourceLanguage: .en, targetLanguage: to)
let translator = NaturalLanguage.naturalLanguage().translator(options: options)
perform(#selector(showHudAfterTime), with: self, afterDelay: 1)
translator.downloadModelIfNeeded(with: .init(allowsCellularAccess: true, allowsBackgroundDownloading: false)) { (error) in
NSObject.cancelPreviousPerformRequests(withTarget: self)
GlobalAPI.hideLoadingHUD()
if error == nil {
translator.translate(text) { (translation, error) in
if error == nil {
compltion(true,translation)
} else {
compltion(false,text)
}
}
} else {
compltion(false,text)
}
}
}
#objc func showHudAfterTime() {
GlobalAPI.showLoadingHud()
}
}
Let me know if need any more description.
Any help would be appreciated!
I have used this code to formatting HTML content on UILabel
var htmlToAttributedString: NSAttributedString? {
do {
guard let data = data(using: String.Encoding.utf8) else {
return nil
}
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print("error: ", error)
return nil
}
}
My HTML Content Like:
("<p>My Name is <b>Ayush</b> <I>Bansal</I></p>")
When I used above code for formatting then I get output like (My Name is Ayush Bansal).
But I want my output will be like this (My Name is Ayush Bansal)
you can use this extension
extension String {
func convertToAttributed(_ defaultFont: UIFont, textColor: UIColor ) -> NSAttributedString? {
guard let data = "".replacingOccurrences(of: "\n", with: "<br>").data(using: .utf8, allowLossyConversion: false) else { return nil }
guard let attributedString = try? NSMutableAttributedString(data: data,
options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil)
else { return nil }
attributedString.enumerateAttribute(NSAttributedString.Key.font,
in: NSRange(location: 0, length: attributedString.length),
options: NSAttributedString.EnumerationOptions(rawValue: 0))
{ (value, range, _) -> Void in
if let oldFont = value as? UIFont {
var newFont = defaultFont
let isBold = oldFont.fontName.lowercased().contains("bold")
let isItalic = oldFont.fontName.lowercased().contains("italic")
if isBold && isItalic {
newFont = UIFont.bolditalicFont(ofSize: 14)
} else if isBold {
newFont = UIFont.boldFont(ofSize: 14)
} else if isItalic {
newFont = UIFont.italicFont(ofSize: 14)
}
attributedString.removeAttribute(NSAttributedString.Key.font, range: range)
attributedString.addAttribute(NSAttributedString.Key.font, value: newFont, range: range)
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: textColor, range: range)
}
}
}
}
usage:
yourLabel.attributedText = string.convertToAttributed(font, textColor: textcolor)
In order to convert html to string, I use this extension:
extension Data {
var html2AttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print("error:", error)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
extension String {
var html2AttributedString: NSAttributedString? {
return Data(utf8).html2AttributedString
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
But text what i get in API i must convert with NSAttributedString.DocumentType.html and sometime with NSAttributedString.DocumentType.plain
How can i combine these two parameters?
What those extensions do is (1) to create a Data object out of an HTML string, (2) to convert the Data object into an NSAtrributedString object. In other words, it goes like the following.
import UIKit
class ViewController: UIViewController {
// MARK: - Variables
// MARK: - IBOutlet
#IBOutlet weak var label: UILabel!
// MARK: - IBAction
override func viewDidLoad() {
super.viewDidLoad()
let htmlStr = makeHTMLString()
let data = Data(htmlStr.utf8)
if let attributedString = try?NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
//print(attributedString.string)
label.attributedText = attributedString
}
}
func makeHTMLString() -> String {
return "<html>\n<head></head>\n<body>\n<h1>Hello, world!</h1>\n</body>\n</html>"
}
}
In the end, you don't need those extensions.
Background: When I retrieve a string from a web service, it includes HTML tags. What I get is:
"Most children who have chronic ear infections outgrow them over time.<div><br></div><div><br></div><div>test</div><div><br></div><div><br></div><div><br></div><div>test1</div>").
Now, I am displaying this to a UITextView by converting it to an attributedString so the user wouldn't see all the HTML tags. I am converting this string to attributedString like this with two extensions:
extension: String {
var data: Data {
return Data(utf8)
}
extension Data {
var attributedString: NSAttributedString? {
do {
return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print(error)
}
return nil
}
self.textView.attributedText = htmlTagString.data.attributedString
Now, the user will be able to view the textview's attributed text without the HTML Tags. But then when they save it, I am only saving the textView.attributedText.string but by doing that, the string's HTML Tags are all gone but I need to save them as well such as etc.
Question: How do I keep the HTML Tags of a textview's string/text? Please and thank you.
Swift 4:
extension NSAttributedString {
var attributedStringToHtml: String? {
do {
let htmlData = try self.data(from:NSMakeRange(0, self.length), documentAttributes:[.documentType: NSAttributedString.DocumentType.html]);
return String.init(data: htmlData, encoding: String.Encoding.utf8)
} catch {
print("error:", error)
return nil
}
}
}
Previous swift versions:
extension NSAttributedString {
var attributedStringToHtml: String? {
do {
let htmlData = try self.data(from: NSMakeRange(0, self.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType])
return String(data:htmlData, encoding:String.Encoding.utf8)
} catch {
print("error:", error)
return nil
}
}
}
Work for me
Next all code in Controller file, but first in it
self.contact.attributedText = contact.html2Attributed
Second in end file
extension String {
var html2Attributed: NSAttributedString? {
do {
guard let data = data(using: String.Encoding.utf8) else {
return nil
}
return try NSAttributedString(data: data, options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
], documentAttributes: nil)
} catch {
print("error: ", error)
return nil
}
}
}
Swift 5:
extension String{
func convertHtml() -> NSAttributedString{
guard let data = data(using: .utf8) else { return NSAttributedString() }
do{
return try NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
}catch{
return NSAttributedString()
}
}
}
How to Use?
descLbl.attributedText = "<!DOCTYPE html><html><body><h1>This is heading 1</h1><h2>This is heading 2</h2><h3>This is heading 3</h3><h4>This is heading 4</h4><h5>This is heading 5</h5><h6>This is heading 6</h6></body></html>".convertHtml()