UITextView的使用

UITextView的基本概念UITextView创建

UITextView的属性设置文本样式设置文本控制代理方法

UITextView的基本概念

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

UITextView创建

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

UITextView的属性

设置文本样式

设置文本默认显示内容

textView.text = @"www.baidu.com";

设置文本为斜体

textView.font = [UIFont italicSystemFontOfSize:14];

设置文本的颜色

textView.textColor = [UIColor redColor];

设置文本的对齐方式

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

textView.textAlignment = NSTextAlignmentRight;

设置是否识别文本里面的超链接等内容

textView.dataDetectorTypes = UIDataDetectorTypeAll;

设置文本控制

设置文本是否可编辑

textView.editable = NO;

设置文本是否可选中

textView.selectable = NO;

代理方法

@protocol UITextViewDelegate

@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

文章来源

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。