首页 技术 正文
技术 2022年11月6日
0 收藏 478 点赞 1,083 浏览 1280 个字

给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. }
上一篇: 毛玻璃效果 css
下一篇: Show Profile
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893