简体   繁体   English

NSAlert调整窗口大小

[英]NSAlert resize window

I have several NSAlert dialogs with different text. 我有几个带有不同文本的NSAlert对话框。 I want to adjust the alert window width to the text, so that the text don't wrap. 我想将警报窗口宽度调整为文本,以便文本不会换行。 Therefore I use this code to calculate the width of the string: 因此我使用此代码来计算字符串的宽度:

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

Then I try to adjust the window of the alert: 然后我尝试调整警报的窗口:

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 just for the first time, I call the method with this code. 这段代码有效,但我第一次用这段代码调用方法。 If I take another string and call the method again, the window will not resize (although the calculated width differentiate). 如果我再取一个字符串并再次调用该方法,则窗口不会调整大小(尽管计算出的宽度不同)。

Any ideas, how I can resize the NSAlert window? 任何想法,我如何调整NSAlert窗口的大小?

An NSAlert can be widened by adding an accessory view of sufficient width: 通过添加足够宽度的附件视图可以扩展NSAlert:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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