给UITextView增加了链接
现在在iOS添加你自己的Twitter账户更加简单了,现在你可以给一个NSAttributedString增加链接了,然后当它被点击的时候唤起一个定制的action。
 
首先,创建一个NSAttributedString然后增加给它增加一个NSLinkAttributeName 属性,见以下:
  1. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
  2. [attributedString addAttribute:NSLinkAttributeName
  3. value:@"username://marcelofabri_"
  4. range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];
  5. NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
  6. NSUnderlineColorAttributeName: [UIColor lightGrayColor],
  7. NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
  8. // assume that textView is a UITextView previously created (either by code or Interface Builder)
  9. textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
  10. textView.attributedText = attributedString;
  11. textView.delegate = self;
 
这样就可以让链接在文本中显示。然而,你也可以控制当链接被点击的时候会发生什么,实现这个可以使用UITextViewDelegate协议的新的shouldInteractWithURL方法,就像这样:
  1. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
  2. if ([[URL scheme] isEqualToString:@"username"]) {
  3. NSString *username = [URL host];
  4. // do something with this username
  5. // ...
  6. return NO;
  7. }
  8. return YES; // let the system open this URL
  9. }

给UITextView添加链接的更多相关文章

  1. sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器)

    sqlserver 多库查询 sp_addlinkedserver使用方法(添加链接服务器) 我们日常使用SQL Server数据库时,经常遇到需要在实例Instance01中跨实例访问Instanc ...

  2. linux下添加链接与删除链接(ln命令的用法)

    添加链接使用ln命令用法:#ln --help用法:ln [选项]... 目标 [链接名]或:ln [选项]... 目标... 目录或:ln [选项]... --target-directory=目录 ...

  3. easyui的datagrid行的某一列添加链接

    通过formatter方法给easyui 的datagrid 每行增加操作链接. 效果图 jsp代码: <th field="url" width="100&quo ...

  4. visual studio 添加链接文件

    本文转载http://blog.163.com/zhongpenghua@yeah/blog/static/87727415201282432345613/   那个有个箭头的文件就是链接文件了,添加 ...

  5. HTML之学习笔记(六)添加链接

    html添加链接所用的标签为<a>标签 语法: 定义:从当前页面,跳转到指定页面或文件的一个标签            <a href="URL">热点文字 ...

  6. ckeditor自定义插件--一键给所有的图片添加链接

    ckeditor自定义插件在网上查了查,感觉还是比较好用的,写了一个一键给编辑器中的所有图片添加链接. 在ckeditor目录下的plugins下建以插件为名的文件夹,在里边建plugin.js文件, ...

  7. WordPress文章自动提取tag并添加链接

    我们在编写文章时,经常需要添加一些标签关键词的链接,这样不仅可以优化我们的内链,对用户来说也可以参照相关的文章,如果对文章的关键字进行手动添加链接,那样对我们来说太麻烦了,而且在标签关键词很多的情况下 ...

  8. Bootstrap历练实例:向列表组添加链接

    向列表组添加链接 通过使用锚标签代替列表项,我们可以向列表组添加链接.我们需要使用 <div> 代替 <ul> 元素.下面的实例演示了这点: <!DOCTYPE html ...

  9. SQLServer添加链接服务器

    右键,添加链接服务器 在安全里面输入用户名和密码 添加成功之后的使用方法 select * from [192.168.1.63,3326].[数据库].[dbo].[表]

随机推荐

  1. Spring Boot教程(八)创建含有多module的springboot工程

    创建根工程 创建一个maven 工程,其pom文件为: <?xml version="1.0" encoding="UTF-8"?> <pro ...

  2. Spring Cloud Stream教程(一)介绍Spring Cloud Stream

    Spring Cloud Stream是构建消息驱动的微服务应用程序的框架.Spring Cloud Stream基于Spring Boot建立独立的生产级Spring应用程序,并使用Spring I ...

  3. SettingBar的点击事件拦截

    接下来我们再来看另外一个案例,正如上面界面上显示的两个条目,车辆选择和始发地点选择,他们都有一个共同的特点就是都有共同的标题,内容和右剪头按钮,这种情况下我们都会把它封装成一个组合的自定义View来显 ...

  4. springboot 配置访问本地图片

    spring.mvc.static-path-pattern=/image/** spring.resources.static-locations=file:D://image/

  5. python正则之match search findall

    match:只匹配一次,开头匹配不上,则不继续匹配 a,b,\w+ match(a,"abcdef") 匹配a >>> re.match("a" ...

  6. Mybaits查询返回值是List类型的

    查询返回值是list类型的 1 首先在接口中写方法 public interface EmployeeMapper { public List<Employee> getEmpsByLas ...

  7. mysql执行拉链表操作

    拉链表需求: 1.数据量比较大 2.变化的比例和频率比较小,例如客户的住址信息,联系方式等,比如有1千万的用户数据,每天全量存储会存储很多不变的信息,对存储也是浪费,因此可以使用拉链表的算法来节省存储 ...

  8. Python学习之==>json处理

    json是一种所有语言都通用的Key-Value数据结构的数据类型,很像Python中的字典,在Python中可以通过json模块对json串和字典进行转换. 1.把字典转换成json串 import ...

  9. linux下 sleep() 与 usleep()

    usleep() 将进程挂起一段时间, 单位是微秒(百万分之一秒): 头文件: unistd.h 语法: void usleep(int micro_seconds); 返回值: 无 内容说明:本函数 ...

  10. Sql注入校验

    /// <summary> /// Sql注入校验 /// </summary> /// <param name="listWord">字符&l ...