iOS7 textView处理URL链接 以及点击 长按手势的处理
来源:互联网 发布:apache 禁止显示目录 编辑:程序博客网 时间:2024/04/19 13:53
举例1 :文本超链接
@property (weak,nonatomic) IBOutletUITextView *tv;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
value:@"username://marcelofabri_"
range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
NSUnderlineColorAttributeName: [UIColor lightGrayColor],
NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
self.tv.linkTextAttributes = linkAttributes;
self.tv.attributedText = attributedString;
self.tv.delegate =self ;
self.tv.editable = NO;
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{
NSLog(@"%@",URL.absoluteString);
return NO ;
}
举例2 :文本:系统默认是双击 ,这里用单击实现
self.tv.dataDetectorTypes = UIDataDetectorTypeAll;
self.tv.attributedText = [[NSAttributedString alloc]initWithString:
@" +8602980000000.\r\n"
"My personal web site www.xxxxxx.com.\r\n"
"My E-mail address is XXXXX@gmail.com.\r\n"
"I was born in 1900-01-01."];
self.tv.editable = NO;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onlyTap:)];
[self.tv addGestureRecognizer:tap];
- (void)onlyTap:(UITapGestureRecognizer *)recognizer
{
CGPoint location = [recognizer locationInView:self.tv];
NSLog(@"Tap Gesture Coordinates: %.2f %.2f", location.x, location.y);
NSString *tappedSentence = [self lineAtPosition:CGPointMake(location.x, location.y)];
NSLog(@"%@",tappedSentence);
}
- (NSString *)lineAtPosition:(CGPoint)position
{
//eliminate scroll offset
position.y += _tv.contentOffset.y;
//get location in text from textposition at point
UITextPosition *tapPosition = [_tv closestPositionToPoint:position];
//fetch the word at this position (or nil, if not available)
UITextRange *textRange = [_tv.tokenizer rangeEnclosingPosition:tapPosition withGranularity:UITextGranularitySentence inDirection:UITextLayoutDirectionRight];
return [_tv textInRange:textRange];
}
http://stackoverflow.com/questions/15034652/tap-gesture-to-part-of-a-uitextview
http://www.raywenderlich.com/48001/easily-overlooked-new-features-ios-7#textViewLinks
- iOS7 textView处理URL链接 以及点击 长按手势的处理
- UIGestureRecognizer,手势处理,点击,长按,拖动,左右划动
- 【android】短信中包含链接的textview的点击处理
- 点击TextView中链接,自定义处理事件
- 处理ios7侧滑手势和scrollview手势的冲突
- Android自定义TextView链接点击和长按事件
- 手势与表格的点击事件处理
- 关于点击手势冲突的处理
- Textview 链接处理
- TextView的内部链接支持长按
- iOS7中手势返回时键盘的处理
- 对home键点击和长按的处理
- 【ExpandableListView】---2 长按和点击处理
- TextView将链接替换为文字,点击文字还是点击链接效果,可以自己处理链接点击事件
- TextView部分文本点击处理
- 获取长按手势所点击的cell的行号
- QWebView 中点击链接的处理
- IOS 长链接操作处理
- Android动画学习总结---下
- js的filter的巧用
- Java数组声明、创建、初始化
- PAT初级题目1009(Java实现)
- SharedPreferences.OnSharedPreferenceChangeListener
- iOS7 textView处理URL链接 以及点击 长按手势的处理
- MySql错误代码1045的解决方案
- C语言数据结构-单链表
- [leetcode] Search a 2D Matrix
- mxGraph开发入门指南
- Android实战-使用gradle打多渠道包
- iOS的音讯通知有两种方式,Badge Notification和Alert Notification
- C++ Template 实践草稿
- Node.js学习技术栈