// Real styled text, not editable: a theme's sample in the settings, drawn by // the same styling the editor uses, from the spans the core answered. #if canImport(UIKit) import SwiftUI import UIKit @MainActor public func writingAttributed(_ text: String, styled: WritingStyled, look: WritingLook) -> NSAttributedString { let storage = NSTextStorage(string: text, attributes: look.base) var styler = WritingStyler(look: look) styler.blockKeys = WritingBlockKey.keys(styled) styler.style(storage, styled, in: NSRange(location: 0, length: storage.length)) // A label draws no chips behind words: a chip is its tint. storage.enumerateAttribute(.writingChip, in: NSRange(location: 0, length: storage.length)) { value, range, _ in if let chip = value as? UIColor { storage.addAttribute(.backgroundColor, value: chip, range: range) } } // A label draws no glyphs either: a bullet or a box is drawn by the // editor's layout, so here its marks become the symbol itself, or the // sample's checklist showed struck words with no box (launch-ios walk, // 22 Nov 2026). var marks: [(NSRange, String)] = [] storage.enumerateAttribute(.writingGlyph, in: NSRange(location: 0, length: storage.length)) { value, range, _ in if let glyph = value as? String { marks.append((range, glyph)) } } for (range, glyph) in marks.reversed() { let font = (storage.attribute(.font, at: range.location, effectiveRange: nil) as? UIFont) ?? look.body storage.replaceCharacters(in: range, with: writingSampleMark(glyph, font: font, look: look)) } return storage } /// A list mark as a label draws it: the symbol and a space, in the theme's colours. @MainActor func writingSampleMark(_ glyph: String, font: UIFont, look: WritingLook) -> NSAttributedString { let symbol: String? = switch glyph { case "box": "square" case "ticked": "checkmark.square" case "bullet": "circle.fill" default: nil } guard let symbol else { return NSAttributedString() } let side = glyph == "bullet" ? font.pointSize * 0.35 : font.pointSize * 1.05 let tint = glyph == "bullet" ? look.theme.accent : look.theme.dim let attachment = NSTextAttachment() attachment.image = UIImage(systemName: symbol)?.withTintColor(tint, renderingMode: .alwaysOriginal) attachment.bounds = CGRect(x: 0, y: (font.capHeight - side) / 2, width: side, height: side) let mark = NSMutableAttributedString(attachment: attachment) mark.append(NSAttributedString(string: " ", attributes: [.font: font])) return mark } /// A sample of styled text on its theme's ground. struct WritingSample: UIViewRepresentable { let text: NSAttributedString let lines: Int public init(_ text: NSAttributedString, lines: Int = 4) { self.text = text self.lines = lines } func makeUIView(context: Context) -> UILabel { let label = UILabel() label.numberOfLines = lines label.lineBreakMode = .byTruncatingTail label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) label.setContentHuggingPriority(.required, for: .vertical) return label } func updateUIView(_ label: UILabel, context: Context) { label.attributedText = text } func sizeThatFits(_ proposal: ProposedViewSize, uiView: UILabel, context: Context) -> CGSize? { let width = proposal.width ?? 280 let fitted = uiView.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude)) return CGSize(width: width, height: fitted.height) } } #endif