我想添加渐变作为标签的背景.我使用以下代码来实现这一点.但问题是虽然渐变颜色出现在标签上,但文字不可见.请帮忙
lblPatientDetail.text=PatientsDetails;
lblPatientDetail.textColor=[UIColor blackColor];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = lblPatientDetail.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor],(id)[[UIColor colorWithRed:255/255.0 green:239/255.0 blue:215/255.0 alpha:1.0] CGColor],nil];
[lblPatientDetail.layer addSublayer:gradient];
lblPatientDetail.backgroundColor=[UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)
Jam*_*s P 21
将子图层插入UILabel会隐藏文本,因此获得所需内容的最佳方法是将标签和渐变图层添加到UIView中.
UIView *gradientLabelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = gradientLabelView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor],(id)[[UIColor colorWithRed:255/255.0 green:239/255.0 blue:215/255.0 alpha:1.0] CGColor],nil];
[gradientLabelView.layer addSublayer:gradient];
lblPatientDetail.frame = gradientLabelView.bounds;
lblPatientDetail.backgroundColor = [UIColor clearColor];
[gradientLabelView addSubview:lblPatientDetail];
[self addSubview:gradientLabelView];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17250 次 |
| 最近记录: |