Код: Выделить всё
public enum TextCase: Codable {
case lowercase
case uppercase
}
public struct TextCaseAttribute: CodableAttributedStringKey {
public typealias Value = TextCase
public static var name = "TextCaseAttribute"
}
public extension AttributeScopes {
struct ExtendedTextAttributes: AttributeScope {
public let textCase: TextCaseAttribute
public let foundation: FoundationAttributes
}
var customAttributes: ExtendedTextAttributes.Type { ExtendedTextAttributes.self }
}
public extension AttributeDynamicLookup {
subscript(
dynamicMember keyPath: KeyPath
) -> T {
get { self[T.self] }
}
}
< /code>
и используйте его так: < /p>
var hello: AttributedString {
var result = AttributedString("Hello")
result.textCase = .uppercase
result.font = .largeTitle
return result
}
Код: Выделить всё
po String(decoding: JSONEncoder().encode(hello), as: UTF8.self)
->
"[\"Hello\",{\"SwiftUI.Font\":{}}]"
Подробнее здесь: https://stackoverflow.com/questions/755 ... es-to-json
Мобильная версия