6

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 Answer 1

3

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))
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes,you are right! I know it's can be worked! But I still want to know My Question's Answer,Thanks
@hopy, theoretically it should work, so as it does not, confirm for Xcode 11.2, - submit feedback to Apple.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.