R.swift
R.swift copied to clipboard
SwiftUI support?
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.
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.
Hello @mac-cain13 ! Could we please have an update on this?
@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";
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)
}
}
Any Update on SwiftUI Support
For strings no need to extend StringResource
This works out of the box
Text(R.string.localizable.some_string_key())