我有动态文本,其中包含用户名作为占位符,我想用实际用户名替换用户名的占位符,但我希望这个名称是可链接的,换句话说当我点击用户名时它应该执行某个选择器,重定向到配置文件页面。这可行吗?
这是我的代码:
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
吗?
答案 0 :(得分: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];