NSAlert调整大小窗口 [英] NSAlert resize window

查看:337
本文介绍了NSAlert调整大小窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个带有不同文本的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆