LoginSignup
7
7

More than 5 years have passed since last update.

TTTAttributedLabelを使ってリンクが設定されているところをタップするとリンク部分の背景色が変わるようにする

Posted at

Twitterアプリではリンクが設定されている箇所をタップすると背景色が変わります。
同じようなことをTTTAttributedLabelを使って実現します。

リンク部分の背景色を変える

//リンクを抽出するようにする
self.commentLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;

//タップしたときのリンク部分の背景色
UIColor* linkBackgroundColor = [UIColor blueColor];
//リンク部分のフォント
UIFont* linkFont = [UIFont fontWithName:@"KohinoorDevanagari-Medium" size:15];

//リンク部分の装飾
self.commentLabel.linkAttributes = @{
                                        NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone),
                                        (NSString *)kCTFontAttributeName : linkFont
                                    };

//リンク部分ハイライト時の装飾
self.commentLabel.activeLinkAttributes = @{
                                            (NSString*)kTTTBackgroundFillColorAttributeName : (__bridge id)[linkBackgroundColor CGColor],
                                            (NSString *)kCTFontAttributeName : linkFont
                                            };

これでリンク部分をタップしたときに背景色を変えることができます。

7
7
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
7
7