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

猜你喜欢

转载自blog.csdn.net/u011646339/article/details/49133729