这是我带阴影的代码:
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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)