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