Forked from christianselig/SwiftAttributedStrings.swift
Created
November 15, 2022 17:43
-
-
Save yspreen/a7b3dc9857e0c98e81b59a4f47580b5e to your computer and use it in GitHub Desktop.
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
// | |
// ContentView.swift | |
// | |
// Created by @spreen_co on 11/13/22. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
var body: some View { | |
Text(attributedString) | |
} | |
var attributedString: AttributedString { | |
var attributes = AttributeContainer() | |
let paragraphStyle = NSMutableParagraphStyle() | |
paragraphStyle.lineSpacing = 20 | |
attributes.paragraphStyle = paragraphStyle | |
var attributedString = AttributedString("Once upon a time there was a duck who lived in a pond and the duck's name was Henry", attributes: attributes) | |
let duckRange = attributedString.range(of: "duck")! | |
attributedString[duckRange].foregroundColor = .white | |
attributedString[duckRange].backgroundColor = .green | |
return attributedString | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
.previewLayout(.fixed(width: 200.0, height: 200.0)) | |
} | |
} | |
func Text(_ content: AttributedString) -> some View { | |
SwiftUI.Text(content) | |
.lineSpacing(content.paragraphStyle?.lineSpacing ?? 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment