Lup*_*rus 3 cocoa objective-c nsalert
我有几个带有不同文本的NSAlert对话框.我想将警报窗口宽度调整为文本,以便文本不会换行.因此我使用此代码来计算字符串的宽度:
NSSize size = [myString sizeWithAttributes:@{NSFontAttributeName: [NSFont systemFontOfSize:[NSFont systemFontSize]]}];
Run Code Online (Sandbox Code Playgroud)
然后我尝试调整警报的窗口:
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];
Run Code Online (Sandbox Code Playgroud)
这段代码有效,但我第一次用这段代码调用方法.如果我再取一个字符串并再次调用该方法,则窗口不会调整大小(尽管计算出的宽度不同).
任何想法,我如何调整NSAlert窗口的大小?
小智 9
通过添加足够宽度的附件视图可以扩展NSAlert:
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
alert.accessoryView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 500, 0)] autorelease];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2165 次 |
| 最近记录: |