NSAlert resizing window

I have multiple NSAlert dialogs with different text. I want to adjust the width of the alert box for the text so that the text doesn't wrap. So I am using this code to calculate the line width:

NSSize size = [myString sizeWithAttributes:@{NSFontAttributeName: [NSFont systemFontOfSize:[NSFont systemFontSize]]}];

      

Then I try to customize the alert box:

NSAlert *alert = [NSAlert alertWithMessageText:...
...
NSPanel *panel = alert.window;
NSRect frame = panel.frame;
float x = ((NSTextField*)[[((NSPanel*)(alert.window)).contentView subviews] objectAtIndex:5]).frame.origin.x;    //the x-position of the NSTextField
frame.size.width = size.width + x;
[alert.window setFrame:frame display:YES];

      

This code works, but only the first time I call the method using this code. If I take a different line and call the method again, the window will not change (although the calculated width will differ).

Any ideas how I can resize the NSAlert window?

+3


source to share


1 answer


NSAlert can be extended by adding an additional view of sufficient width:



NSAlert *alert = [[[NSAlert alloc] init] autorelease];
alert.accessoryView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 500, 0)] autorelease];

      

+7


source







All Articles