首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用STTweetlabel在浏览器中打开链接

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

Stack Overflow用户
提问于 2014-03-04 11:16:27
回答 1查看 395关注 0票数 0

我使用STTweetLabel来显示tweet,这是我的应用程序中的一个表视图。

我拥有的提要包含指向网站的链接,例如“http://www.google.com

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

现在发生的事情是,当链接显示在表视图中时,链接会自动打开(用户没有单击它)。

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

代码语言:javascript
复制
@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里也有这个

代码语言:javascript
复制
        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];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-04 12:23:36

解决以下问题:

代码语言:javascript
复制
    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];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22170455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档