[Objective-C]UITextView的使用

UITextView的基本概念

UItextView类继承UIScrollView,所以他拥有UIScrollView类的属性和方法,因此适合长文本输入。

UITextView创建

UITextView* textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 100, 320, 300)];

UITextView的属性

设置文本样式

  1. 设置文本默认显示内容
textView.text = @"www.baidu.com";
  1. 设置文本为斜体
textView.font = [UIFont italicSystemFontOfSize:14];
  1. 设置文本的颜色
textView.textColor = [UIColor redColor];
  1. 设置文本的对齐方式

NSTextAlignmentLeft(左对齐)
NSTextAlignmentRight(右对齐)
NSTextAlignmentCenter(局中显示)

textView.textAlignment = NSTextAlignmentRight;
  1. 设置是否识别文本里面的超链接等内容
textView.dataDetectorTypes = UIDataDetectorTypeAll;

设置文本控制

  1. 设置文本是否可编辑
textView.editable = NO;
  1. 设置文本是否可选中
textView.selectable = NO;

代理方法

@protocol UITextViewDelegate <NSObject, UIScrollViewDelegate>

@optional

// 文本开始编辑时触发的方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
// 文本结束编辑时触发的方法
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;

// 文本已经开始编辑的时候触发的方法
- (void)textViewDidBeginEditing:(UITextView *)textView;
// 文本已经结束编辑的时候触发的方法
- (void)textViewDidEndEditing:(UITextView *)textView;

// 文本内容发生改变的时候触发的方法,返回文本是否可编辑
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
// 文本内容已经发生改变的时候触发的方法
- (void)textViewDidChange:(UITextView *)textView;

// 文本选择区域发生改变的时候触发的方法
- (void)textViewDidChangeSelection:(UITextView *)textView;

// 点击文本链接后触发的方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction API_AVAILABLE(ios(10.0));
// 指定范围的内容与文本附件将要相互作用时,自动激发该方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction API_AVAILABLE(ios(10.0));

// 指定范围的内容与 URL 将要相互作用时激发该方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange API_DEPRECATED_WITH_REPLACEMENT("textView:shouldInteractWithURL:inRange:interaction:", ios(7.0, 10.0));
// 指定范围的内容与文本附件将要相互作用时,自动激发该方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange API_DEPRECATED_WITH_REPLACEMENT("textView:shouldInteractWithTextAttachment:inRange:interaction:", ios(7.0, 10.0));

@end
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值