Xcode HTML到字符串返回零?- SWIFT 3
Posted
技术标签:
【中文标题】Xcode HTML到字符串返回零?- SWIFT 3【英文标题】:Xcode HTML to String return nil ?- SWIFT 3 【发布时间】:2018-03-30 00:21:41 【问题描述】:public func sethtmlBody(_ body: String, bounds: CGRect = UIScreen.main.bounds) -> String
let font = "-apple-system"
let fontSize = 19
let fontColor = "#000000"
let lineHeight = 25
let imageWidth = bounds.width - 40
let margin = 20
let codeStyle = "pre[class*=\"language-\"]background:white;border-radius:14px;color:black;display:block;font-size:16px;font-weight:500;padding:20px;overflow-x:auto;white-space:pre-wrap;line-height:130%;pre.language-coffeescript .token.commentcolor:#6a7576pre.language-coffeescript .token.stringcolor:#8ADC64pre.language-coffeescript .token.number,pre.language-coffeescript .token.operatorcolor:#a580f8pre.language-coffeescript .token.keyword,pre.language-coffeescript .token.class-name,pre.language-coffeescript .token.functioncolor:#8dfpre.language-swift .token.commentcolor:#690pre.language-swift .token.stringcolor:#ee433fpre.language-swift .token.keywordcolor:#C945A7pre.language-swift .token.number,pre.language-swift .token.function,pre.language-swift .token.builtin,pre.language-swift .token.class-namecolor:#5C2699"
let htmlString = "<style>p, li font-family:\"\(font)\"; color: \(fontColor); font-size:\(fontSize)px; line-height:\(lineHeight)px p margin: \(margin)px 0; img max-width: \(imageWidth)px; #p font-weight: bold; font-size: 24px; line-height: 130%; margin: 50px 20px; ul li, ol li margin: 20px 0; font-weight: bold; \(codeStyle)</style>\(body)"
return htmlString
我正在使用 swift 3。为什么该函数将我返回为 Nil?
签名:
bodyAttributedString = setHtmlBody(sectionBody).htmlToAttributedString
【问题讨论】:
你的函数返回String
,而不是String?
。所以它不能为零
bodyAttributedString = setHtmlBody(sectionBody).htmlToAttributedString,在这种情况下,没有
什么是htmlToAttributedString
?这不是标准库或基金会、AFAIK 中 String
的属性。
【参考方案1】:
这将是htmlToAttributedString
功能所有其他工作正常的问题。我用类似的方法尝试过,它对我来说非常有效
请检查...
扩展将html作为属性字符串
extension Data
var htmlToAttributedString: 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 htmlToAttributedString?.string ?? ""
extension String
var htmlToAttributedString: NSAttributedString?
return Data(utf8).htmlToAttributedString
var html2String: String
return htmlToAttributedString?.string ?? ""
HTML 字符串构建函数
public func setHtmlBody(_ body: String, bounds: CGRect = UIScreen.main.bounds) -> String
let font = "-apple-system"
let fontSize = 19
let fontColor = "#000000"
let lineHeight = 25
let imageWidth = bounds.width - 40
let margin = 20
let codeStyle = "pre[class*=\"language-\"]background:white;border-radius:14px;color:black;display:block;font-size:16px;font-weight:500;padding:20px;overflow-x:auto;white-space:pre-wrap;line-height:130%;pre.language-coffeescript .token.commentcolor:#6a7576pre.language-coffeescript .token.stringcolor:#8ADC64pre.language-coffeescript .token.number,pre.language-coffeescript .token.operatorcolor:#a580f8pre.language-coffeescript .token.keyword,pre.language-coffeescript .token.class-name,pre.language-coffeescript .token.functioncolor:#8dfpre.language-swift .token.commentcolor:#690pre.language-swift .token.stringcolor:#ee433fpre.language-swift .token.keywordcolor:#C945A7pre.language-swift .token.number,pre.language-swift .token.function,pre.language-swift .token.builtin,pre.language-swift .token.class-namecolor:#5C2699"
let htmlString = "<style>p, li font-family:\"\(font)\"; color: \(fontColor); font-size:\(fontSize)px; line-height:\(lineHeight)px p margin: \(margin)px 0; img max-width: \(imageWidth)px; #p font-weight: bold; font-size: 24px; line-height: 130%; margin: 50px 20px; ul li, ol li margin: 20px 0; font-weight: bold; \(codeStyle)</style>\(body)"
return htmlString
演示文稿
override func viewDidLoad()
super.viewDidLoad()
let body = "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>"
let htmlString = setHtmlBody(body)
if let attributedString = htmlString.htmlToAttributedString
self.labelHTML.attributedText = attributedString
输出
【讨论】:
以上是关于Xcode HTML到字符串返回零?- SWIFT 3的主要内容,如果未能解决你的问题,请参考以下文章
《从零开始学Swift》学习笔记(Day 52)——Cocoa错误处理模式
在 Xcode 7 Swift 2 中将字符串与数组分开以供 HTML 使用
为啥我在 swift 上的视图是这样的 我更新了它,现在我可以向下滑动并能够返回到最后一页 XCODE 11