【问题标题】:Replace text in UITextView with linkable text - iOS用可链接文本替换 UITextView 中的文本 - iOS
【发布时间】:2014-02-27 03:52:25
【问题描述】:

我有包含用户名作为占位符的动态文本,我想用实际用户名替换用户名的占位符,但我希望这个名称是可链接的,换句话说,当我点击用户名时它应该执行重定向到个人资料页面的某个选择器。这是可行的吗?

这是我的代码:

NSString *originalText = @"go to :user profile or go to :place";
NSString *userText = @"Sawsan";
NSString *placeText = @"SomePlace";

originalText = [originalText stringByReplacingOccurrencesOfString:@":user"
                                                             withString:userText];
originalText = [originalText stringByReplacingOccurrencesOfString:@":place"
                                                             withString:placeText];
self.mainLabel.text = originalText;

/*
 * Result is: "go to Sawsan profile or go to SomePlace"
 */

我可以用 Sawsan 和 SomePlace 替换 :user:place,当用户点击它们中的任何一个时,它们可以执行不同的 selector

【问题讨论】:

    标签: ios objective-c nsstring uitextview


    【解决方案1】:

    使用UITapGestureRecognizer

    self.mainLabel.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushAction)];
    [self.mainLabel addGestureRecognizer:tap];
    

    PushAction:

    -(void)pushAction
    {
        NSLog(@"mainLabel tapped");
    }
    

    另外,我会对用户这样做:

    [NSString stringWithFormat:@"go to %@ profile", userText];
    

    【讨论】:

    • 如果我有多个动态文本,比如 :user 和 :place 怎么办?我希望他们每个人都执行不同的选择器
    • 对不起,我不知道该怎么做:[
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2015-06-24
    相关资源
    最近更新 更多