- iOS 教程
- iOS - 主页
- iOS - 入门
- iOS - 设置环境
- iOS - Objective-C 基础
- iOS - 首个 iPhone 应用
- iOS - 动作与接口
- iOS - 委托
- iOS - UI 元素
- iOS - 加速度计
- iOS - 通用应用程序
- iOS - 摄像头管理
- iOS - 位置处理
- iOS - SQLite 数据库
- iOS - 发送电子邮件
- iOS - 音频和视频
- iOS - 文件处理
- iOS - 访问地图
- iOS - 应用内购买
- iOS - iAd 集成
- iOS - GameKit
- iOS - 故事板
- iOS - 自动布局
- iOS - Twitter 与 Facebook
- iOS - 内存管理
- iOS - 应用程序调试
- iOS 实用资源
- iOS - 快速指南
- iOS - 实用资源
- iOS - 讨论
iOS - 文本视图
使用文本视图
文本视图用于显示可滚动的多行文本,并且该文本可以进行编辑(可选)。
重要属性
- dataDetectorTypes
- delegate
- editable
- inputAccessoryView
- inputView
- text
- textAlignment
- textColor
重要委托方法
-(void)textViewDidBeginEditing:(UITextView *)textView -(void)textViewDidEndEditing:(UITextView *)textView -(void)textViewDidChange:(UITextView *)textView -(BOOL)textViewShouldEndEditing:(UITextView *)textView
添加自定义方法 addTextView
-(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]; }
在 ViewController.m 中实现文本视图委托
#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; }
如下所示更新 ViewController.m 中的 viewDidLoad
(void)viewDidLoad { [super viewDidLoad]; [self addTextView]; }
输出
运行应用程序后,我们将获得以下输出 −
ios_ui_elements.htm
广告