A lightweight SwiftUI library that allows developers to dynamically color specific portions of text using hex color codes and custom formatting.
- Apply different colors to specific text segments using simple markup
- Configure custom prefix and suffix markers for coloring
- Support for hex color codes
- Control text and view alignment
- Load configuration and styled text directly from JSON
import PartialHexText
struct ContentView: View {
var body: some View {
// Apply pink color to "There" and blue color to "Kenobi"
PartialHexText(text: "Hello #{There}, General #{Kenobi}", .pink, .blue)
}
}import PartialHexText
struct CustomFormatView: View {
var body: some View {
// Using custom prefix/suffix markers
let config = PartialHexTextConfig(
prefix: "@[",
suffix: "]",
textAlignment: .center,
viewAlignment: .leading
)
PartialHexText(
text: "This is @[custom] formatting with @[different] markers",
config: config,
.red, .green
)
}
}Parse dynamic text formatting directly from a server response or configuration file:
let jsonData = """
{
"config": {
"prefix": "#(",
"suffix": ")",
"textAlignment": "leading",
"viewAlignment": "trailing"
},
"text": "#(PartialHexText) is a #(SwiftUI) library that allows #(dynamic) text coloring",
"colors": [
"#1450A3",
"#279EFF",
"#FF6969"
]
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
if let data = try? decoder.decode(PartialHexTextData.self, from: jsonData) {
PartialHexText(data: data)
}Here's how the component renders with different configurations:
// Standard usage
PartialHexText(text: "Hello #{World}, this is #{Partial} #{Hex} #{Text}",
.blue, .red, .green, .purple)
// Custom configuration
let config = PartialHexTextConfig(prefix: "*", suffix: "*")
PartialHexText(text: "Different *markers* can be *used*",
config: config,
.orange, .teal)- iOS 15.0+
