Text và Image
1. Text
Phần tiêu đề “1. Text”Cú pháp cơ bản
Phần tiêu đề “Cú pháp cơ bản”Text("Hello, SwiftUI!")Font và Style
Phần tiêu đề “Font và Style”Text("Title") .font(.largeTitle)
Text("Headline") .font(.headline)
Text("Body text") .font(.body)
Text("Caption") .font(.caption)
// Custom fontText("Custom") .font(.system(size: 24, weight: .bold, design: .rounded))
Text("SF Mono") .font(.system(.body, design: .monospaced))Text Modifiers
Phần tiêu đề “Text Modifiers”Text("Styled Text") .font(.title) .fontWeight(.bold) .foregroundColor(.blue) .italic() .underline() .strikethrough()Multiline Text
Phần tiêu đề “Multiline Text”Text("This is a very long text that will wrap to multiple lines automatically in SwiftUI") .lineLimit(3) // Giới hạn số dòng .truncationMode(.tail) // Cắt ở cuối .multilineTextAlignment(.center)Text với Formatting
Phần tiêu đề “Text với Formatting”// DateText(Date(), style: .date)Text(Date(), style: .time)Text(Date(), format: .dateTime.day().month().year())
// NumberText(42.5, format: .number.precision(.fractionLength(2)))Text(1234.56, format: .currency(code: "USD"))
// Attributed StringText("Hello ") + Text("World").bold().foregroundColor(.red)Markdown Support
Phần tiêu đề “Markdown Support”Text("This is **bold** and this is *italic*")Text("Click [here](https://apple.com)")Text("Code: `print()`")2. Image
Phần tiêu đề “2. Image”Image từ Assets
Phần tiêu đề “Image từ Assets”Image("photo") // Tên trong Assets.xcassets .resizable() .aspectRatio(contentMode: .fit) .frame(width: 200, height: 200)SF Symbols
Phần tiêu đề “SF Symbols”Image(systemName: "heart.fill") .font(.largeTitle) .foregroundColor(.red)
Image(systemName: "star.fill") .imageScale(.large) .symbolRenderingMode(.multicolor)Image Modifiers
Phần tiêu đề “Image Modifiers”Image("landscape") .resizable() // Cho phép resize .aspectRatio(contentMode: .fill) // fill hoặc fit .frame(width: 300, height: 200) .clipped() // Cắt phần thừa .cornerRadius(16)Scaling Modes
Phần tiêu đề “Scaling Modes”// Fit: giữ tỷ lệ, fit trong frameImage("photo") .resizable() .scaledToFit() .frame(width: 200, height: 200)
// Fill: giữ tỷ lệ, fill hết frameImage("photo") .resizable() .scaledToFill() .frame(width: 200, height: 200) .clipped()Circular Image
Phần tiêu đề “Circular Image”Image("avatar") .resizable() .aspectRatio(contentMode: .fill) .frame(width: 100, height: 100) .clipShape(Circle()) .overlay(Circle().stroke(.white, lineWidth: 4)) .shadow(radius: 10)AsyncImage (iOS 15+)
Phần tiêu đề “AsyncImage (iOS 15+)”AsyncImage(url: URL(string: "https://example.com/image.jpg")) { phase in switch phase { case .empty: ProgressView() case .success(let image): image .resizable() .aspectRatio(contentMode: .fit) case .failure: Image(systemName: "photo") .foregroundColor(.gray) @unknown default: EmptyView() }}.frame(width: 200, height: 200)3. Kết hợp Text và Image
Phần tiêu đề “3. Kết hợp Text và Image”Label
Phần tiêu đề “Label”Label("Favorites", systemImage: "heart.fill")
Label { Text("Custom Label") .font(.headline)} icon: { Image(systemName: "star") .foregroundColor(.yellow)}HStack với Icon
Phần tiêu đề “HStack với Icon”HStack { Image(systemName: "person.circle.fill") .font(.title) .foregroundColor(.blue)
VStack(alignment: .leading) { Text("John Doe") .font(.headline) Text("john@example.com") .font(.caption) .foregroundColor(.gray) }}📝 Tóm tắt
Phần tiêu đề “📝 Tóm tắt”| View | Mục đích |
|---|---|
Text |
Hiển thị văn bản |
Image |
Hiển thị hình ảnh |
Label |
Kết hợp text và icon |
Text modifiers: .font(), .foregroundColor(), .bold(), .italic()
Image modifiers: .resizable(), .aspectRatio(), .frame(), .clipShape()