OC 中的 Text View函数

首页 / iOS入门教程 / OC 中的 Text View函数

Text View用于显示多行可滚动文本,该文本可有选择地进行编辑。

Text View - 重要属性

  • dataDetectorTypes
  • delegate
  • editable
  • inputAccessoryView
  • inputView
  • text
  • textAlignment
  • textColor

Text View - 重要方法

-(void)textViewDidBeginEditing:(UITextView *)textView
-(void)textViewDidEndEditing:(UITextView *)textView
-(void)textViewDidChange:(UITextView *)textView
-(BOOL)textViewShouldEndEditing:(UITextView *)textView

Text View - 自定义方法

-(void)addTextView {
   myTextView = [[UITextView alloc]initWithFrame:
   CGRectMake(10 50 300 200)];
   [myTextView setText:@"Lorem ipsum dolor sit er elit lamet, consectetaur
   cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et 
   dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
   ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
   dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat 
   nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in 
   culpa qui officia deserunt mollit anim id est laborum. Nam liber te 
   conscient to factor tum poen legum odioque civiuda.
   Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing
   pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aiqua. 
   Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi 
   aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit 
   in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
   Excepteur sint occaecat cupidatat non proident, sunt in culpa
   qui officia deserunt mollit anim id est laborum. Nam liber te conscient
   to factor tum poen legum odioque civiuda."];
   myTextView.delegate = self;
   [self.view addSubview:myTextView];
}

Text View - 实现委托

#pragma mark - Text View delegates

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:
   (NSRange)range replacementText:(NSString *)text {
   
   if ([text isEqualToString:@"\n"]) {
      [textView resignFirstResponder];
   }
   return YES;
}

-(void)textViewDidBeginEditing:(UITextView *)textView {
   NSLog(@"Did begin editing");
}

-(void)textViewDidChange:(UITextView *)textView {
   NSLog(@"Did Change");
}

-(void)textViewDidEndEditing:(UITextView *)textView {
   NSLog(@"Did End editing");
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView {
   [textView resignFirstResponder];
   return YES;
}

Text View - 更新viewDidLoad

(void)viewDidLoad {
   [super viewDidLoad];
   [self addTextView];
}

运行应用程序时,将获得以下输出-

无涯教程网

iOS Tutorial

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入拆解Java虚拟机 -〔郑雨迪〕

算法面试通关40讲 -〔覃超〕

10x程序员工作法 -〔郑晔〕

从0打造音视频直播系统 -〔李超〕

TypeScript开发实战 -〔梁宵〕

Linux内核技术实战课 -〔邵亚方〕

成为AI产品经理 -〔刘海丰〕

分布式金融架构课 -〔任杰〕

大厂设计进阶实战课 -〔小乔〕

好记忆不如烂笔头。留下您的足迹吧 :)