纯代码代码创建textView 输入文字时出现文字不从第一行显示问题 以及textField的初始化

来源:互联网 发布:mac applications 编辑:程序博客网 时间:2024/05/17 09:15

 纯代码代码创建textView  输入文字时出现文字不从第一行显示问题    

默默的查了一下,原来是controller的影响,但是从storyboard 拖得控件就不会这样,修改一下控制器的属性

self.automaticallyAdjustsScrollViewInsets = NO;

就解决了。会受到状态栏,导航栏,以及工具条,tabBar的影响 自动添加内部的insets

A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.

The default value of this property is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.


附textView的placeholder效果 

实现方法一:

#pragma mark 代理监听 textView的改变

- (void)textViewDidBeginEditing:(UITextView *)textView

{

    if ([textView.textisEqualToString:@"请输入内容"]) {

        textView.text = nil;

        textView.textColor = [UIColorblackColor];

//        self.automaticallyAdjustsScrollViewInsets =NO;

    }

    NSLog(@"%s",__func__);

    return;

}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView

{

     if ([textView.textisEqualToString:@""] ) {

        textView.text = @"请输入内容";

        textView.textColor = CFColor(213, 213, 213);

    }

    return YES;

}

- (void)textViewDidEndEditing:(UITextView *)textView

{

    if ([textView.textisEqualToString:@""] ) {

        textView.text = @"请输入内容";

        textView.textColor = CFColor(213, 213, 213);

    }

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    if ([self.contentField.textisEqualToString:@""] ) {

        self.contentField.text =@"请输入内容";

        self.contentField.textColor =CFColor(213,213,213);

        [self.contentFieldresignFirstResponder];

    }

    NSLog(@"touchesBegan");

}


实现方式二:

 UITextView上如何加上类似于UITextFieldplaceholder呢,其实在UITextView上加上一个UILabel,然后再实现

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text这个代理方法就可以了

 

 具体实现如下:

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

 {

 if (![text isEqualToString:@""]) {

 _label1.hidden = YES;

 }

 if ([text isEqualToString:@""] && range.location == 0 && range.length == 1) {

 _label1.hidden = NO;

 }

 return YES;

 }

(1)  _label1 是加在UITextView上的UILabel _label1.text = @"请输入内容";

(2) [text isEqualToString:@""] 表示输入的是退格键

(3) range.location == 0 && range.length == 1 表示输入的是第一个字符


textField的代码初始化特殊需求:(storyboard可以设置的就能用代码实现)

    self.registerPwdField.clearButtonMode = UITextFieldViewModeWhileEditing;//账号文本框文本框

    self.registerPwdField.secureTextEntry  = YES;//密码文本框

    self.registerPwdField.keyboardType = UIKeyboardTypePhonePad;// 文本框弹出的键盘的类型



0 0
原创粉丝点击