Created
September 2, 2024 11:24
-
-
Save vurgunmert/a393d750cc1f475a452ac5ba16e4359d to your computer and use it in GitHub Desktop.
SwiftUI Label Text tvOS Storyboard MedienMonster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SwiftUILabelStoryboard.swift | |
// TemplateAppTvOS | |
// | |
// Created by Mert Vurgun on 02.09.2024. | |
// | |
import SwiftUI | |
struct SwiftUILabelStoryboard: View { | |
var body: some View { | |
VStack(spacing: 40) { | |
// Top horizontal stack with Text examples | |
HStack(spacing: 40) { | |
createSimpleTextStack() | |
createAttributedTextStack() | |
} | |
// Stack with different text styles | |
createAttributedTextAndHTMLStack() | |
} | |
.padding() | |
.background(Color(.darkGray)) | |
} | |
// Simple Text with different font styles | |
private func createSimpleTextStack() -> some View { | |
VStack(alignment: .center, spacing: 10) { | |
Text("SwiftUI Text") | |
.font(.system(size: 20, weight: .bold)) | |
.foregroundColor(.green) | |
Text("Headline") | |
.font(.system(size: 30, weight: .bold)) | |
Text("Subheading") | |
.font(.system(size: 24, weight: .regular)) | |
Text("Display") | |
.font(.system(size: 34, weight: .bold)) | |
Text("Body text") | |
.font(.system(size: 20, weight: .regular)) | |
Text("Caption text") | |
.font(.system(size: 16, weight: .regular)) | |
.foregroundColor(.gray) | |
} | |
} | |
// Attributed Text with different styles | |
private func createAttributedTextStack() -> some View { | |
VStack(alignment: .center, spacing: 10) { | |
let titleText = AttributedString("SwiftUI AttributedText") | |
let headlineText = AttributedString("Headline") | |
let subheadingText = AttributedString("Subheading") | |
let displayText = AttributedString("Display") | |
let bodyText = AttributedString("Body text") | |
let captionText = AttributedString("Caption text") | |
Text(titleText) | |
.font(.system(size: 20, weight: .bold)) | |
.foregroundColor(.green) | |
Text(headlineText) | |
.font(.system(size: 30, weight: .bold)) | |
Text(subheadingText) | |
.font(.system(size: 24, weight: .regular)) | |
Text(displayText) | |
.font(.system(size: 34, weight: .bold)) | |
Text(bodyText) | |
.font(.system(size: 20, weight: .regular)) | |
Text(captionText) | |
.font(.system(size: 16, weight: .regular)) | |
.foregroundColor(.gray) | |
} | |
} | |
// Function to create custom attributed text in SwiftUI | |
private func getCustomText() -> AttributedString { | |
var attributedText = AttributedString("Attributed Text with Styled Colors") | |
if let range = attributedText.range(of: "Attributed Text") { | |
attributedText[range].foregroundColor = .red | |
attributedText[range].font = .system(size: 24, weight: .bold) | |
} | |
if let range = attributedText.range(of: "Styled") { | |
attributedText[range].foregroundColor = .blue | |
attributedText[range].font = .italicSystemFont(ofSize: 20) | |
} | |
if let range = attributedText.range(of: "Colors") { | |
attributedText[range].foregroundColor = .green | |
attributedText[range].font = .system(size: 24) | |
} | |
return attributedText | |
} | |
// Demo of attributed text with color, size, and HTML conversion | |
private func createAttributedTextAndHTMLStack() -> some View { | |
HStack(spacing: 20) { | |
Text("Simple Text with Custom Color") | |
.font(.system(size: 20)) | |
.foregroundColor(.blue) | |
.frame(maxWidth: .infinity) | |
// AttributedText with multiple styles | |
Text(getCustomText()) | |
.frame(maxWidth: .infinity) | |
HTMLTextView(htmlText: """ | |
<h1 style="font-size: 36px;">This is an H1 header</h1> | |
<h2 style="font-size: 30px;">This is an H2 header</h2> | |
<p style="font-size: 24px;">This is a paragraph with <b>bold</b> and <i>italic</i> text.</p> | |
<ul style="font-size: 20px;"> | |
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li> | |
<li>Aliquam tincidunt mauris eu risus.</li> | |
</ul> | |
""") | |
.frame(maxWidth: .infinity) | |
} | |
} | |
} | |
struct HTMLTextView: UIViewRepresentable { | |
var htmlText: String | |
func makeUIView(context: Context) -> UILabel { | |
let label = UILabel() | |
label.numberOfLines = 0 | |
label.textAlignment = .left | |
if let htmlAttributedString = try? NSAttributedString( | |
data: Data(htmlText.utf8), | |
options: [.documentType: NSAttributedString.DocumentType.html, | |
.characterEncoding: String.Encoding.utf8.rawValue], | |
documentAttributes: nil) { | |
label.attributedText = htmlAttributedString | |
} else { | |
label.text = "" | |
} | |
return label | |
} | |
func updateUIView(_ uiView: UILabel, context: Context) { | |
// updates as needed | |
} | |
} | |
struct SwiftUILabelStoryboard_Previews: PreviewProvider { | |
static var previews: some View { | |
SwiftUILabelStoryboard() | |
.previewLayout(.sizeThatFits) | |
.padding() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment