R.swift icon indicating copy to clipboard operation
R.swift copied to clipboard

SwiftUI support?

Open apascual opened this issue 4 years ago • 11 comments

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.

apascual avatar Jan 28 '20 09:01 apascual

Hi,

Some news about SwiftUI support ?

Thanks

addel avatar Apr 12 '20 09:04 addel

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

schroepf avatar Apr 16 '20 07:04 schroepf

@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 👍

apptekstudios avatar Apr 24 '20 10:04 apptekstudios

Any update on this?

philipbel avatar Jun 22 '20 21:06 philipbel

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

sreeram780 avatar Jul 24 '20 13:07 sreeram780

@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.

scaio avatar Sep 23 '20 22:09 scaio

Hello @mac-cain13 ! Could we please have an update on this?

ZsoltMolnarMBH avatar Nov 08 '21 20:11 ZsoltMolnarMBH

@schroepf @apptekstudios

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

    var text: Text {
        Text(localizedStringKey)
    }
}

This gives back the key name for me, rather than the key value..

Plus this extension doesn't work with localised strings with parameters.. eg - "test1" = "%@-test1";

kunalverma25 avatar Nov 09 '21 06:11 kunalverma25

The above changes don't work as it is with some assets in libraries/pods if you use R.Swift in those... Minor changes are needed for that (add bundle)..

public extension ColorResource {
    var color: Color {
        Color(name, bundle: bundle)
    }
}

public extension ImageResource {
    var image: Image {
        Image(name, bundle: bundle)
    }
}

kunalverma25 avatar Nov 17 '21 05:11 kunalverma25

Any Update on SwiftUI Support

learnwithgabbar avatar Feb 08 '22 05:02 learnwithgabbar

For strings no need to extend StringResource

This works out of the box Text(R.string.localizable.some_string_key())

ahbou avatar May 17 '22 15:05 ahbou