UITextView绘制圆角和添加背景

作者: shaneZhang 分类: ios技术 发布时间: 2014-05-02 20:22

UITextView 修改样式资料比较少,最近摸索了下,总结了以下几种方式:

 
一、给UITextView绘制圆角,通过QuartzCore框架,操作CALayer可以给UITextView绘制圆角边框。
       textView.layer.cornerRadius = 6
       textView.layer.masksToBounds = YES
通过cornerRadius可设置圆角弧度。
 
二、UITextView和UIImageView组合,将背景图片制作好加载进UIImageView。
      CGRect imageViewFrame = CGRectMake(30.0,100.0,240.0,90.0);
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
      imageView.image = [UIImage imageNamed:@”background.png”];
      imageView.userInteractionEnabled = YES;
      CGRect textViewFrame = CGRectMake(5.0,5.0,230.0,80.0);
      UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame];
      textView.backgroundColor = [UIColor clearColor];
      [imageView addSubview:textView];
设置imageView.userInteractionEnabled = YES,让textView响应触碰事件。
 
三、设置UITextView背景图片,在UITextView区域内。
      CGRect textViewFrame = CGRectMake(30.0,100.0,240.0,90.0);
      UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame];      
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:[textView bounds]];
      imageView.image = [UIImage imageNamed:@”background.png”];
      [textView addSubview:imageView];
      [textView sendSubviewToBack:imageView];
看到的另一种方法,这个方法我已经实验过了,可行:

  1. UITextView *textView = [[UITextView alloc]initWithFrame: window.frame];
  1.     textView.text = @”text\n text\n text”;
  1.     UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
  1.     imgView.image = [UIImage imageNamed: @”myImage.jpg”];
  1.     [self.view addSubview: imgView];
  1.     [self.view sendSubviewToBack: imgView];
  1.     [self.view addSubview: textView];
  1. 但是注意,5,6,7行我修改过,原来的代码是:
  1.   [textView addSubview: imgView];
  1.     [textView sendSubviewToBack: imgView];
  1.     [self.view addSubview: textView];
  1. 这样实际上是把imgview作为了textview的一个字视图先加进来,然后把textview在加入到当前视图,这样做的不好处就是imgview因为是textview的一个字视图,所以大小跟textview一样,也就是说你没法设置整个页面的背景,如果你的textview不是全屏的话

本页面支持繁体中文友好显示:UITextView绘制圆角和添加背景

如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复