简体   繁体   中英

Weird font behavior on the NSTextField

I added programmatically NSTextField to my NSView:

NSTextField *projectLabel = [[NSTextField alloc] initWithFrame:frame];
[projectLabel setStringValue:@"projectName"];
[projectLabel setBezeled:NO];
[projectLabel setDrawsBackground:NO];
[projectLabel setEditable:NO];
[projectLabel setSelectable:NO];

[projectLabel setFont:[NSFont controlContentFontOfSize:13]];
projectLabel.autoresizingMask = NSViewMaxXMargin | NSViewMinYMargin; 

[self addSubview:projectLabel];
[self setAutoresizesSubviews:NO];

This field was added correctly, but when I change size of view (or even move window to second display), font on field changes very weird (see attached image).

on start 之前
after change of the size 后
I do not know what I did wrong

I drew this label on drawRect every time, when the size changes.

So, you're manually telling the field to display in its parent view's drawRect: ?

Don't do that. It's a subview, so it'll get told to draw in its turn anyway. Just let that happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM