HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 带圆角,投影和背景图案的UILabel大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在尝试我发现的每一种方法,但我无法做到.我只想制作一个带圆角的标签,一个带背景图案的投影.只有当我不想要圆角时,阴影才有效.我不能把它们放在一起!

这是我带阴影的代码

label.text = msg;
label.textAlignment = UITextAlignmentCenter;
label.frame = CGRectMake(20,10,280,40);
label.BACkgroundColor 
    = [[UIColor alloc] initWithPatternImage:[UIImage imagenamed:@"msg_Box_bg.png"]];

[label.layer setCornerRadius:10];
[label.layer setMasksToBounds:NO];

/* Shadow */
label.layer.shadowColor = [UIColor blackColor].CGColor;
label.layer.shadowOpacity = 0.6;
label.layer.shadowOffset = CGSizeMake(0,0);
label.layer.shadowRadius = 3;

这给了我没有圆角的阴影.但是,如果我使用

[label.layer setMasksToBounds:YES];

这将给我圆角,没有阴影.我已经建议使用阴影路径,所以带阴影路径的代码如下所示:

label.text = msg;
label.textAlignment = UITextAlignmentCenter;
label.frame = CGRectMake(20,40);
label.BACkgroundColor 
    = [[UIColor alloc] initWithPatternImage:[UIImage imagenamed:@"msg_Box_bg.png"]];

[label.layer setCornerRadius:10];
[label.layer setMasksToBounds:YES];

/* Shadow */
label.layer.shadowColor = [UIColor blackColor].CGColor;
label.layer.shadowOpacity = 0.6;
label.layer.shadowOffset = CGSizeMake(0,0);
label.layer.shadowRadius = 3;
label.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:label.frame cornerRadius:10]CGPath];
label.layer.shouldRasterize = YES;

这段代码确实给了我圆角但没有阴影.
有什么建议?

谢谢!

解决方法

我使用下面的代码来获得您所追求的结果.
CGSize size = CGSizeMake(280,40);

/** Shadow */
CALayer *shadowLayer = [CALayer new];
shadowLayer.frame = CGRectMake(20,100,size.width,size.height);
shadowLayer.cornerRadius = 10;

shadowLayer.BACkgroundColor = [UIColor clearColor].CGColor; 
shadowLayer.shadowColor = [UIColor blackColor].CGColor;
shadowLayer.shadowOpacity = 0.6;
shadowLayer.shadowOffset = CGSizeMake(0,0);
shadowLayer.shadowRadius = 3;

/** Label */
UILabel *label = [UILabel new];
label.text = @"Hello World";
label.textAlignment = UITextAlignmentCenter;
label.frame = CGRectMake(0,size.height);
label.BACkgroundColor = [UIColor clearColor]; 
label.layer.cornerRadius = 10;
[label.layer setMasksToBounds:YES];
//  customLabel.BACkgroundColor = [UIColor whiteColor]; 
label.BACkgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imagenamed:@"options.png"]];

/** Add the Label to the shawdow layer */
[shadowLayer addSublayer:label.layer];

[self.view.layer addSublayer:shadowLayer];      

[shadowLayer release];

大佬总结

以上是大佬教程为你收集整理的ios – 带圆角,投影和背景图案的UILabel全部内容,希望文章能够帮你解决ios – 带圆角,投影和背景图案的UILabel所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。