Skip to content

V8tr/AttributedStringBuilder

Repository files navigation

Article related to this project


AttributedStringBuilder

A sample project showing how to implement custom Swift function builder using NSAttributedString as an example.

Usage:

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        label.attributedText = NSAttributedString {
            NSAttributedString {
                "Folder "
                UIImage(systemName: "folder")!
            }
            
            "\n"
            
            NSAttributedString {
                "Document "
                UIImage(systemName: "doc")!.withRenderingMode(.alwaysTemplate)
            }
            .withAttributes([
                .font: UIFont.systemFont(ofSize: 32),
                .foregroundColor: UIColor.red
            ])
        }
    }
}

Result:

Swift function builders, SwiftUI ViewBuilder