Swift Swift - 2 months ago 6
Swift Question

Is there a text label I can use for CGPoint in macOS app (Swift)?

Could you help me to find a way to attach a text label to CGPoint for macOS cocoa app? I need a similar approach as label.text in iOS.


You'll need to set the frame property on an instance of a NSLabel to position (and size it).

One tricky piece is that you have to ensure the label is of large enough size as to not clip the text.

let label : NSTextField = // get a static text field from IB or previous initialization
label.stringValue = // get the label's contents
label.sizeToFit() // ensures the label's bounds do not clip the text
let point = // get the CGPoint you want this to be placed at in the Cocoa coordinate plane
label.frame = CGRect(origin: point, size: CGSize(width: label.bounds.width, height: label.bounds.height))