Iphone 将CGGradient作为子层添加到UILabel会隐藏标签的文本
我想添加渐变作为标签的背景。我使用以下代码实现了这一点。但问题是,虽然渐变颜色出现在标签上,但文本不可见。请帮忙Iphone 将CGGradient作为子层添加到UILabel会隐藏标签的文本,iphone,uilabel,Iphone,Uilabel,我想添加渐变作为标签的背景。我使用以下代码实现了这一点。但问题是,虽然渐变颜色出现在标签上,但文本不可见。请帮忙 lblPatientDetail.text=PatientsDetails; lblPatientDetail.textColor=[UIColor blackColor]; CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = lblPatientDetail.bounds; gradie
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];
将子层插入到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];
我想你可以通过调整上面代码中的alpha值来创建渐变色
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor],(id)[[UIColor colorWithRed:255/255.0 green:239/255.0 blue:215/255.0 alpha:0.1] CGColor],nil];
通过这样做,我能够获得渐变效果。在UIView中使用UILabel的建议答案有效。显然,在给背景加上渐变色背景后,UILabel中不能有文本。。。不知道为什么 但这是解决方案的完整代码。。。。希望这对某人有帮助:)
编码快乐 再思考一次来纠正答案。 如果使用自动布局来放置自定义视图,则可能会出现以下问题- 因此,视图的大小不同于子视图-UILabel和子层-渐变层 我通过增加一种方法来解决这个问题
class wResultView: UIView {
var label = UILabel()
var gradientLayer = CAGradientLayer()
override func layoutSubviews() {
gradientLayer.frame = self.bounds
label.frame = self.bounds
}
......
您可以在
UILabel
中添加UILabel
像这样
class GradientLabel: UILabel {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.clipsToBounds = true
DispatchQueue.main.async {
self.applyGradient(with: Color.gradientColor, gradient: GradientOrientation.topLeftBottomRight)
self.textColor = UIColor.white
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
label.text = self.text
label.font = self.font
label.textColor = self.textColor
label.textAlignment = self.textAlignment
self.text = ""
self.addSubview(label)
}
}
}
extension UIView {
func applyGradient(with colours: [UIColor], locations: [NSNumber]? = nil) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.locations = locations
self.layer.insertSublayer(gradient, at: 0)
}
func applyGradient(with colours: [UIColor], gradient orientation: GradientOrientation) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
self.layer.insertSublayer(gradient, at: 0)
}
}
是的,我知道它被添加到文本上方:(…你能告诉我如何在后面添加这一层吗..我已经试过你建议的这一行了。它不起作用。我也更改了索引,但没有效果:)请帮助,提前谢谢:)我不确定,可能是你不能在UILabels上这样做。您可以尝试创建带有渐变的UIView,然后将标签添加为子视图。感谢James,我尝试了您的第二个选项:)在UIView上添加渐变,然后将标签添加为子视图:-)将修改后的UILabel的背景色更改为[UIColor clearColor]。如果不使用UILabel,则背景默认为whitebackground:p我们可以在不使用额外视图的情况下执行相同的操作吗
class GradientLabel: UILabel {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.clipsToBounds = true
DispatchQueue.main.async {
self.applyGradient(with: Color.gradientColor, gradient: GradientOrientation.topLeftBottomRight)
self.textColor = UIColor.white
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
label.text = self.text
label.font = self.font
label.textColor = self.textColor
label.textAlignment = self.textAlignment
self.text = ""
self.addSubview(label)
}
}
}
extension UIView {
func applyGradient(with colours: [UIColor], locations: [NSNumber]? = nil) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.locations = locations
self.layer.insertSublayer(gradient, at: 0)
}
func applyGradient(with colours: [UIColor], gradient orientation: GradientOrientation) {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
self.layer.insertSublayer(gradient, at: 0)
}
}