NSTextFieldにハイパーリンクを設定する


概要

  • メッセージとハイパーリンク付きのテキストを表示するだけのWindowを作成する
  • このウィンドウのxib及びコントローラの作成する

実装

HyperLinkWindowController.xib

  • ラベル2つとButtonを貼り付けただけの簡単なUI
  • 1View-1Controllerの原則により、後述の通りWindowControllerを定義していく。

HyperLinkWindowController.h

- (id)initWithMessage:(NSString *)message hyperLink:(NSString *)hyperLink;
  • init関数のみ公開する
  • その他は外部クラスに知らせる必要はないので、実装ファイルのinterfaceに記載する

HyperLinkWindowController.m

init関数

- (id)initWithMessage:(NSString *)message hyperLink:(NSString *)hyperLink {
    if (self = [super initWithWindowNibName:[self className] owner:self]) {
        _message = message;
        _hyperLink = hyperLink;
    }
    return self;
}

ハイパーリンクの設定

コード全体は以下の通り。

/**
 @brief ラベルを更新する
 */
- (void)updateLabels {
    // 本文の設定
    [_messageLabel setStringValue:_message];
    // ハイパーリンクの設定
    [_hyperLinkLabel setAllowsEditingTextAttributes: YES];
    [_hyperLinkLabel setSelectable: YES];
    NSMutableAttributedString *attrbutedString = [[NSMutableAttributedString alloc]
                                                  initWithString:_hyperLinkLabel.stringValue
                                                  attributes:@{
                                                               NSForegroundColorAttributeName:[NSColor blueColor],
                                                               NSFontAttributeName           :[NSFont  systemFontOfSize:13.0f],
                                                               NSUnderlineStyleAttributeName :@(NSUnderlineStyleSingle)
                                                               }
                                                  ];
    [attrbutedString addAttribute:NSLinkAttributeName
                            value:_hyperLink
                            range:NSMakeRange(0, attrbutedString.length)];
    [_hyperLinkLabel setAttributedStringValue:attrbutedString];
}
[_hyperLinkLabel setAllowsEditingTextAttributes: YES];
[_hyperLinkLabel setSelectable: YES];
  • textFieldにAttributedStringを設定できるようにする
  • ハイパーリンクの選択を許可する
NSMutableAttributedString *attrbutedString = [[NSMutableAttributedString alloc]
                                              initWithString:_hyperLinkLabel.stringValue
                                              attributes:@{
                                                           NSForegroundColorAttributeName:[NSColor blueColor],
                                                           NSFontAttributeName           :[NSFont  systemFontOfSize:13.0f],
                                                           NSUnderlineStyleAttributeName :@(NSUnderlineStyleSingle)
                                                           }
                                           ];
  • textFieldに設定するAttributesStringの変数を作成する
  • initWithStringにはハイパーリンクの文言を設定

  • attributesにはtextFieldのUIをハイパーリンク用に設定する

    • 文字をblueColorにする
    • フォントのサイズを13にする
    • 下線を引く
[attrbutedString addAttribute:NSLinkAttributeName
                        value:_hyperLink
                        range:NSMakeRange(0, attrbutedString.length)];
[_hyperLinkLabel setAttributedStringValue:attrbutedString];
  • 先程作成したAttibutedStringの変数にNSLinkAttributeName属性を追加する
  • 渡す値はハイパーリンクのURL
  • rangeには今回テキスト全体をハイパーリンクにするので先頭から文字列の長さを指定

GitHub