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

  • 相关阅读:
    Discuz! X3 全新安装图文教程
    https://blog.csdn.net/doegoo/article/details/50749817
    docker 网络
    Docker的网络类型和固定IP设置
    docker lamp
    MongoDB分片搭建
    iptables原理及使用教程
    VNC服务安装、配置与使用
    ssh免密码登陆
    nmap
  • 原文地址:https://www.cnblogs.com/lxllanou/p/3676555.html
Copyright © 2011-2022 走看看