我有一个UITextView,我正在管理一个NSAttributedString,最初通过键盘正常输入.我将属性字符串保存为HTML,看起来很好.当我再次加载它,并将其转换回HTML中的属性字符串时,字体大小似乎发生了变化.
例如,加载时的HTML如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 21.0px Helvetica; color: #000000; -webkit-text- stroke: #000000}
span.s1 {font-family: 'Helvetica'; font-weight: normal; font-style: normal; font-size: 21.00pt; font-kerning: none}
</style>
</head>
<body>
<p class="p1"><span class="s1">There is some text as usual lots of text</span></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我转换它并使用以下代码检查属性:
// convert to attributed string
NSError *err;
NSAttributedString *as = [[NSAttributedString alloc] …Run Code Online (Sandbox Code Playgroud) 我试图通过NSAttributedString加载HTML文件,以在UITextView中显示它.
那么,我如何将CSS应用于文本视图的内容?
在我的应用程序中,用户将键入他想要的任何内容,并且可以将文本设置为Bold,Italic和Underline.
我能够做文本 - 粗体,斜体和下划线.
但是我需要从用户输入的属性字符串中找到哪个Text(或Character)是Bold,Italic和Underline.
任何类型的链接,教程,代码都会有很大的帮助.
编辑:
我需要这样的结果......
如果字符6是斜体,则打印斜体.
如果字符14是粗体,则打印粗体.
我需要将所有文本从Calibri字体系列转换为HelveticaNeue字体系列而不更改其属性.
我有一个UITextView,我正在尝试显示一些HTML格式的属性文本.它使用内联粗体和斜体等内容.客户端现在希望更改此字体.但是,每当我更改字体时,它似乎忽略任何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) 我在label上添加了属性字符串.我显示的html文本工作正常,但默认情况下它始终显示Times新罗马家族.请告诉我如何更改文本系列.
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[inst.desc dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
Run Code Online (Sandbox Code Playgroud)