AccessibleAuthorLabel icon indicating copy to clipboard operation
AccessibleAuthorLabel copied to clipboard

SwiftUI support?

Open TheQuinbox opened this issue 1 year ago • 3 comments

Hi, I absolutely love this thing! I hope more apps start using it, it's great! Is it possible to add a version for SwiftUI? This way, it's cross-platform, and SwiftUI seems to be the way of the future, despite the bugs.

TheQuinbox avatar Mar 04 '23 20:03 TheQuinbox

PRs are welcome! Wouldn't be super hard to add and could use the basis of the UILabel implementation for a SwiftUI Label one.

christianselig avatar Mar 04 '23 20:03 christianselig

How would you like me to structure this? Put it in the same folder? A different one? How about testing? You don't seem to have a testing infra, so should I just create an app and somehow test it outside this repo? apologies, I'm still fairly new to the Swift ecosystem. I'm thinking something like:

struct AccessibleLabel: View {
    let text: String
    
    static let characterMapping: [String: String] = {
        guard let plistURL = Bundle.module.url(forResource: "mapping", withExtension: "plist") else { return [:] }
        guard let mapping = NSDictionary(contentsOf: plistURL) as? [String: String] else { return [:] }
        return mapping
    }()
    
    var body: some View {
        Text(text.map { Self.characterMapping[$0.lowercased()] ?? "" }.joined())
            .accessibilityLabel(text)
    }
}

Another idea I had is to make a View extension, and have .accessibleLabel be a modifier that can be applied, but the issue with that is that you can't have static properties inside extensions, at least I don't think you can.

TheQuinbox avatar Mar 04 '23 23:03 TheQuinbox

Yeah that's totally perfect! This repo is mostly for academic purposes more than anything :)

christianselig avatar Mar 04 '23 23:03 christianselig