zoukankan      html  css  js  c++  java
  • UIView显示圆角边框 并显示阴影

    //自定义一个继承UIView的控件
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 50)];
        [nameLabel setText:@"不否认你的情绪化"];
        [nameLabel setTextAlignment:NSTextAlignmentCenter];
        [nameLabel setBackgroundColor:[UIColor cyanColor]];
        nameLabel.layer.borderColor = [UIColor redColor].CGColor;
        [nameLabel.layer setBorderWidth:4];
        [shadow addSubview:nameLabel];//把圆角的nameLabel放到一个大小与它一样的的UIView中
        [nameLabel release];
    

    让UIView显示圆角很简单 只要三行代码就行

      CALayer *layer = [nameLabel layer];
      [layer setMasksToBounds:YES];
      [layer setCornerRadius:9];

    如果用传统的方法加阴影是加不上的,传统方法是

        [nameLabel.layer setShadowColor:[UIColor blackColor].CGColor];
        [nameLabel.layer setShadowOffset:CGSizeMake(5, 5)];
        [nameLabel.layer setShadowOpacity:1];
    

    因为setMasksToBounds表示对frame外的内容进行了裁减,只可显示frame内的内容。由于这种方法加的阴影在frame外,所以被裁减了。

    传统方法不行,那我们可以把圆角的nameLabel放到一个大小与它一样的的UIView中,让这个view有阴影,那效果看起来就一样了。

        UIView *shadow = [[UIView alloc]initWithFrame:CGRectMake(20, 50, 150, 50)];
        [shadow.layer setShadowColor:[UIColor blackColor].CGColor];
        [shadow.layer setShadowOffset:CGSizeMake(5, 5)];//设置阴影的偏移大小
        [shadow.layer setShadowOpacity:1];//设置阴影的透明度 默认是0
    //    [shadow.layer setShadowRadius:9];//设置阴影的半径
    //    [shadow.layer setCornerRadius:9];
        [shadow setClipsToBounds:NO];//默认是NO 设置成YES
        [self.view addSubview:shadow];
        [shadow release];
        
        //自定义一个继承UIView的控件 加到和它等大小的设置好阴影的UIView上
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 50)];
        [nameLabel setText:@"爱上一匹野马"];
        [nameLabel setTextAlignment:NSTextAlignmentCenter];
        [nameLabel setBackgroundColor:[UIColor cyanColor]];
        nameLabel.layer.borderColor = [UIColor redColor].CGColor;
        [nameLabel.layer setBorderWidth:4];
        [shadow addSubview:nameLabel];//把圆角的nameLabel放到一个大小与它一样的的UIView中
        [nameLabel release];
        
        //nameLabel显示圆角
        CALayer *layer = [nameLabel layer];
        [layer setMasksToBounds:YES];//setMasksToBounds表示对frame外的内容进行了裁减,只可显示frame内的内容
        [layer setCornerRadius:9];
    

     成功!!!

     转自http://blog.csdn.net/devday?viewmode=contents

  • 相关阅读:
    Linux新用户创建与删除细节详解
    通过windows远程访问linux桌面的方法(简单)
    物理机网络地址配置原理
    Hive安装中metadata初始化问题
    彻底理解Promise对象——用es5语法实现一个自己的Promise(上篇)
    基于react+react-router+redux+socket.io+koa开发一个聊天室
    深入探析koa之异步回调处理篇
    深入探析koa之中间件流程控制篇
    【踩坑记录】一个新手几乎都踩过的坑...
    NodeJS优缺点及适用场景讨论
  • 原文地址:https://www.cnblogs.com/lxllanou/p/3676555.html
Copyright © 2011-2022 走看看