说明:
- 本文将介绍UITextView的三种效果.
- 本文会使用一些NSAttributedString的属性.
- 所以, 建议在阅读本篇博文之前, 先阅读 iOS_NSAttributedString 的21种属性详细介绍(图文混排)
- 本文所展示的效果只是给大家提供个思路, 一定会有不完美的地方, 同时也希望大家根据自己的需求灵活运用.
此文章由 @春雨 编写. 经 @Scott,@黑子 审核. 若转载此文章,请注明出处和作者
链接地址在应用程序内跳转
核心API
Class: UITextView
/** UITextView的编辑状态, 默认YES. */
@property(nonatomic, getter=isEditable) BOOL editable
/** */
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes
/** 字典内存储链接文本的属性. */
@property(nonatomic, copy) NSDictionary *linkTextAttributes
/** 询问代理人, 是否可以跳转到指定的链接地址. */
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
功能实现
我们在UITextView里面点击链接地址时, 它是跳到浏览器里面的. 如果我们不想跳到浏览器, 想在自己的程序内部跳转显示, 该怎么做呢?
思路
UITextView有一个代理方法是用来链接跳转动作是否执行的. 返回值是BOOL类型, 默认是YES.
* 当我们返回NO时, 它就不会跳转了.
* 在这个代理方法内, 我们执行其他的操作, 让链接地址的内容在程序内显示.
Code :
#import "ViewController.h"
#import "WebViewController.h"
/** 签订协议 */
@interface ViewController ()<UITextViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self layoutTextView];
/** 这个属性是UIViewController的属性, 当有Navicontroller的时候, 会自动向下调整UIScrollView及其子类的坐标位置, 默认为YES, 开启状态. */
/** 如果为YES, TextView里的内容会自动向下移动位置. 可以自己测试一下. */
self.automaticallyAdjustsScrollViewInsets = NO;
}
- (void)layoutTextView {
/** 第一种链接界面: 地址链接. */
/** 创建UITextView的对象. 在这里我们并不需要textContainer, 设置成nil即可. */
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(30, 100, 300, 150) textContainer:nil];
textView.text = @"http://blog.youkuaiyun.com/sponge_cmz?viewmode=contents";
textView.font = [UIFont systemFontOfSize:20];
textView.layer.borderColor = [UIColor blackColor].CGColor;
textView.layer.borderWidth = 1;
/** 设置代理人, 我们要实现的效果, 需要用到代理方法. 在上面签订UITextViewDelegate协议. */
textView.delegate = self;
/** 链接地址能够跳转, textView的编辑状态必须为NO, 否则与普通文本无异. */
textView.editable = NO;
/** 设置自动检测类型为链接网址. */
textView.dataDetectorTypes = UIDataDetectorTypeLink;
/** 设置链接文字的属性. */
textView.linkTextAttributes = @{NSForegroundColorAttributeName: [UIColor orangeColor]};
[self.view addSubview:textView];
/** 第二种连接界面: 文字链接 */
UITextView *otherTextView = [[UITextView alloc] initWithFrame:CGRectMake(30, 350, 300, 150) textContainer:nil];
/** font属性是设置text的字体, 但是它对attributedText的字体不起作用. */
// otherTextView.font = [UIFont systemFontOfSize:20];
otherTextView.layer.borderColor = [UIColor blackColor].CGColor;
otherTextView.layer.borderWidth = 1;
otherTextView.delegate = self;
otherTextView.editable = NO;
/** 详细内容请见博文说明中提到的另外一篇博客. */
NSAttributedString *linkAttribute = [[NSAttributedString alloc] initWithString:@"百度" attributes:@{NSLinkAttributeName: [NSURL URLWithString:@"http://www.baidu.com"], NSFontAttributeName:[UIFont systemFontOfSize:25]}];
otherTextView.attributedText = linkAttribute;
[self.view addSubview:otherTextView];
}
/** 当点击链接时, 是否要跳转到浏览器. 默认返回YES. 想要实现在应用程序内部跳转, 只需要返回NO即可. */
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
/** 跳转到WebViewController的WebView上. */
WebViewController *webContro = [[WebViewController alloc] init];
UIWebView *web = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bo

本文详细介绍了在iOS应用中如何处理UITextView的链接跳转,通过自定义代理方法避免跳转到浏览器,实现内部处理。同时,讲解了如何为UITextView添加占位符效果,以及如何改变选中文本的属性,如加下划线和更改颜色,以满足阅读和标注需求。
最低0.47元/天 解锁文章
390

被折叠的 条评论
为什么被折叠?



