pointfreeco / swift-snapshot-testing

📸 Delightful Swift snapshot testing.
https://www.pointfree.co/episodes/ep41-a-tour-of-snapshot-testing
MIT License
3.8k stars 580 forks source link

SiwftUI views with custom fonts produce incorrect screenshots #341

Open amuresia opened 4 years ago

amuresia commented 4 years ago

Hi everyone! I don't know if I am doing something wrong but when I take a screenshot it looks like this: image

After a few hours of investigating, I came to the conclusion that custom colours are not visible in the screenshot. I have a view like the following:

struct CircularSpinner: View {
    @State var spinCircle = false

    var body: some View {
        ZStack {
            Circle()
                .trim(from: 0.5, to: 1)
                struct CircularSpinner: View {
    @State var spinCircle = false

    var body: some View {
        ZStack {
            Circle()
                .trim(from: 0.5, to: 1)
                .stroke(Color("red"), lineWidth:4)
                .frame(width:60)
                .rotationEffect(.degrees(spinCircle ? 0 : -360), anchor: .center)
                .animation(Animation.linear(duration: 1).repeatForever(autoreverses: false))
        }
        .onAppear {
            self.spinCircle = true
        }
        .onDisappear() {
            self.spinCircle = false
        }
    }
}
                .frame(width:60)
                .rotationEffect(.degrees(spinCircle ? 0 : -360), anchor: .center)
                .animation(Animation.linear(duration: 1).repeatForever(autoreverses: false))
        }
        .onAppear {
            self.spinCircle = true
        }
        .onDisappear() {
            self.spinCircle = false
        }
    }
}

Please note .stroke(Color("red"), lineWidth:4). If I change this to use a system colour i.e. .stroke(Color.red, lineWidth:4) then the same test produces the following screenshot (which is a bit close to what I would expect): image

My app also has some views that are stacked on top of other views, some of them using the .offset modifier, and I don't believe that is picked up either i.e. in the screenshots the 3 (out of 4) visible icons should be at the bottom as they are part of a custom nav bar. I can probably live with this as long as we have a working solution for custom colours.

PS: In my assets catalogue, the custom colour is targeting both the app and tests targets.

dorsafamara50 commented 3 years ago

I have the same problem when taking a screen shot of a viewController that contains a custom button with a font. when I delete the line of setting the font (titleLabel?.font = titleFont), the screen shot is correct.