这在最初的主人上得到了明确表述。 (斜体):
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(...)