iOSiOS技术iOS 知识点

iOS-UITextView 文本输入框

2018-03-21  本文已影响881人  我是谁重要吗

//记得设置代理
- (void)viewDidLoad {
    [super viewDidLoad];
//     Do any additional setup after loading the view, typically from a nib.

    
    UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 20, 300, 330)];
    textView.backgroundColor = [UIColor grayColor];
    //文本
    textView.text = @"aaweqtrehgtbwsagas 123456 撒旦法师打发四的发生的 阿斯顿发送到发送到发阿斯顿发生阿斯蒂芬 撒旦法阿斯蒂芬";
    //字体
    textView.font = [UIFont boldSystemFontOfSize:20.0];
    //对齐
    textView.textAlignment = NSTextAlignmentCenter;
    //字体颜色
    textView.textColor = [UIColor redColor];
    //允许编辑
    textView.editable = YES;
    //用户交互     ///////若想有滚动条 不能交互 上为No,下为Yes
    textView.userInteractionEnabled = YES; ///
    //自定义键盘
    //textView.inputView = view;//自定义输入区域
    //textView.inputAccessoryView = view;//键盘上加view
    textView.delegate = self;
    [self.view addSubview:textView];
    
    textView.scrollEnabled = YES;//滑动
    textView.returnKeyType = UIReturnKeyDone;//返回键类型
    textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应
    textView.dataDetectorTypes = UIDataDetectorTypeAll;//数据类型连接模式
    textView.autocorrectionType = UITextAutocorrectionTypeNo;//自动纠错方式
    textView.autocapitalizationType = UITextAutocapitalizationTypeNone;//自动大写方式
    
    //禁止文字居中或下移64,因为avigationController下scrollView自动适应屏幕,而UITextView继承自UIScrollView
    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    
    
    //[self testMPItem];
}

//////////事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //判断类型,如果不是UITextView类型,收起键盘
    for (UIView* view in self.view.subviews) {
        if ([view isKindOfClass:[UITextView class]]) {
            UITextView* tv = (UITextView*)view;
            [tv resignFirstResponder];
        }
    }
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    NSLog(@"开始编辑");
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    NSLog(@"结束编辑");
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    //控制文本输入内容
    if (range.location>=100){
        //控制输入文本的长度
        return  NO;
    }
    if ([text isEqualToString:@"\n"]){
        //禁止输入换行
        return NO;
    }
    
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
    NSLog(@"已经修改");
    //自适应文本高度
    //计算文本的高度
    CGSize constraintSize;
    constraintSize.width = textView.frame.size.width-16;
    constraintSize.height = MAXFLOAT;
    CGSize sizeFrame =[textView.text sizeWithFont:textView.font
                                constrainedToSize:constraintSize
                                    lineBreakMode:UILineBreakModeWordWrap];

    //重新调整textView的高度
    textView.frame = CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textView.frame.size.width,sizeFrame.height+5);
    
}

- (void)textViewDidChangeSelection:(UITextView *)textView{
    NSLog(@"textViewDidChangeSelection");
}

上一篇下一篇

猜你喜欢

热点阅读