英文:
`.font` not working on `Text` with HTML attributes
问题
以下是您要翻译的内容:
"Text can render AttributedString with HTML tags using AttributedString with the uiKit flag. The problem is I can't apply font to the resulting component. fontWeight works but font doesn't. Even underline works.
I need the font modifier as I'm using a custom font. Even System font does not work though.
struct SecondContentView: View {
    var body: some View {
        let html = "<u>Heading</u> paragraph."
        if var nsAttributedString = try? NSAttributedString(data: Data(html.utf8), options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil),
           let attributedString = try? AttributedString(nsAttributedString, including: \.uiKit) {
            Text(attributedString)
                .font(Font.system(size: 48))
                .fontWeight(.bold)
        } else {
            Text(html)
        }
    }
}
struct SecondContentView_Previews: PreviewProvider {
    static var previews: some View {
        SecondContentView()
    }
}
I tried other properties and only font was not working."
英文:
Text can render AttributedString with HTML tags using AttributedString with the uiKit flag. The problem is I can't apply font to the resulting component. fontWeight works but font doesn't. Even underline works.
I need the font modifier as I'm using a custom font. Even System font does not work though.
struct SecondContentView: View {
    var body: some View {
        let html = "<u>Heading</u> paragraph."
        if var nsAttributedString = try? NSAttributedString(data: Data(html.utf8), options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil),
           let attributedString = try? AttributedString(nsAttributedString, including: \.uiKit) {
            Text(attributedString)
                .font(Font.system(size: 48))
                .fontWeight(.bold)
        } else {
            Text(html)
        }
    }
}
struct SecondContentView_Previews: PreviewProvider {
    static var previews: some View {
        SecondContentView()
    }
}
I tried other properties and only font was not working.
答案1
得分: 1
以下是已翻译的内容:
这在初始化程序的文档中已经非常清楚地说明了(重点在于):
使用此初始化程序根据指定的
AttributedString中的属性来设置文本样式。属性字符串中的属性优先于视图修饰符添加的样式。 例如,以下示例中的属性文本将显示为蓝色,尽管在封闭的VStack中使用foregroundColor(_:)修饰符使用红色:
var content: AttributedString {
    var attributedString = AttributedString("Blue text")
    attributedString.foregroundColor = .blue
    return attributedString
}
var body: some View {
    VStack {
        Text(content)
        Text("Red text")
    }
    .foregroundColor(.red)
}
如果您print(attributedString),您将看到它已经具有NSFont属性。这是输出内容:
Heading {
	NSKern = 0.0
	NSColor = kCGColorSpaceModelRGB 0 0 0 1 
	NSStrokeWidth = 0.0
	NSFont = <UICTFont: 0x7fb3dcd18a10> font-family: "Times New Roman"; font-weight: normal; font-style: normal; font-size: 12.00pt
	NSUnderline = NSUnderlineStyle(rawValue: 1)
	NSStrokeColor = kCGColorSpaceModelRGB 0 0 0 1 
	NSParagraphStyle = Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (
), DefaultTabInterval 36, Blocks (
), Lists (
), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0 PresentationIntents (
) ListIntentOrdinal 0 CodeBlockIntentLanguageHint ''
}
 paragraph {
	NSColor = kCGColorSpaceModelRGB 0 0 0 1 
	NSFont = <UICTFont: 0x7fb3dcd18a10> font-family: "Times New Roman"; font-weight: normal; font-style: normal; font-size: 12.00pt
	NSStrokeColor = kCGColorSpaceModelRGB 0 0 0 1 
	NSKern = 0.0
	NSStrokeWidth = 0.0
	NSParagraphStyle = Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (
), DefaultTabInterval 36, Blocks (
), Lists (
), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0 PresentationIntents (
) ListIntentOrdinal 0 CodeBlockIntentLanguageHint ''
}
如果您想使用视图修饰符指定字体,只需删除现有的字体属性。
Text(attrString.transformingAttributes(AttributeScopes.UIKitAttributes.FontAttribute.self) { font in
    font.value = nil
})
.font(...)
英文:
This is stated pretty clearly in the initialiser's documentation (emphasis mine):
> Use this initializer to style text according to attributes found in
> the specified AttributedString. Attributes in the attributed
> string take precedence over styles added by view modifiers. For
> example, the attributed text in the following example appears in blue,
> despite the use of the foregroundColor(_:) modifier to use red
> throughout the enclosing VStack:
>
>     var content: AttributedString {
>         var attributedString = AttributedString("Blue text")
>         attributedString.foregroundColor = .blue
>         return attributedString
>     }
>
>     var body: some View {
>         VStack {
>             Text(content)
>             Text("Red text")
>         }
>         .foregroundColor(.red)
>     }
If you print(attributedString), you will see that it already has a NSFont attribute. This is the output:
Heading {
	NSKern = 0.0
	NSColor = kCGColorSpaceModelRGB 0 0 0 1 
	NSStrokeWidth = 0.0
	NSFont = <UICTFont: 0x7fb3dcd18a10> font-family: "Times New Roman"; font-weight: normal; font-style: normal; font-size: 12.00pt
	NSUnderline = NSUnderlineStyle(rawValue: 1)
	NSStrokeColor = kCGColorSpaceModelRGB 0 0 0 1 
	NSParagraphStyle = Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (
), DefaultTabInterval 36, Blocks (
), Lists (
), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0 PresentationIntents (
) ListIntentOrdinal 0 CodeBlockIntentLanguageHint ''
}
 paragraph {
	NSColor = kCGColorSpaceModelRGB 0 0 0 1 
	NSFont = <UICTFont: 0x7fb3dcd18a10> font-family: "Times New Roman"; font-weight: normal; font-style: normal; font-size: 12.00pt
	NSStrokeColor = kCGColorSpaceModelRGB 0 0 0 1 
	NSKern = 0.0
	NSStrokeWidth = 0.0
	NSParagraphStyle = Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (
), DefaultTabInterval 36, Blocks (
), Lists (
), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0 LineBreakStrategy 0 PresentationIntents (
) ListIntentOrdinal 0 CodeBlockIntentLanguageHint ''
}
If you want to specify a font with a view modifier, just remove the existing font attribute.
Text(attrString.transformingAttributes(AttributeScopes.UIKitAttributes.FontAttribute.self) { font in
    font.value = nil
})
.font(...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论