7

@mentions と #hashtags を識別し、私の TTTAttributedLabel インスタンスのその位置にリンクを作成するリンク検出器を TTTAttributedLabel に追加しました。

- (void)highlightMentionsInString:(NSString *)text withColor:(UIColor *)color isBold:(BOOL)bold isUnderlined:(BOOL)underlined
{
    NSRegularExpression *mentionExpression = [NSRegularExpression regularExpressionWithPattern:@"(?:^|\\s)(@\\w+)" options:NO error:nil];

    NSArray *matches = [mentionExpression matchesInString:text
                                                  options:0
                                                    range:NSMakeRange(0, [text length])];
    for (NSTextCheckingResult *match in matches) {
        NSRange matchRange = [match rangeAtIndex:1];
        NSString *mentionString = [text substringWithRange:matchRange];
        NSRange linkRange = [text rangeOfString:mentionString];
        NSString* user = [mentionString substringFromIndex:1];
        NSString* linkURLString = [NSString stringWithFormat:@"user:%@", user];
        [self.attributedLabel addLinkToURL:[NSURL URLWithString:linkURLString] withRange:linkRange];
    }
}

また、リンクの色と属性を簡単に変更するためにこれを行うことができることも発見しました。

NSArray *keys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName,(id)kCTUnderlineStyleAttributeName
                             , nil];
NSArray *objects = [[NSArray alloc] initWithObjects:color,[NSNumber numberWithInt:kCTUnderlineStyleNone], nil];
NSDictionary *linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];

self.attributedLabel.linkAttributes = linkAttributes;

ただし、これにより、Web リンク、ハッシュタグ、メンションなど、すべてのリンク属性の色が変更されます。正規表現または範囲を使用して異なるリンクの色を作成する方法はありますか? @メンションをグレー、@ハッシュタグを赤、Web リンクを青にしたいとします。

4

2 に答える 2

8

私はちょうど同様の問題に取り組んでいて、あなたの質問に出くわしました。ラベル内の他の種類のものと一致させるために、ある種の異なる式を挿入する方法が正確にわからなかったので、最初のコードでそれが解決されました。

あなたの質問ですが、私がしたことは、TTTAttributedLabel メソッドを NSTextCheckingResult を追加するメソッドに変更することでした。したがって、forそのメソッドのループにいくつかの変更を加え[self.label addLinkWithTextCheckingResult: attributes: ]、提案どおりに属性を使用および設定すると、そのループは次のようになります。

for (NSTextCheckingResult *match in matches) {
    NSRange matchRange = [match rangeAtIndex:1];
    NSString *mentionString = [text substringWithRange:matchRange];
    NSString* user = [mentionString substringFromIndex:1];
    NSString* linkURLString = [NSString stringWithFormat:@"user:%@", user];
    NSArray *keys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName, (id)kCTUnderlineStyleAttributeName, nil];
    NSArray *objects = [[NSArray alloc] initWithObjects:color,[NSNumber numberWithInt:kCTUnderlineStyleNone], nil];
    NSDictionary *linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
    [self.label addLinkWithTextCheckingResult:match attributes:linkAttributes];
}

私の場合、これは # と @ をトーストしたオレンジ色で表示します。

そして、TTTAttributedLabelDelegate に- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTextCheckingResult:(NSTextCheckingResult *)resultTTTAttributedLabelDelegate メソッドがあります。# または @ テキストをタップすると、NSTextCheckingResult で呼び出されます。

これはあなたが探していたものですか?

于 2014-04-10T21:25:25.020 に答える
0

強調表示されたリンクの状態に関する質問はまだ回答されていないため、簡単な解決策を次に示します。

var attrs = [NSFontAttributeName : UIFont.systemFontOfSize(14.0), NSForegroundColorAttributeName: UIColor.blackColor()]
label.activeLinkAttributes = attrs
于 2015-06-26T02:47:46.027 に答える