UItextView,UIscrollView,UITableViewCell三种能够使页面滑动控件的总结(1)

来源:互联网 发布:java怎么用easyui 编辑:程序博客网 时间:2024/06/14 13:38

UItextView,UIscrollView,UITableViewCell这三种控件都能使页面滑动,而用法有一些区别,以下是它们各自的使用方法

UITextView

先在.h文件中设置代理

@interface ViewController : UIViewController<UITextViewDelegate>
以及设置一个UITextView的属性,也可以在.m中设置属性

@property(nonatomic, retain) UITextView *textView;

然后是.m中

//初始化方法self.textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, self.view.frame.size.width - 20, 100)];    //设置代理    self.textView.delegate = self;    //在有导航栏的情况下可能输入文本框是下移,    //恢复文本框是偏移    self.automaticallyAdjustsScrollViewInsets = NO;    //文字居中    self.textView.textAlignment = NSTextAlignmentCenter;    //字体颜色    self.textView.textColor = [UIColor brownColor];    //字体大小    self.textView.font = [UIFont systemFontOfSize:22.0];    //编辑使能    self.textView.editable = YES;    self.textView.backgroundColor = [UIColor grayColor];    //圆角大小    self.textView.layer.cornerRadius = 6.0f;    //边框宽度    self.textView.layer.borderWidth = 2;    //边框颜色    self.textView.layer.borderColor = [[UIColor whiteColor]CGColor];    //返回键类型    self.textView.returnKeyType = UIReturnKeyDefault;    //键盘类型    self.textView.keyboardType = UIKeyboardTypeDefault;    //是否可以拖动    self.textView.scrollEnabled = YES;    //设置文本属性    self.textView.allowsEditingTextAttributes = YES;    //自适应高度    self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;    //添加到视图上     [self.view addSubview:self.textView];
然后是点击空白处回收键盘的方法,总共有三种

第一种:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    [super touchesBegan:touches withEvent:event];   [self.view endEditing:YES];}

第二种:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    [self.textView resignFirstResponder];}
第三种:是代理里面选择实现的方法

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{    if ([text isEqualToString:@"\n"]) {        [textView resignFirstResponder];        return NO;    }    return YES;}


0 0