在iOS中检测哈希标记#,提及标记@,就像在Twitter应用程序中一样
我需要检测描述在iOS中检测哈希标记#,提及标记@,就像在Twitter应用程序中一样,ios,objective-c,uitableview,uilabel,hashtag,Ios,Objective C,Uitableview,Uilabel,Hashtag,我需要检测描述UILabel中给出的标记,并将文本颜色更改为[UIColor BlueColor]无法将UILabel中的特定文本颜色更改为蓝色。现在我在customUITableViewCell中使用这个UILabel。有没有办法通过文本颜色来区分#标记和普通文本?有人能帮我解决这个问题吗 -(NSMutableAttributedString*)decorateTags:(NSString *)stringWithTags{ NSError *error = nil;
UILabel
中给出的标记,并将文本颜色更改为[UIColor BlueColor]代码>无法将UILabel
中的特定文本颜色更改为蓝色。现在我在customUITableViewCell
中使用这个UILabel
。有没有办法通过文本颜色来区分#标记和普通文本?有人能帮我解决这个问题吗
-(NSMutableAttributedString*)decorateTags:(NSString *)stringWithTags{
NSError *error = nil;
//For "Vijay #Apple Dev"
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];
//For "Vijay @Apple Dev"
//NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"@(\\w+)" options:0 error:&error];
NSArray *matches = [regex matchesInString:stringWithTags options:0 range:NSMakeRange(0, stringWithTags.length)];
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:stringWithTags];
NSInteger stringLength=[stringWithTags length];
for (NSTextCheckingResult *match in matches) {
NSRange wordRange = [match rangeAtIndex:1];
NSString* word = [stringWithTags substringWithRange:wordRange];
//Set Font
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, stringLength)];
//Set Background Color
UIColor *backgroundColor=[UIColor orangeColor];
[attString addAttribute:NSBackgroundColorAttributeName value:backgroundColor range:wordRange];
//Set Foreground Color
UIColor *foregroundColor=[UIColor blueColor];
[attString addAttribute:NSForegroundColorAttributeName value:foregroundColor range:wordRange];
NSLog(@"Found tag %@", word);
}
// Set up your text field or label to show up the result
// yourTextField.attributedText = attString;
//
// yourLabel.attributedText = attString;
return attString;
}
新增:
在iOS 9中使用此链接
旧的:
也可以使用它的自定义功能这里有一个快捷的解决方案。它使用UITextView
,该视图支持属性文本、多行,并支持内置委托将点击事件映射到所选单词(用户看到蓝色文本时可能会想到)
它没有将字符范围更改为.blueColor()
,而是添加了一个链接属性,可以自动将可单击文本设置为全局色调
它还包含一些基本的Twitter标签规则支持,用于处理数字#1
和特殊字符@abc.go
工作样本项目:
带有更一般解释的博客文章:
用法:
textView.resolveHashTags()
要处理tap事件,请执行以下操作:
func showHashTagAlert(tagType:String, payload:String){
let alertView = UIAlertView()
alertView.title = "\(tagType) tag detected"
// get a handle on the payload
alertView.message = "\(payload)"
alertView.addButtonWithTitle("Ok")
alertView.show()
}
extension ViewController : UITextViewDelegate {
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
// check for our fake URL scheme hash:helloWorld
if let scheme = URL.scheme {
switch scheme {
case "hash" :
showHashTagAlert("hash", payload: URL.resourceSpecifier!)
case "mention" :
showHashTagAlert("mention", payload: URL.resourceSpecifier!)
default:
println("just a regular url")
}
}
return true
}
}
你可以使用这个图书馆。它还检测用户对hashtag的点击
像这样:
label.hashtagLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) {
NSLog(@"Hashtag tapped %@", string);
};
嗨,Vijay,当我使用它时,我感觉我在UITableView
中的滚动出现了问题。是因为对UILabel
使用了AttributedString
?有什么想法吗?将AttributedString用于UILabel不会导致tableview出现问题。我认为最好仔细检查所有单词,并检查单词是否以#
或@
开头。您只需为数据源数组创建一次属性字符串,然后反复使用相同的字符串。这是最好的想法,比如方法的名称,–decorateTags:
:)到目前为止,它有很大的缺陷,当你选择最后一个字符时,它崩溃了。它有什么问题@FedeHenzeIt在STTweetLabel github页面中表示,该组件不再受支持。我不建议使用不再受支持的组件。@FedeHenze请检查编辑后的答案+1如果有帮助的话!我已经使用了ActiveLabel,但感谢您的更新!
label.hashtagLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) {
NSLog(@"Hashtag tapped %@", string);
};