SwiftUI3.0 Text的控件的使用 相当于UILabel
//
// ContentView.swift
// TextControl
//
// Created by lujun on 2021/12/19.
//
import SwiftUI
struct ContentView: View {
var body: some View {
// basic
// customFont
ScrollView(.vertical, showsIndicators: true) {
// textFit
formatDisPlay
// otherStyle
// combineText
// localizedStr
}
}
//MARK: - Basic
var basic: some View {
VStack {
Text("Hello")
Text("Hello")
.foregroundColor(.red)
Text("Hello")
.font(.title)
Text("Hello")
.font(.title)
.italic()
.bold()
}
}
//MARK: - customFont
var customFont: some View {
VStack {
Text("Hello")
.font(.system(size: 30, weight: .light, design: .serif))
Text("Hello")
.font(.system(size: 30, weight: .bold, design: .monospaced))
Text("小孙")
.font(.custom("font twelve", size: 25))
}
}
//MARK: - TextFit
@State private var myTextField = ""
var textFit: some View {
VStack {
Text("To Be, or not to Be sd ree sadf weq:")
.frame(width: 100)
Text("To Be, or not to Be sd ree sadf weq:")
.frame(width: 100)
.lineLimit(1)
Text("To Be, or not to Be Be sd ree sadf")
.font(.body)
.frame(width: 200,height: 50,alignment: .leading)
.lineLimit(1)
.allowsTightening(true)
Text("To Be, or not to Be Be sd ree sadf")
.font(.body)
.frame(width: 200,height: 50,alignment: .leading)
.lineLimit(1)
.allowsTightening(false)
HStack {
Text("To Be, or not to Be Be sd ree sadf")
.lineLimit(1)
.minimumScaleFactor(0.5)
TextField("My Long Text Field",text: $myTextField)
}
VStack {
Text("To Be, or not to Be Be sd ree sadfTo Be, or not to Be Be sd ree sadfTo Be, or not to Be Be sd ree sadfTo Be, or not to Be Be sd ree sadf")
.frame(width: 150, height: 150)
.truncationMode(.tail)
Text("To Be, or not to Be Be sd ree sadfTo Be, or not to Be Be sd ree sadfTo Be, or not to Be Be sd ree sadfTo Be, or not to Be Be sd ree sadf")
.frame(width: 150, height: 150)
.truncationMode(.head)
Text("To Be, or not to Be Be sd ree sadfTo Be, or not to Be Be sd ree sadfTo Be, or not to Be Be sd ree sadfTo Be, or not to Be Be sd ree sadf")
.frame(width: 150, height: 150)
.truncationMode(.middle)
}
}
}
//MARK: - 日期格式化输出
let price = 10.341477878
let startDate = Date(timeIntervalSinceNow: -3600)
var formatDisPlay: some View {
VStack {
Text("$\(price,specifier: "%.2f")")
Text(startDate,style: .time)
Text(startDate,style: .date)
Text(startDate,style: .relative)
Text(startDate,style: .offset)
Text(startDate,style: .timer)
Text(startDate...Date())
Text(DateInterval(start: startDate, duration: 300))
}
}
//MARK: - 其他样式
var otherStyle: some View {
VStack(spacing: 20) {
Text("Hello")
.font(.largeTitle)
.strikethrough(true, color: .red)
Text("Hello")
.font(.largeTitle)
.underline(true, color: .red)
HStack {
VStack(alignment: .leading) {
Text("ABCDEFGH")
.kerning(-3)
Text("ABCDEFGH")
Text("ABCDEFGH")
.kerning(3)
}
Spacer()
VStack(alignment: .leading) {
Text("ABCDEFGH")
.tracking(-3)
Text("ABCDEFGH")
Text("ABCDEFGH")
.tracking(3)
}
}
.padding(.horizontal,60)
HStack (alignment: .top) {
Text("Hello")
.baselineOffset(-10)
.border(Color.red)
Text("Hello")
.border(Color.green)
Text("Hello")
.baselineOffset(10)
.border(Color.blue)
}
.background(Color(white: 0.9))
Text("Head")
.textCase(.uppercase)
Text("delivery on eligible Apple products in most metros. Offer is not available on customized Mac, engraved products, and for certain order types including orders paid for with financing or by bank transfer. Delivery times vary according to your selected delivery address, availability of your items, and the time of day you place your order. Find a store to view local store hours or see checkout for estimated delivery. A signature is required for delivery. Drivers may ask for verbal confirmation of receipt from a safe distance to satisfy the signature requirement")
.frame(width: 200, height: 200, alignment: .leading)
.lineSpacing(10)
.background(Color.gray)
Text("delivery on eligible Apple products in most metros. Offer is not available on customized Mac, engraved products, and for certain order types including orders paid for with financing or by bank transfer. Delivery times vary according to your selected delivery address, availability of your items, and the time of day you place your order. Find a store to view local store hours or see checkout for estimated delivery. A signature is required for delivery. Drivers may ask for verbal confirmation of receipt from a safe distance to satisfy the signature requirement")
.frame(width: 200, height: 200, alignment: .leading)
.multilineTextAlignment(.center)
Text("hello world")
.flipsForRightToLeftLayoutDirection(true)
.environment(\.layoutDirection, .rightToLeft)
}
}
//MARK: - 富文本
var combineText: some View {
Text("鲁军")
.foregroundColor(.red)
.font(.title)
+
Text("给科技加点料")
.foregroundColor(.green)
.font(.title)
.italic()
}
//MARK: - 本地化
var localizedStr: some View {
VStack {
Text("pencil")
Text(verbatim: "pencil")
Text(LocalizedStringKey("pencil"))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
分类:
swiftUI
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix