使用STTweetlabel在浏览器中打开链接

时间:2014-03-04 11:16:28

标签: ios twitter browser hyperlink nsdictionary

我正在使用STTweetLabel显示推文是我应用中的表格视图。

我所拥有的Feed包含指向“http://www.google.com

等网站的链接

我想要做的是,当用户触摸推文中的链接时,它将打开safari并将其指向链接所在的任何内容。

当前发生的事情是,在桌面视图中显示链接时自动打开链接(用户不点击它)

这是我到目前为止检测链接的代码:

@property (nonatomic, strong) NSDictionary *attributesLink;



- (NSDictionary *)attributesForHotWord:(STTweetHotWord)hotWord {
switch (hotWord) {
    case STTweetHandle:
        return _attributesHandle;
        break;
    case STTweetHashtag:
        return _attributesHashtag;
        break;
    case STTweetLink:




        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_attributesLink]];


        return _attributesLink;
        break;
    default:
        break;
}
}

编辑:

我的ViewController中也有这个

        STTweetLabel *tweetLabel = [[STTweetLabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 300.0, 180.0)];


    tweetLabel.text.isAccessibilityElement = YES;




    [tweetLabel setText:text];
    tweetLabel.textAlignment = NSTextAlignmentLeft;


    CGSize size = [tweetLabel suggestedFrameSizeToFitEntireStringConstraintedToWidth:tweetLabel.frame.size.width];
    CGRect frame = tweetLabel.frame;
    frame.size.height = size.height;
    tweetLabel.frame = frame;
    tweetLabel.textSelectable = YES;
    [tweetLabel setDetectionBlock:^(STTweetHotWord hotWord, NSString *string, NSString *protocol, NSRange range) {
        NSArray *hotWords = @[@"Handle", @"Hashtag", @"Link"];




        //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"Link"]];

        titleLabel.text = [NSString stringWithFormat:@"%@ [%d,%d]: %@%@", hotWords[hotWord], (int)range.location, (int)range.length, string, (protocol != nil) ? [NSString stringWithFormat:@" *%@*", protocol] : @""];

    }];


    [cell addSubview:dateLabel];
    [cell addSubview:tweetLabel];
}

1 个答案:

答案 0 :(得分:0)

解决了以下问题:

    STTweetLabel *tweetLabel = [[STTweetLabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 300.0, 180.0)];


    tweetLabel.text.isAccessibilityElement = YES;

    [tweetLabel setText:text];
    tweetLabel.textAlignment = NSTextAlignmentLeft;


    CGSize size = [tweetLabel suggestedFrameSizeToFitEntireStringConstraintedToWidth:tweetLabel.frame.size.width];
    CGRect frame = tweetLabel.frame;
    frame.size.height = size.height;
    tweetLabel.frame = frame;
    tweetLabel.textSelectable = YES;
    [tweetLabel setDetectionBlock:^(STTweetHotWord hotWord, NSString *string, NSString *protocol, NSRange range) {
        NSArray *hotWords = @[@"Handle", @"Hashtag", @"Link"];


        switch (hotWord) {
            case STTweetLink:

                //Open Safari with the link clicked
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];


                break;

            case STTweetHashtag:

                //Preform acton when hashtag is clicked

                break;

            case STTweetHandle:

                //Preform action when username is clicked

                break;

            default:
                break;
        }

        titleLabel.text = [NSString stringWithFormat:@"%@ [%d,%d]: %@%@", hotWords[hotWord], (int)range.location, (int)range.length, string, (protocol != nil) ? [NSString stringWithFormat:@" *%@*", protocol] : @""];

    }];


    [cell addSubview:dateLabel];
    [cell addSubview:tweetLabel];
}