[aString drawAtPoint:withAttributes:] Works in High Sierra, does not in Mojave - Why?


Markus Ruggiero
 

I have written a small Cocoa program that works in High Sierra but stopped working in Mojave. The program loads a graphics file from disk and displays it full screen. At the bottom it should write the filename. The picture is shown, the rect for the filename display is properly painted in dark grey, but there is no filename shown. I did upgrade my rMBP the other day from High Sierra to Mojave and since then the filename is gone. I ran the app from Xcode and verified with the debugger that the infoString is properly set. So something must be wrong with the way I draw the string. But what? Mind you I am not a Cocoa nor an Obj-C programmer. This little app is just something to play with and learn.

This method resides inside MyCustomView.m file and is supposed to draw the infoString at the bottom of the screen inside a darkGray rectangle. The rectangle is 25 pxels high an stretches across the bottom of the screen.

- (void)drawInfo: (NSString*)infoString inRect:(NSRect)infoRect {

[[NSColor darkGrayColor] set];
NSRectFill(infoRect);

NSMutableDictionary *attrs = [[NSMutableDictionary alloc] init];
[attrs setObject: [NSColor whiteColor] forKey: NSForegroundColorAttributeName];
[self lockFocus];
[infoString drawAtPoint:NSMakePoint(20.0, 4.0) withAttributes:attrs];
[self unlockFocus];
}

Any idea?
Thanks a lot
---markus---


Gary L. Wade
 

There are a lot of things that need to be done to do drawing right, and any drawing should be done from within a view’s drawRect: method or eventually called by it. Once a view’s drawRect: method is called, everything needed to be done to set up drawing has been done. For simple text drawing, it’s often best to just use a non-editable NSTextField placed where you want it and filled with the attributes and text you want to show, and when the frameworks are ready, they will draw your text for you.
--
Gary L. Wade
http://www.garywade.com/

On Nov 1, 2019, at 6:28 PM, Markus Ruggiero <mailinglists@...> wrote:


I have written a small Cocoa program that works in High Sierra but stopped working in Mojave. The program loads a graphics file from disk and displays it full screen. At the bottom it should write the filename. The picture is shown, the rect for the filename display is properly painted in dark grey, but there is no filename shown. I did upgrade my rMBP the other day from High Sierra to Mojave and since then the filename is gone. I ran the app from Xcode and verified with the debugger that the infoString is properly set. So something must be wrong with the way I draw the string. But what? Mind you I am not a Cocoa nor an Obj-C programmer. This little app is just something to play with and learn.

This method resides inside MyCustomView.m file and is supposed to draw the infoString at the bottom of the screen inside a darkGray rectangle. The rectangle is 25 pxels high an stretches across the bottom of the screen.

- (void)drawInfo: (NSString*)infoString inRect:(NSRect)infoRect {

[[NSColor darkGrayColor] set];
NSRectFill(infoRect);

NSMutableDictionary *attrs = [[NSMutableDictionary alloc] init];
[attrs setObject: [NSColor whiteColor] forKey: NSForegroundColorAttributeName];
[self lockFocus];
[infoString drawAtPoint:NSMakePoint(20.0, 4.0) withAttributes:attrs];
[self unlockFocus];
}

Any idea?
Thanks a lot
---markus---


Sak Wathanasin
 



On 1 Nov 2019, at 22:56, Markus Ruggiero <mailinglists@...> wrote:

I have written a small Cocoa program that works in High Sierra but stopped working in Mojave

Try removing the lockfocus/unlockficus - I had to remove them from all my custom widgets to get them to work in 10.14.

Regards
Sak


Markus Ruggiero
 

Thanks, that was it.

Just for the peace of my mind ... why?

---markus---

On 2 Nov 2019, at 09:24, Sak Wathanasin <sw@...> wrote:



On 1 Nov 2019, at 22:56, Markus Ruggiero <mailinglists@...> wrote:

I have written a small Cocoa program that works in High Sierra but stopped working in Mojave

Try removing the lockfocus/unlockficus - I had to remove them from all my custom widgets to get them to work in 10.14.

Regards
Sak



Sak Wathanasin
 


On 02 Nov 2019, at 13:20, Markus Ruggiero wrote:

Thanks, that was it.

Just for the peace of my mind ... why?


I'd love to know too: I discovered it "emprically", as they say; i.e. much hair-pulling, swearing and cursing was involved. Lock/unlockfocus was the recommended way of doing things and our legacy app was littered with them. Oh, it's building with the newer SDK that triggers the isuue - had you built your app with, say, Xcode 9, it would have been OK on 10.14+ (you might have other issues, of course).

Regards
Sak