NSAlert调整大小窗口 [英] NSAlert resize window
问题描述
我有几个带有不同文本的NSAlert对话框.我想将警报窗口的宽度调整为文本,以使文本不会自动换行.因此,我使用以下代码来计算字符串的宽度:
I have several NSAlert dialogs with different text. 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).
任何想法,如何调整NSAlert窗口的大小?
Any ideas, how I can resize the NSAlert window?
推荐答案
可以通过添加足够宽度的附件视图来扩展NSAlert:
An NSAlert can be widened by adding an accessory view of sufficient width:
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
alert.accessoryView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 500, 0)] autorelease];
这篇关于NSAlert调整大小窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!