当前位置: 首页 > java >正文

SwiftUI 1.Text介绍和使用

SwiftUI 的 Text 是用于显示静态文本的核心组件,它提供了丰富的样式和布局控制。以下是 Text 的详细介绍和使用示例:


一、基础用法

1. 显示简单文本
Text("Hello, SwiftUI!")
2. 设置字体样式
Text("Hello, SwiftUI!").font(.title)          // 系统字体(如 .title, .body, .caption).fontWeight(.bold)     // 字重(.light, .medium, .black 等).italic()              // 斜体.underline()           // 下划线
3. 修改颜色和对齐
Text("Hello, SwiftUI!").foregroundColor(.blue)  // 文字颜色.multilineTextAlignment(.center) // 多行对齐方式.frame(width: 200)       // 控制布局范围

二、高级功能

1. 多行文本与截断
Text("This is a long text that might need to wrap or truncate.").lineLimit(3)                // 最多显示3行.truncationMode(.tail)        // 截断位置(.head, .middle, .tail).lineSpacing(10)              // 行间距
2. Markdown 支持(iOS 15+)
Text("**Bold Text** *Italic* [Link](https://apple.com)")// 直接渲染 Markdown
3. 本地化字符串
Text("welcome_message", tableName: "Localizable")// 使用 Localizable.strings 中的键值
4. 格式化日期、数字等
Text(Date(), style: .date)       // 显示日期(如 "June 23, 2023")
Text(100.0, format: .currency(code: "USD")) // 格式化为 "$100.00"

三、自定义样式

1. 组合不同样式的文本
Text("Hello ")+ Text("SwiftUI").foregroundColor(.red).bold()
2. 使用 AttributedString(iOS 15+)
var attributedText: AttributedString {var text = AttributedString("Custom Style")text.font = .titletext.foregroundColor = .purplereturn text
}Text(attributedText)
3. 添加阴影和边框
Text("Stylish Text").shadow(color: .gray, radius: 2, x: 1, y: 1).border(.green, width: 1)

四、与其他组件结合

1. 在布局容器中使用
VStack {Text("First Line")Text("Second Line").padding()
}
2. 结合 Image
HStack {Text("Apple Logo")Image(systemName: "applelogo")
}

五、常见问题

  1. 动态内容更新Text 会自动响应绑定的数据变化。

    @State var count = 0
    Text("Count: \(count)") // 当 count 变化时自动更新
    
  2. 性能优化:避免在频繁更新的内容中使用复杂样式。

  3. 自定义字体

    Text("Custom Font").font(.custom("AvenirNext-Regular", size: 20))
    

六、完整示例

struct ContentView: View {@State private var counter = 0var body: some View {VStack(spacing: 20) {Text("Welcome!").font(.largeTitle).foregroundColor(.indigo)Text("You have \(counter) items").font(.body).foregroundColor(.secondary)Button("Add") { counter += 1 }}}
}

通过灵活组合这些功能,你可以轻松实现从简单标签到复杂富文本的展示需求。

http://www.xdnf.cn/news/1503.html

相关文章:

  • Elasticsearch 报错 Limit of total fields [1000] has been exceeded
  • SwiftUI 3.Button介绍和使用
  • Python爬虫学习:高校数据爬取与可视化
  • UIAutomator 与 Playwright 在 AI 自动化中的界面修改对比
  • Java学习手册:Web 安全基础
  • MyBatis 升级至 MyBatis-Plus 详细步骤
  • 常用嵌入式软件代码编码规范的关系和覆盖
  • 海康NVR配置NAS-TrueNAS
  • Mysql 简单数据查询
  • 知识储备-后仿
  • AtCoder Beginner Contest 402题解
  • Pillow库中的convert(“L“)彩色图像转换灰度图像详解~
  • 2026《数据结构》考研复习笔记六(串的KMP算法)
  • 【网工第6版】第5章 网络互联⑥
  • 《MySQL:MySQL表的内外连接》
  • 【Redis】redis主从哨兵
  • MySQL常见问题解答
  • 【异常解决】Spring Boot 返回排序后的 Map 但前端接收顺序不对的解决方案
  • C++类与继承
  • SpringBoot中6种自定义starter开发方法
  • 同z科技面经
  • Python爬虫第18节-动态渲染页面抓取之Splash使用上篇
  • Sci期刊的编辑会对投稿论文进行查重吗?
  • 【深度学习与大模型基础】第13章-什么是机器学习
  • CSGO 盲盒开箱系统技术实现深度解析
  • Spring Boot + MyBatis 动态字段更新方法
  • ToDesk远程开机设置指南(适用于HP台式机)
  • 网络安全零基础培训 L1-7 Web基础和CSS渲染
  • C++入门小馆: 探寻vector类
  • 配置 Nginx 的 HTTPS