无涯教程-OC - Text View函数

83 阅读1分钟

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

参考链接

www.learnfk.com/ios/ios-ui-…