import{Host,Text}from'@expo/ui/swift-ui';import{ font, foregroundStyle }from'@expo/ui/swift-ui/modifiers';exportdefaultfunctionStyledTextExample(){return(<Hoststyle={{ flex:1}}><Textmodifiers={[font({ size:24, weight:'bold'}),foregroundStyle('blue'),]}>
Large Bold Blue Text
</Text></Host>);}
嵌套文本(每段样式)
🌐 Nested text (per-segment styling)
嵌套 Text 组件以对单独的片段进行不同的样式设置。这对于内联格式很有用,例如句子中的加粗或彩色单词。
🌐 Nest Text components to style individual segments differently. This is useful for inline formatting, such as bold or colored words within a sentence.
注意: 嵌套文本使用 SwiftUI 的 Text 拼接,因此只有返回 Text 的修饰符(例如带有颜色的 bold、italic、font、foregroundColor 和 foregroundStyle)会应用于嵌套部分。
使用带有 family 参数的 font 修饰符来使用自定义字体。你可以使用 expo-font 库加载自定义字体。
🌐 Use the font modifier with a family parameter to use custom fonts. You can load custom fonts using expo-font library.
CustomFontExample.tsx
import{Host,Text,VStack}from'@expo/ui/swift-ui';import{ font }from'@expo/ui/swift-ui/modifiers';exportdefaultfunctionCustomFontExample(){return(<HostmatchContentsstyle={{ alignSelf:'center'}}><VStackspacing={4}><Textmodifiers={[font({ family:'Inter-Bold', size:18})]}>
Inter Bold
</Text><Textmodifiers={[font({ family:'Inter-Regular', size:18})]}>
Inter Regular
</Text></VStack></Host>);}
有行数限制的文本
🌐 Text with line limit
使用 lineLimit 修饰符在达到一定行数后截断文本。
🌐 Use the lineLimit modifier to truncate text after a certain number of lines.
LineLimitExample.tsx
import{Host,Text}from'@expo/ui/swift-ui';import{ lineLimit }from'@expo/ui/swift-ui/modifiers';exportdefaultfunctionLineLimitExample(){const longText ='This is a very long text that will be truncated after two lines. '.repeat(5);return(<Hoststyle={{ flex:1}}><Textmodifiers={[lineLimit(2)]}>{longText}</Text></Host>);}
Markdown
使用 markdownEnabled 属性来启用文本内容的 Markdown 格式化。
🌐 Use the markdownEnabled property to enable Markdown formatting for the text content.
MarkdownTextExample.tsx
import{Host,Text,VStack}from'@expo/ui/swift-ui';exportdefaultfunctionMarkdownTextExample(){return(<Hoststyle={{ flex:1}}><VStackspacing={4}><TextmarkdownEnabled>Regular text.</Text><TextmarkdownEnabled>
This is **bold text**, *italic text* and ***text in both
bold and italic***.
</Text><TextmarkdownEnabled>~~Strikethrough text~~</Text><TextmarkdownEnabled>`This is monospaced text`</Text><TextmarkdownEnabled>
Visit the [Expo
Docs](https://expo.nodejs.cn/versions/latest/sdk/ui/) to
learn more about Expo UI
</Text></VStack></Host>);}
自动更新日期
🌐 Auto-updating date
使用 date 和 dateStyle 属性来显示会随着时间推移自动更新的日期。这在小部件和实时活动中尤其有用。
🌐 Use the date and dateStyle props to display a date that automatically updates as time passes. This is especially useful in widgets and Live Activities.