iOS中的UITextView

#import "TextViewController.h"

@interface TextViewController ()<UITextViewDelegate>

@end

@implementation TextViewController
/*
 UITextView文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文。常见UITextView使用在APP的软件简介、内容详情显示、小说阅读显示、发表空间内容输入、说说文本框、评论文本框等。
 UITextView的使用有它本身的代理方法,也有继承于父类的方法。本身的方法有从开始编辑到结束编辑的整个过程的监听,继承的方法主要是继承于UIScrollView的方法,因为关于滚动的控制都属于UIScrollView的。
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 100, 220, 100)];
    textView.tag = 100;
    
    //背景颜色
    textView.backgroundColor= [UIColor yellowColor];
    
    
    //配置文本属性
    //1. 设置textView的文本
    textView.text = @"Where there is a will, there is a way!";
    
    // 实际上,UITextView的文本默认就是居上显示的,出现上面的情况很多都是因为使用了navigationController让scrollView自动适应屏幕造成的。(UITextView继承自UIScrollView)。
            if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
                self.automaticallyAdjustsScrollViewInsets = NO;
            }
    
    //2. 设置文字对齐方式 (默认是 NSTextAlignmentLeft)
    textView.textAlignment = NSTextAlignmentLeft;
    
    //3. 设置文字的字体和大小 (默认字体是 17 点 Helvetica 普通字体)
    textView.font = [UIFont fontWithName:@"Marker Felt" size:20];
    
    //4. 设置字体的颜色
    textView.textColor = [UIColor magentaColor];
    
    //5. 是否允许编辑内容 (默认为YES)
    textView.editable = YES;
    
    //6. 设置文字能否被选择
    textView.selectable = YES;
    
    //7. 返回键的类型 (默认为UIKeyUIReturnKeyDefault)
    textView.returnKeyType = UIReturnKeyDefault;
    
    //8. 键盘类型
    textView.keyboardType = UIKeyboardTypeDefault;
    
    //9. 是否可以拖动
    textView.scrollEnabled = YES;
    
    //10. 设置为边框角弧度
    textView.layer.cornerRadius = 5;
    
    //11. 设置边框宽度
    textView.layer.borderWidth = 1.0;
    
    
    //12.UITextView自定选择文字后的菜单
    UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"分享到空间" action:@selector(changeColor:)];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setMenuItems:[NSArray arrayWithObject:menuItem]];
    [menuItem release];
    
    //设置代理
    textView.delegate = self;
    
    [self.view addSubview:textView];
    [textView release];
    
}
#pragma mark --- wuyiyi
- (void)changeColor:(UIMenuItem *)item {
    
}

#pragma mark --- UITextViewDelegate
//将要开始编辑时触发
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    return YES;
}
//将要结束编辑时触发
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    return YES;
}
//已经开始编辑时触发
- (void)textViewDidBeginEditing:(UITextView *)textView {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    //回收键盘
    //(1)如果程序有导航条, 可以在导航条上添加一个Done按钮, 用来退出键盘
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(levelEditMode:)];
    self.navigationItem.rightBarButtonItem = done;
    [done release];
}
//已经结束编辑时触发
- (void)textViewDidEndEditing:(UITextView *)textView {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    //结束编辑, 消除Done按钮
    self.navigationItem.rightBarButtonItem = nil;
}

//改变文本时触发
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"%s, %d", __FUNCTION__, __LINE__);
    //(2)点击return回收键盘
    if ([text isEqualToString:@"
"]) {
        [textView resignFirstResponder];
    }
    return YES;
//    return NO;
}

//更改UITextView的光标的位置:
- (void)textViewDidChangeSelection:(UITextView *)textView {
    NSRange range;
    range.location = 0;
    range.length = 0;
    textView.selectedRange = range;
}
#pragma mark ---- done action
//点击按钮退出键盘方法
- (void)levelEditMode:(UIBarButtonItem *)sender {
    UITextView *textV = (UITextView *)[self.view viewWithTag:100];
    [textV resignFirstResponder];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
原文地址:https://www.cnblogs.com/wohaoxue/p/4819930.html