0

I'm trying to display an NSAlert with an accessory view so I can show a link in a block of text below the informative message. Here is my code.

#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 says "the informative text (which uses small system font)", so I'm using [NSFont smallSystemFontSize] but it doesn't render properly (see):

  • It's not aligned
  • It's not using a small font size (I've tried using other values, like 1.0) but it seems the font attribute is ignored.

Any hints? Should I create my own NSAlert component?

Thanks!

DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
Romain Pouclet
  • 864
  • 2
  • 8
  • 17

1 Answers1

1

Have you tried creating an IBOutlet for the textView and using that as an accessory view for the NSAlert?

Shashank
  • 1,743
  • 1
  • 14
  • 20
  • I tried your solution and it worked, so I tried setting the font directly to the NSTextView using its font property and now it works... Any idea about the gap on the left ? ([here](http://d.pr/i/fVPW]) – Romain Pouclet Feb 15 '13 at 13:48