2011-01-31 19 views
12

的文本。我用下面的代碼來實現它。但問題是雖然漸變顏色出現在標籤上,但文本不可見。請幫助添加CGGradient作爲子層的UILabel隱藏我想漸變添加爲背景,以標籤的標籤

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]; 

回答

13

插入一個子層一個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]; 
+1

遐我知道,它得到上面的文字說:(...你能告訴我如何在back..I添加此層已經嘗試過這行你的建議,它憑着工作了。我改變了股指亦但無濟於事:)請大家幫忙,在此先感謝:) – Fatema 2011-02-01 05:19:21

+0

我不知道,它可能是你不能做到這一點的UILabels。您可以嘗試使用漸變創建UIView,然後將標籤添加爲子視圖。 – 2011-02-01 15:48:07

-4

我認爲你可以通過調整上述代碼中的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]; 

我能夠通過這樣做獲得漸變效果。

2

建議的答案有一個UIView內的UILabel工作。顯然,UILabels在給出背景漸變顏色背景之後不能在文本中包含文本......不知道爲什麼......

但是繼承瞭解決方案的完整代碼....希望這可以幫助某人:)

UIView *EnvironmentalsLabelView = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 320, 20)]; 
CAGradientLayer *gradient = [CAGradientLayer layer]; 
gradient.frame = EnvironmentalsLabelView.bounds; 
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor darkGrayColor]CGColor], (id)[[UIColor blackColor]CGColor], nil]; 
[EnvironmentalsLabelView.layer insertSublayer:gradient atIndex:0]; 
[scroller addSubview:EnvironmentalsLabelView]; 

UILabel *EnviornmentalsLabelText = [[UILabel alloc] initWithFrame:EnvironmentalsLabelView.bounds]; 
[EnviornmentalsLabelText setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12.0f]]; 
EnviornmentalsLabelText.textAlignment = NSTextAlignmentCenter; 
EnviornmentalsLabelText.backgroundColor = [UIColor clearColor]; 
EnviornmentalsLabelText.text = @"Environmental Benefits"; 
[EnvironmentalsLabelView addSubview:EnviornmentalsLabelText]; 

編碼愉快!!!!

1

還有一個額外的認爲正確的答案。 如果您是使用地點自動版式自定義視圖,你可以得到這個問題 - http://prntscr.com/5tj7bx

所以你的觀點有不同的大小,則子視圖 - 的UILabel和子 - 梯度層。

我解決了這個問題添加一個方法

class wResultView: UIView { 
var label = UILabel() 
var gradientLayer = CAGradientLayer() 

override func layoutSubviews() { 
    gradientLayer.frame = self.bounds 
    label.frame = self.bounds 
} 
......