当文本'@'符号出现在twitter中时,我需要使用TTTAttributeLabel点击该部分。例如:“hi @test hello text”。 只有@test部分可点击。我正在uitableview单元格中使用TTTAttributeLabel类,以便当用户按下标签的其他部分时表格didSelectedRowAtIndexPath方法调用意味着不要点击'@test'。
提前致谢。
答案 0 :(得分:2)
您可以使用以后会拦截的伪 网址,为支持链接的任何可用UILabel
替换添加自定义操作:
TTTAttributedLabel *tttLabel = <# create the label here #>;
NSString *labelText = @"Lost? Learn more.";
tttLabel.text = labelText;
NSRange r = [labelText rangeOfString:@"Learn more"];
[tttLabel addLinkToURL:[NSURL URLWithString:@"action://show-help"] withRange:r];
然后,在TTTAttributedLabelDelegate
:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([[url scheme] hasPrefix:@"action"]) {
if ([[url host] hasPrefix:@"show-help"]) {
/* load help screen */
} else if ([[url host] hasPrefix:@"show-settings"]) {
/* load settings screen */
}
} else {
/* deal with http links here */
}
}
TTTAttributedLabel是OHAttributedLabel的分支。
如果您想要更复杂的方法,请查看Nimbus Attributed Label。它支持开箱即用的自定义链接。
或强>
查看RTLabel
答案 1 :(得分:1)
您可以使用this question中的代码示例(请参阅highlightMentionsInString:
函数)
然后将此代码添加到tableView:cellForRowAtIndexPath:
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
MyCell *cell;
// configure your cell
// ...
cell.attributedLabel.delegate = self;
cell.attributedLabel.userInteractionEnabled = YES;
cell.attributedLabel.text = someText;
[self highlightMentionsInLabel:cell.attributedLabel]
return cell;
}
- (void)highlightMentionsInLabel:(TTTAttributedLabel *)attributedLabel {
NSString *text = attributedLabel.text;
NSRegularExpression *mentionExpression = [NSRegularExpression regularExpressionWithPattern:@"(?:^|\\s)(@\\w+)" options:NO error:nil];
// and so on, use code from question I linked above
// ...
}
...
# pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
// your code here to handle `user:username` links
}
如果你想在用户/#标签录制时有不同的行为,你需要为#hashtags实现单独的highlightTagsInLabel:
方法,使用不同的正则表达式和不同的网址(例如tag:tag_string
)并在之后调用它highlightMentionsInLabel:
答案 2 :(得分:0)
您可以为支持链接的UILabel替换添加假网址方案链接,并使用委托点击链接点击链接。
例如:
TTTAttributedLabel *label = <# create the label here #>;
NSString *labelText = @"My name is @test.";
label.text = labelText;
NSRange r = [labelText rangeOfString:@"Learn more"];
// here you can add some params to your url
[label addLinkToURL:[NSURL URLWithString:@"your-fake-scheeme://say-hello"] withRange:r];
然后为您的标签添加委托并实现attributionLabel:didSelectLinkWithURL:method:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([[url scheme] hasPrefix:@"your-fake-scheeme"]) {
if ([[url host] hasPrefix:@"say-hello"]) {
//parse your url and extract params to do something
}
} else {
// if it is a regular link open it in browser
[[UIApplication sharedApplication] openURL:url];
}
}
我认为标签中的链接不会与您的表格视图冲突
答案 3 :(得分:0)
这对我在Swift中有用(需要定义两个扩展名),你可以在这里找到它们
func highlightMentionsInLabel(attributedLabel:TTTAttributedLabel){
let textObject = attributedLabel.text
var mentionExpression:NSRegularExpression?
do {
mentionExpression = try NSRegularExpression(pattern: "(?:^|\\s)(@\\w+)", options: NSRegularExpressionOptions.CaseInsensitive)
}catch{
print("error:\(error)")
return
}
if let text = textObject, let expression = mentionExpression{
let matches: [NSTextCheckingResult] = expression.matchesInString(text, options: NSMatchingOptions.init(rawValue: 0), range: NSMakeRange(0, text.characters.count))
for match: NSTextCheckingResult in matches {
let matchRange: NSRange = match.rangeAtIndex(1)
let swiftRange = text.rangeFromNSRange(matchRange)!
let mentionString: String = text.substringWithRange(swiftRange)
let linkRange = text.rangeOfString(mentionString)
let index:String.Index = mentionString.startIndex.advancedBy(1)
let user = mentionString.substringFromIndex(index)
let linkURLString: String = "user:\(user)"
attributedLabel.addLinkToURL(NSURL(string: linkURLString)!, withRange: linkURLString.NSRangeFromRange(linkRange!))
}
}
}