《iOS 8开发指南》,自己总结用

UITextView

文本视图(UITextView)与文本框类似,差别在于文本视图可显示一个可滚动和编辑的文本块,供用户阅读或修改。仅当需要的的输入很多时,才应使用文本视图。

Attribute Inspector分为3部分(TextField, control, view)
(1) Text:设置文本框的默认文本
(2) Placeholder:可以在文本框中显示灰色的字,用于提示用户应该在这个文本框输入什么内容。输入文字,自动消失。
(3) Background:设置背景
(4) Disabled:若选中此项,用户将不能更改文本框内容
(5) 接下来是3个按钮,用来设置对齐方式
(6) Border Style:选择边界风格
(7) 清楚按钮
——》Never appears:从不出现
——》Appears while editing:编辑时出现
——》Appears unless editing: 编辑时不出现
——》Is always visible:总是可见
(8) Clear when editing begins:若选中此项,则当开始编辑这个文本框时,文本框中之前的内容会被清除掉。
(9)设置文本框中文本的颜色
(10)Font:设置文本的字体与字号
(11)Min Font Size :最小字体
(12)Adjust To Fit:指定文本框尺寸缩小时文本框中的文本是否也要缩小。选择它,可以使得全本文本都可见,及时文本可长。但是这个选项要跟Min Font Size配合使用,文本再缩小,也不会小于设定的Min Font Size.
(13)Capitalization:设置大写。下啦菜单中有4个选项。
——》None:不设置大写
——》Words:每个单词首字母大写,这里的单词指的是以空格分开的字符串
——》Sentences:每个句子的第一个字母大写,这里的句子是以句号加空格分开的字符串
——》 All Characters:所有字母大写
(14) Correction:检查拼写,默认是YES
(15)Keyboard:选择键盘类型,比如全数字、字母和数字等
(16)Return Key:选择返回键,可以选择Search、Return、Done等
(17)Auto-enable Return Key:如选择此项,则只有至少在文本框输入一个字符后键盘的返回键才有效。
(18)Secure:当你的文本框用作密码输入框时,可以选择此项。

常用属性

(1) textColor
(2) font
(3) editable
(4) textAlignment
——》UITextAlignmentRight:右对齐
——》UITextAlignmentCenter:居中对齐
——》UITextAlignmentLeft:左对齐

在屏幕中换行显示文本

//TextView
-(void)testUITextView1{
    UITextView *textView = [[UITextView alloc]init];
    textView.frame = self.view.bounds;
    textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//    textView.editable = NO;//不可编辑
    textView.backgroundColor = [UIColor blackColor];//背景为黑色
    textView.textColor = [UIColor whiteColor];//字符为白色
    textView.font = [UIFont systemFontOfSize:32];//字体的设置
    //textView.textAlignment = NSTextAlignmentCenter;
    textView.text = @"学习UITextView\n"
                    "第2行\n"
                    "第3行\n"
                    "第3行\n"
                    "第4行\n"
                    "第5行\n"
                    "第6行\n"
                    "第7行\n"
                    "第8行\n"
                    "第9行\n"
                    "第10行\n"
                    "第11行\n"
                    "第12行\n";
    [self.view addSubview:textView];
}

在屏幕中显示可编辑的文本

-(void)testUITextView2{
    self.uiTextView2 = [[UITextView alloc]init];
    self.uiTextView2.frame = self.view.bounds;
    self.uiTextView2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.uiTextView2.delegate = self;
    self.uiTextView2.text = @"亲们,可以编辑这一段文本";
    [self.view addSubview:self.uiTextView2];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self.navigationController setToolbarHidden:NO animated:YES];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self textViewDidEndEditing:self.uiTextView2]; //画面显示时, 设置为非编辑模式
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.uiTextView2 resignFirstResponder];//画面跳转时设置为非编辑模式
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    static const CGFloat kKeyboardHeight = 216.0;
    //按钮设置为【完成】
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneDidPush)];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    //缩小UITextView以免被键盘挡住
    CGRect textViewFrame = textView.frame;
    textViewFrame.size.height = self.view.bounds.size.height - kKeyboardHeight;
    textView.frame = textViewFrame;
    //工具条位置上移
    CGRect toolbarFrame = self.navigationController.toolbar.frame;
    toolbarFrame.origin.y = self.view.window.bounds.size.height - toolbarFrame.size.height - kKeyboardHeight;
    self.navigationController.toolbar.frame = toolbarFrame;
    [UIView commitAnimations];
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    // 按钮设置为【编辑】
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editDidPush)];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    // 恢复UITextView的尺寸
    textView.frame = self.view.bounds;
    //恢复工具条的位置
    CGRect toolbarFrame = self.navigationController.toolbar.frame;
    toolbarFrame.origin.y = self.view.window.bounds.size.height - toolbarFrame.size.height;
    self.navigationController.toolbar.frame = toolbarFrame;
    [UIView commitAnimations];
}

-(void)editDidPush{
    [self.uiTextView2 becomeFirstResponder];
}

-(void)doneDidPush{
    [self.uiTextView2 resignFirstResponder];
}
-(void)testUITextView3{
    //UITextView的追加
    self.uiTextView3 = [[UITextView alloc]init];
    self.uiTextView3.frame = self.view.bounds;
    self.uiTextView3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.uiTextView3.text = @"此文本可编辑";
   // [self.view addSubview:self.uiTextView3];
    
    //在工具条中追加
    UIBarButtonItem *hasTextButton = [[UIBarButtonItem alloc] initWithTitle:@"hasText" style:UIBarButtonItemStylePlain target:self action:@selector(hasTextDidPush)];
    UIBarButtonItem *selectionButton = [[UIBarButtonItem alloc]initWithTitle:@"selction" style:UIBarButtonItemStylePlain target:self action:@selector(selectionDidPush)];
    UIBarButtonItem *alignmentButton = [[UIBarButtonItem alloc]initWithTitle:@"alignment" style:UIBarButtonItemStylePlain target:self action:@selector(alignmentDidPush)];
    UIBarButtonItem *scrollButton = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(scrollDidPush)];

    NSArray *buttons = [NSArray arrayWithObjects:hasTextButton, selectionButton, alignmentButton, scrollButton,nil];
    [self setToolbarItems:buttons animated:YES];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    static const CGFloat kKeyboardHeight = 216.0;
    //调整工具条位置
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    self.uiTextView3.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - kKeyboardHeight);
    CGRect  toolbarFrame = self.navigationController.toolbar.frame;
    toolbarFrame.origin.y = self.view.window.bounds.size.height - toolbarFrame.size.height - kKeyboardHeight;
    self.navigationController.toolbar.frame = toolbarFrame;
    [UIView commitAnimations];
    [self.uiTextView3 becomeFirstResponder]; // 画面显示时显示键盘
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    // 恢复工具条
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    self.uiTextView3.frame = self.view.bounds;
    CGRect toolbarFrame = self.navigationController.toolbar.frame;
    toolbarFrame.origin.y = self.view.window.bounds.size.height - toolbarFrame.size.height;
    self.navigationController.toolbar.frame = toolbarFrame;
    [UIView commitAnimations];
    [self.uiTextView3 resignFirstResponder];
}

-(void)hasTextDidPush{
    UIAlertView *alert = [[UIAlertView alloc]init];
    if(self.uiTextView3.hasText) {
        alert.message = @"textView.hasText = YES";
    } else {
        alert.message = @"textView.hasText = NO";
    }
    [alert addButtonWithTitle:@"OK"];
    [alert show];
}

-(void)selectionDidPush{
    UIAlertView *alert = [[UIAlertView alloc]init];
    alert.message = [NSString stringWithFormat:@"location = %lu, length = %d", (unsigned long)self.uiTextView3.selectedRange.location, self.uiTextView3.selectedRange.length];
    [alert addButtonWithTitle:@"OK"];
    [alert show];
}

-(void)alignmentDidPush{
    self.uiTextView3.editable = NO;
    if(UITextAlignmentRight < ++self.uiTextView3.textAlignment) {
        self.uiTextView3.textAlignment = UITextAlignmentLeft;
    }
    self.uiTextView3.editable = YES;
}

-(void)scrollDidPush{
    //NSRange scrollRange = NSMakeRange(0, 1);
    [self.uiTextView3 scrollRangeToVisible:NSMakeRange(0, 1)];
}