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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值