gpt4 book ai didi

html - 使用字体从 HTML 设置标签文本 NSAttributedString

转载 作者:行者123 更新时间:2023-11-28 06:22:15 26 4
gpt4 key购买 nike

我正在从服务器获取 html 文本并尝试使用此代码将其设置为标签

let about = try! NSAttributedString(
data: (myHTMLText as! String).data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
self.aboutLabel.attributedText = about

但在执行此操作后,我的标签字体将更改为默认字体。如何将我的字体设置为标签而不丢失文本的属性部分?

最佳答案

我使用以下内容,它可以很好地为段落设置样式。如果您想为整个文本设置样式,请改用 body 标签。您可以看到生成的 NSAttributedString 具有正确的字体样式。

//: Playground - noun: a place where people can play

import UIKit

extension UIFont {
class func printFontNames() {
for family in UIFont.familyNames {
let fonts = UIFont.fontNames(forFamilyName: family)

print("Family: ", family, "Font Names: ", fonts)
}
}
}

UIFont.printFontNames()


let paragraphFont = "AvenirNextCondensed-Medium"
let paragraphSize = 12.0
let defaultFont = "Helvetica-Bold"
let defaultSize = 15.0

let htmlStyle = "<style>p {font-family:\(paragraphFont); font-size:\(paragraphSize)px;} body {font-family:\(defaultFont); font-size:\(defaultSize)px;}}</style>"

let htmlText = "<p>Some Paragraph..</p> Some other text"
let htmlString = "\(htmlStyle)\n\n\(htmlText)"

let html = try! NSAttributedString(data: htmlString.data(using: .utf8)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

print(html)

结果是:

Family:  Avenir Next Condensed 
Font Names: ["AvenirNextCondensed-BoldItalic", "AvenirNextCondensed-Heavy", "AvenirNextCondensed-Medium", "AvenirNextCondensed-Regular", "AvenirNextCondensed-HeavyItalic", "AvenirNextCondensed-MediumItalic", "AvenirNextCondensed-Italic", "AvenirNextCondensed-UltraLightItalic", "AvenirNextCondensed-UltraLight", "AvenirNextCondensed-DemiBold", "AvenirNextCondensed-Bold", "AvenirNextCondensed-DemiBoldItalic"]

Some Paragraph..
{
NSColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSFont = "<UICTFont: 0x7fbd5c706c20> font-family: \"AvenirNextCondensed-Medium\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 12, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 17/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSStrokeWidth = 0;
}Some other text{
NSColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSFont = "<UICTFont: 0x7fbd5f203bf0> font-family: \"Helvetica\"; font-weight: bold; font-style: normal; font-size: 15.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 19/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0 0 0 1 ";
NSStrokeWidth = 0;
}

关于html - 使用字体从 HTML 设置标签文本 NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43033876/

26 4 0