Как добавить ссылку в NSTextField




#import <Cocoa/Cocoa.h>


@interface AGAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (strong) IBOutlet NSTextField *textField;

@end


@implementation AGAppDelegate

@synthesize textField;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [textField setAllowsEditingTextAttributes: YES];
    [textField setSelectable:YES];
    NSString *theHTMLString = [NSString stringWithFormat:@"<a href=\"http://apple.com\">Apple</a>"];
    [textField setAttributedStringValue:[self stringFromHTML:theHTMLString withFont:[textField font]]];
}

- (NSAttributedString *)stringFromHTML:(NSString *)aHTMLString withFont:(NSFont *)aFont
{
    if (!aFont)
    {
        aFont = [NSFont systemFontOfSize:0.0];  // Default font
    }
    aHTMLString = [NSString stringWithFormat:@"<span style=\"font-family:'%@'; font-size:%dpx;\">%@</span>",
        [aFont fontName], (int)[aFont pointSize], aHTMLString];
    NSData *theData = [aHTMLString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *theOptions = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
        NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
    NSMutableAttributedString *theResult =
    [[NSMutableAttributedString alloc] initWithHTML:theData options:theOptions documentAttributes:nil];
    return theResult;
}

@end