R.swift: SwiftUI support?

Created on 28 Jan 2020  路  6Comments  路  Source: mac-cain13/R.swift

Hi,

I am wondering if there are plans to migrate the returned resources into SwiftUI defaults: Color instead of UIColor, Image instead of UIImage and so on...

Thanks.

enhancement

Most helpful comment

I built my own extensions for some SwiftUI compatibility of R.swift:

import Rswift
import SwiftUI

extension FontResource {
    func font(size: CGFloat) -> Font {
        Font.custom(fontName, size: size)
    }
}

extension ColorResource {
    var color: Color {
        Color(name)
    }
}

extension StringResource {
    var localizedStringKey: LocalizedStringKey {
        LocalizedStringKey(key)
    }

    var text: Text {
        Text(localizedStringKey)
    }
}

extension ImageResource {
    var image: Image {
        Image(name)
    }
}

Strings - of course - can also be used without the extension, but I noticed that XCode preview will only show properly localized strings when using the SwiftUI APIs so I also use the extension for that.

Usage:

let font = R.font.myFunnyFontBold.font(size: 35)
let color = R.color.listItemTitle.color
let text = R.string.localizable.hello_world.text
let image = R.image.random_image.image

All 6 comments

Hi,

Some news about SwiftUI support ?

Thanks

I built my own extensions for some SwiftUI compatibility of R.swift:

import Rswift
import SwiftUI

extension FontResource {
    func font(size: CGFloat) -> Font {
        Font.custom(fontName, size: size)
    }
}

extension ColorResource {
    var color: Color {
        Color(name)
    }
}

extension StringResource {
    var localizedStringKey: LocalizedStringKey {
        LocalizedStringKey(key)
    }

    var text: Text {
        Text(localizedStringKey)
    }
}

extension ImageResource {
    var image: Image {
        Image(name)
    }
}

Strings - of course - can also be used without the extension, but I noticed that XCode preview will only show properly localized strings when using the SwiftUI APIs so I also use the extension for that.

Usage:

let font = R.font.myFunnyFontBold.font(size: 35)
let color = R.color.listItemTitle.color
let text = R.string.localizable.hello_world.text
let image = R.image.random_image.image

@apascual @addel I posted a PR to support this in November. Until it is merged you can set your dependency to https://github.com/apptekstudios/R.swift.Library 馃憤

Any update on this?

I built my own extensions for some SwiftUI compatibility of R.swift:

import Rswift
import SwiftUI

extension FontResource {
    func font(size: CGFloat) -> Font {
        Font.custom(fontName, size: size)
    }
}

extension ColorResource {
    var color: Color {
        Color(name)
    }
}

extension StringResource {
    var localizedStringKey: LocalizedStringKey {
        LocalizedStringKey(key)
    }

    var text: Text {
        Text(localizedStringKey)
    }
}

extension ImageResource {
    var image: Image {
        Image(name)
    }
}

Strings - of course - can also be used without the extension, but I noticed that XCode preview will only show properly localized strings when using the SwiftUI APIs so I also use the extension for that.

Usage:

let font = R.font.myFunnyFontBold.font(size: 35)
let color = R.color.listItemTitle.color
let text = R.string.localizable.hello_world.text
let image = R.image.random_image.image

can you provide an example

@tomlokhorst + @mac-cain13 Any updates on merging https://github.com/mac-cain13/R.swift.Library/pull/37 ? Seeems like a pretty worthy and straight forward addition.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Przemyslaw-Wosko picture Przemyslaw-Wosko  路  4Comments

jalone87 picture jalone87  路  6Comments

appcelerate picture appcelerate  路  6Comments

tomlokhorst picture tomlokhorst  路  3Comments

theamorn picture theamorn  路  4Comments