·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> UITextView

UITextView

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

//初始化
    self.textView=[[UITextView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    //设置内容
    self.textView.text=@"显示内容";
    //设置字体
    self.textView.font=[UIFont systemFontOfSize:20];
    //设置字体颜色
    self.textView.textColor=[UIColor whiteColor];
    //设置背景色
    self.textView.backgroundColor=[UIColor whiteColor];
    //设置返回键样式
    self.textView.returnKeyType=UIReturnKeyDefault;
    //设置键盘样式
    self.textView.keyboardType=UIKeyboardTypeDefault;
    //是否允许拖动
    self.textView.scrollEnabled=NO;
    //自适应
    self.textView.autoresizingMask=UIViewAutoresizingFlexibleHeight;
    //是否允许编辑
    self.textView.editable=NO;
    //添加到视图
    [self.view addSubview:self.textView];

//TextView继承自ScrollView,所以也具有偏移量属性,只是在ios7环境中,textView并不能立即更新contentSize,采用以下方法可以解决这个问题
    float height;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        
        CGRect textFrame=[[self.textView layoutManager]usedRectForTextContainer:[self.textView textContainer]];
        height = textFrame.size.height;
        
    }else {
        
        height = self.textView.contentSize.height;
    }