Qiita Conference 2025

鹿野壮 (@tonkotsuboy_com)

アウトプットは最強の自己投資 〜発信が人生とキャリアを変えた話〜

1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-05-17

概要

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

実装

HyperLinkWindowController.xib

-w866

  • ラベル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

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?