SwiftUI can't create Color by UIColor(patternImage:)

Viewed 444

I want create Color object from pattern image, but it's seem not work:

struct ContentView: View {

    var imageColor:Color{
        let image = UIImage(named: "image_name")!
        let uiColor = UIColor(patternImage: image)
        return Color(uiColor)
    }

    var body: some View {
        Text("Hello World")
            .background(imageColor)
    }
}

run the code, the Text background seem like transparent.

so I use po command in lldb to see what exactly imageColor is, and I got this:

po imageColor
▿ <UIDynamicPatternColor: 0x283b90540; image = <UIImage:0x2809a8090 named(main: bwBlocks2) {509, 300}>>
▿ provider : <ColorBox<UIColor>: 0x283b905c0>

seem like it's created right color from image, but I can't see it in Text Background!

What's going on? Is there a problem with the image size? Thanks ;)

1 Answers

If I correctly understood your goal, it can be achieved with the following

var body: some View {
    Text("Hello World")
        .background(Image("image_name").resizable(resizingMode: .tile))
}
Related