`.font` 在带有 HTML 属性的 `Text` 上无效。

huangapple go评论74阅读模式
英文:

`.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 = &quot;&lt;u&gt;Heading&lt;/u&gt; paragraph.&quot;

        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 = &lt;UICTFont: 0x7fb3dcd18a10&gt; 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 = &lt;UICTFont: 0x7fb3dcd18a10&gt; 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 = &lt;UICTFont: 0x7fb3dcd18a10&gt; font-family: &quot;Times New Roman&quot;; 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 &#39;&#39;
}
 paragraph {
	NSColor = kCGColorSpaceModelRGB 0 0 0 1 
	NSFont = &lt;UICTFont: 0x7fb3dcd18a10&gt; font-family: &quot;Times New Roman&quot;; 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 &#39;&#39;
}

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(...)

huangapple
  • 本文由 发表于 2023年6月29日 10:32:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定