Old Label text remains in some instances

I have a NSlabel that changes text, font and colour periodically.

Occasionally the old text will remain in the label a bit like a watermark. The new text will be displayed on top.

I think old label text is remaining in the memory somehow, I have tried setting the label back to empty before adding the new text but that didn’t stop the problem.

any help would be greatly appreciated.
Is it Swift UIKit or SwiftUI ?

Did you try calling
Code Block
func setNeedsDisplay(NSRect)

on the frame of the label ?
Hello,

Thanks for your response.
Im using SwiftUI and I have called that function with the below line of code.

   view.setNeedsDisplay(viewSize)

where viewSize is a NSRect.

I think maybe it has something to do with the text colour as I am using a 'patterImage'. Maybe it's possible that the old patternImage is remaining in memory in some instances however im not sure how I can isolate that. It happens maybe one in 50 times the text is changed.
Could you show the code where you set patternImage ?

Also look here to see if replacing with .tile solves the problem
https://stackoverflow.com/questions/60749545/swiftui-cant-create-color-by-uicolorpatternimage

let gradientNumber = Int.random(in: 0..<gradients.count)
let selectedGradient = gradients[gradientNumber]
selectedGradient?.size = .init(width: screenWidth, height: screenHeight)

label.textColor = NSColor (patternImage: selectedGradient!)
view.setNeedsDisplay(viewSize)

This is the code that applies the text colour, .tile wouldn't work properly because it's a gradient not a flat colour.


cheers.
Old Label text remains in some instances
 
 
Q