【问题标题】:Accessory view in NSAlert doesn't render text with proper sizeNSAlert 中的附件视图无法以适当的大小呈现文本
【发布时间】:2013-01-31 00:27:51
【问题描述】:

我正在尝试显示带有附件视图的NSAlert,以便可以在信息性消息下方的文本块中显示链接。这是我的代码。

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSAlert *activateAlert = [NSAlert alertWithMessageText: @"Some message text"
                                         defaultButton: @"OK"
                                       alternateButton: nil
                                           otherButton: nil
                             informativeTextWithFormat: @"Some informative text"];

    NSTextView *accessory = [[NSTextView alloc] initWithFrame: NSMakeRect(0,0,300,15)];
    NSFont *font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
    NSDictionary *textAttributes = @{NSFontAttributeName: font};
    [accessory insertText:[[NSAttributedString alloc] initWithString:@"Some text in an accessory view"
                                                      attributes: textAttributes]];
    accessory.editable = NO;
    accessory.drawsBackground = NO;
    [accessory setAutomaticLinkDetectionEnabled: YES];

    activateAlert.accessoryView = accessory;

    [activateAlert beginSheetModalForWindow: self.window
                              modalDelegate: nil
                             didEndSelector: nil
                                contextInfo: nil];
}

@end

Apple documentation 表示“信息性文本(使用小系统字体)”,所以我使用的是[NSFont smallSystemFontSize],但它无法正确呈现(see):

  • 未对齐
  • 它没有使用小字体(我尝试使用其他值,例如 1.0),但似乎忽略了字体属性。

有什么提示吗?我应该创建自己的NSAlert 组件吗?

谢谢!

【问题讨论】:

  • 这对我来说似乎是一个错误。我可以更改字体,但不能更改大小。
  • 对我来说也一样。更改颜色也很有效。

标签: objective-c font-size nsalert


【解决方案1】:

您是否尝试过为 textView 创建一个 IBOutlet 并将其用作 NSAlert 的辅助视图?

【讨论】:

  • 我尝试了您的解决方案并且它有效,所以我尝试使用它的字体属性将字体直接设置为 NSTextView,现在它可以工作了......关于左边的差距有什么想法吗? (here
猜你喜欢
  • 2017-08-02
  • 1970-01-01
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
  • 2013-11-23
相关资源
最近更新 更多