English 中文(简体)
`font'没有在“Text”工作,具有超文本属性
原标题:`.font` not working on `Text` with HTML attributes

案文可在<条码>上加上使用<条码>的超文本标签。 Kit sign. 问题在于,我可以先将<代码>font应用于由此产生的构成部分。 <代码>fontWalaworking but font don t. Even underline work.

我需要老调器,而我用的是老板。 即便是系统字体也不可行。

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.

问题回答

这在最初的主人上得到了明确表述。 (斜体):

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)
}

如果您(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(...)




相关问题
SwiftUI: How do I position a button x points above a sheet?

I am trying to recreate a Maps-style UI with a persistent bottom sheet and controls fixed above. Apple Maps UI with controls above ☝️ Example here, the "Look Around" binoculars button and ...

BraintreeDropIn deprecated functions error

I m getting this error on the latest release of BraintreeDropIn, I really don t get it since based on the patch notes, this should ve been addressed already, I have already tried cleaning derived data,...

how to limit zoom level in map swift ui?

how to limit zoom level in map swift ui i have map and need to make zoom level limited Map(coordinateRegion: $region,annotationItems: viewModel.nearbyGyms) { item in ...

UIImageView - How to get the file name of the image assigned?

Is it possible to read the name of an UIImageView s UIImage that s presently stored in the UIImageView? I was hoping you could do something kind of like this, but haven t figured it out. NSString *...

热门标签