ハイパーリンクをフォーマットで返す API を使用しています。
正規表現を使用してタグ間のテキストを取得し、href 間のテキストを使用して URL を取得します。タグ間のテキストとhrefのURLへのリンクを表示したい。
次のようにラベルを作成します。
TTTAttributedLabel * attributedLabel = [[TTTAttributedLabel alloc] init];
[attributedLabel setFont:font];
[attributedLabel setTextColor:[UIColor colorFromHex:0x4b4b4b]];
[attributedLabel setLineBreakMode:NSLineBreakByWordWrapping];
[attributedLabel setNumberOfLines:0];
attributedLabel.userInteractionEnabled = YES;
[attributedLabel setEnabledTextCheckingTypes:NSTextCheckingTypeLink];
[attributedLabel setDelegate:delegate];
attributedLabel.linkAttributes = @{ (id)kCTForegroundColorAttributeName: [UIColor colorFromHex:0x0080be],
(id)kCTUnderlineStyleAttributeName : [NSNumber numberWithInt:NSUnderlineStyleNone] };
[attributedLabel setText:string];
そして、次のように URL とテキストを設定します。正規表現は割愛します (URL は別の関数で設定されているため、attributedLabel はラベルになりました):
NSString *replacementString = [label.text substringWithRange:[match rangeAtIndex:2]];
NSString *linkString = [label.text substringWithRange:[match rangeAtIndex:1]];
label.text = [label.text stringByReplacingCharactersInRange:[match rangeAtIndex:0] withString:replacementString];
NSRange replacementRange = NSMakeRange([match rangeAtIndex:0].location, [replacementString length]);
[label addLinkToURL:[NSURL URLWithString:linkString] withRange:replacementRange];
[ラベルのリンク] をログに記録しても、次の結果が得られます。
(
"<NSLinkCheckingResult: 0x7b9f2780>{454, 4} {http://mysuperawesomesite.com}"
)
誰かが私が間違っているかもしれないことを知っていますか?