为UIView设置渐变背景

原文发布于http://blog.csdn.net/xzc2677/article/details/31790257

项目需要,需要在图片上显示文字,但是文字的颜色很难控制,有时候与背景图的颜色很接近导致文字难以看清楚,可以通过将图片上显示文字的地方加一层黑色的半透明的背景色来解决这个问题。将这层背景色做成从黑色到透明的渐变。

比如这样一张图,我需要在低端加上介绍文字

可以看到底下变得更黑了,文字更加清楚。


实现方式主要使用了CAGradientLayer。先在ImageView的底端加一个UIView,并在这个UIView上插入一个从透明到黑色的CAGradientLayer,然后再UIView上插入一个Label就行了。

实现代码如下:


[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  ViewController.m  
  3. //  Layer  
  4. //  
  5. //  Created by xuzhaocheng on 14-6-17.  
  6. //  Copyright (c) 2014年 Zhejiang University. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16. {  
  17.     CAGradientLayer *_gradientLayer;  
  18.     UIView *_layerView;  
  19.     UIImageView *_imageView;  
  20. }  
  21.   
  22. - (void)viewDidLoad  
  23. {  
  24.     [super viewDidLoad];  
  25.     // Do any additional setup after loading the view, typically from a nib.  
  26.     self.view.backgroundColor = [UIColor blueColor];  
  27.       
  28.     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10350150100)];  
  29.     [button setTitle:@"显示/隐藏标题" forState:UIControlStateNormal];  
  30.     [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];  
  31.     [self.view addSubview:button];  
  32.       
  33.     _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test"]];  
  34.     _imageView.frame = CGRectMake(00320320);  
  35.   
  36.     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(1025320100)];  
  37.     label.text = @"我是标题标题标题!";  
  38.     label.textColor = [UIColor whiteColor];  
  39.   
  40.       
  41.     _layerView = [[UIView alloc] initWithFrame:CGRectMake(0320-100320100)];  
  42.       
  43.     _gradientLayer = [CAGradientLayer layer];  // 设置渐变效果  
  44.     _gradientLayer.bounds = _layerView.bounds;  
  45.     _gradientLayer.borderWidth = 0;  
  46.   
  47.     _gradientLayer.frame = _layerView.bounds;  
  48.     _gradientLayer.colors = [NSArray arrayWithObjects:  
  49.                                     (id)[[UIColor clearColor] CGColor],  
  50.                                     (id)[[UIColor blackColor] CGColor], nil nil];  
  51.     _gradientLayer.startPoint = CGPointMake(0.50.5);  
  52.     _gradientLayer.endPoint = CGPointMake(0.51.0);  
  53.     
  54.     [_layerView.layer insertSublayer:_gradientLayer atIndex:0];  
  55.       
  56.     [_imageView addSubview:_layerView];  
  57.     [_layerView addSubview:label];  
  58.     [self.view addSubview:_imageView];  
  59. }  
  60.   
  61.   
  62. - (void)buttonPressed  
  63. {  
  64.     static BOOL yesOrNo = YES;  
  65.     if (yesOrNo) {  
  66.         [_layerView removeFromSuperview];  
  67.     } else {  
  68.         [_imageView addSubview:_layerView];  
  69.     }  
  70.     yesOrNo = !yesOrNo;  
  71.   
  72. }  
  73.   
  74.   
  75. @end  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值