我有一些HTML数据,其中包含标题,段落,图像和列表标签.
有没有办法在一个UITextView或中显示这些数据UILabel?
我在SO上找到了这个字符串扩展,允许我将html代码转换为属性字符串:
func html2AttributedString() -> NSAttributedString {
return try! NSAttributedString(data: self.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
}
Run Code Online (Sandbox Code Playgroud)
它在Swift 3中运行良好,但是对于Swift 4,Xcode抱怨:
无法将'NSAttributedString.DocumentAttributeKey'类型的值转换为预期的字典键类型'NSAttributedString.DocumentReadingOptionKey'
我该如何解决?
说我有一个NSMutableAttributedString.
该字符串具有各种格式的格式:
这是一个例子:
这个字符串在iOS中很难改变 ,真的很糟糕.
但是,字体本身不是您想要的字体.
我想要
将每个字符更改为特定字体(例如,Avenir)
但,
在各个地方保留整个字符串中出现的其他每个属性(粗体,斜体,颜色等).
你到底怎么这样做?
注意:
如果您在整个范围内轻松添加属性"Avenir":它只是删除所有其他属性范围,您将丢失所有格式
需要明确的是:不幸的是,属性并非实际上是"附加的".
我是iOS开发的新手,现在正在开发一个接收某种JSON数据的应用程序.但是一些后端专家认为,如果用户只是直接从Word中复制信息并将其粘贴到信息系统中,对用户来说会更好.所以我坐在这里,尝试在UITableView中创建一个可点击的链接.
我从Web解析数据并获得具有以下格式的String:
Für mehr Informationen klicken sie <a href="http://www.samplelink.com/subpage.php?id=8">here</a>.
Run Code Online (Sandbox Code Playgroud)
我已经尝试了UILabel,但经过一些研究后我现在使用经常建议的UITextView.在Attributed Inspector中,我将其设置为属性文本并启用链接检测.现在文本显示为红色且可单击.
现在的问题是,HTML标签和正确的(德国)字符集仍然缺失,我不知道如何以正确的方式显示它.
显示的字符串以这种方式解析:
func showHTMLString(unformattedString: String) -> NSAttributedString{
var attrStr = NSAttributedString(
data: tmpString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
return attrStr!
}
Run Code Online (Sandbox Code Playgroud)
如果我填写Textview,attrStr?.string格式以正确的方式显示,但链接也消失了.
有什么建议如何以正确的方式格式化显示的字符串?
在此先感谢AR4G4
我想知道如何从Web网址中删除JSON中的HTML标记.我是否必须使用NSString类似的东西.
所以我想要去掉汇总值中的html标签.我查看了abit,它说NSString可以使用,但我不确定这是否可以在Swift 3中实现.任何帮助将不胜感激.
我的代码:
import UIKit
import Alamofire
struct postinput {
let mainImage : UIImage!
let name : String!
let author : String!
let summary : String!
}
class TableViewController: UITableViewController {
var postsinput = [postinput]()
var mainURL = "https://www.example.com/api"
typealias JSONstandard = [String : AnyObject]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
callAlamo(url: mainURL)
}
func callAlamo(url : String){
Alamofire.request(url).responseJSON(completionHandler: {
response in
self.parseData(JSONData: response.data!)
}) …Run Code Online (Sandbox Code Playgroud) 我有一个NSAttributedString,它由HTML构成,它显示一些图像.问题是图像比容器大,我想知道如何使它们适合它.
谢谢你的帮助
我目前正在使用此答案提供的此扩展程序将html转换为字符串中的字符串UITextView.一切都很完美,但由于某些奇怪的原因,NSFontAttributeName在此过程中不会应用于字符串.我有什么问题吗?(或者我应该在使用NSAttributedString后面的html解析字符串后应用?如果是这样,是否可以将属性应用于NSAttributeString?).
PS.使用时字体大小不会改变,html2String但html中的链接会被更长时间识别UITextView
extension String {
var html2AttributedString: NSAttributedString? {
guard
let data = dataUsingEncoding(NSUTF8StringEncoding)
else { return nil }
do {
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding, NSFontAttributeName : UIFont.systemFontOfSize(17.0)], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
cell.desc.attributedText = apiData[currentIndex].description!.html2AttributedString //Font attribute not recognized
cell.desc.text = apiData[currentIndex].description!.html2String //Font recognized by …Run Code Online (Sandbox Code Playgroud) 在我的一个模块中,我希望使用UILabel将多语言HTML文本(英语和泰米尔语)显示为NSAttributedString.如果文本是纯英文,我可以用这种方式表现出我的愿望.但我的内容包含英文和泰米尔语字符.我该怎么处理这个场景.如果有人知道,请分享您的建议.
HTML内容
<medium><b><font color='#2f3744'>IPL Tamil Web Series Episode #3 | ?????? Swetha ? | Tamil Comedy Web Series | Being Thamizhan</font></b></medium> has been succesfully scheduled on <medium><b><font color='#2f3744'>2018-05-23 08:51 PM</font></b></medium>
Run Code Online (Sandbox Code Playgroud)
期望
IPL泰米尔语网络系列第3集| யாருடாSwetha?| 泰米尔喜剧网络系列| 作为Thamizhan成功地安排在2018-05-23 08:45 PM
电流输出
IPL泰米尔语网络系列第3集| &*$%!@#$ @ ^&$&^%$ Swetha?| 泰米尔喜剧网络系列| 作为Thamizhan成功地安排在2018-05-23 08:45 PM
注意:我尝试使用下面的代码段来存档它
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return NSAttributedString() }
do {
return try NSAttributedString(data: data, options:
[NSAttributedString.DocumentReadingOptionKey.documentType: …Run Code Online (Sandbox Code Playgroud)