簡體   English   中英

如何動態設置 UIlabel TextColor

[英]How to set UIlabel TextColor Dynamically

我使用此代碼創建了多個標簽,

.H文件

@interface ViewController : UIViewController
{
    NSArray * phraseAry ;
    UIView * containerView;
    UILabel * oldLabel;
    NSMutableArray *dataArray;
}
@property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;

.M 文件

- (void)viewDidLoad
{
    [super viewDidLoad];

    heightValue = 20;
    widthValue = 0;
    xValue = 5;
    yValue = 10;

containerView = [[UIView alloc] init];
    for (int i=0; i<phraseAry.count; i++) {
        widthValue = [self returnWidth:[phraseAry objectAtIndex:i]];

        int newXValue = xValue+widthValue+5;

        //NSLog(@"newXValue : %i",newXValue);
        if (newXValue > 310) {
            yValue +=20;
            xValue = 5;
            newXValue = xValue+widthValue+5;
            UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(xValue, yValue, widthValue, heightValue)];
            lbl.text = [phraseAry objectAtIndex:i];
            [lbl setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
            lbl.tag = i;
            lbl.textColor = [UIColor colorWithRed:(92/255.0) green:(109/255.0) blue:(43/255.0) alpha:1];
            [containerView addSubview:lbl];
            xValue = newXValue;
        } else {
            UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(xValue, yValue, widthValue, heightValue)];
            lbl.text = [phraseAry objectAtIndex:i];
            [lbl setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
            lbl.tag = i;
            lbl.textColor = [UIColor colorWithRed:(92/255.0) green:(109/255.0) blue:(43/255.0) alpha:1];
            [containerView addSubview:lbl];
            xValue = newXValue;
        }
    }

    containerView.frame = CGRectMake(0, 0, 320, yValue);
    //add code to customize, e.g. polygonView.backgroundColor = [UIColor blackColor];
    [self.myScrollView addSubview:containerView];
    self.myScrollView.contentSize = containerView.frame.size;
}

然后我使用此代碼在特定表中設置背景顏色和文本顏色

- (void)updateLabelMethod
 {
            oldLabel.backgroundColor = [UIColor clearColor];
            UILabel *label = (UILabel *)[containerView viewWithTag:2];
            oldLabel = label;
            label.backgroundColor = [UIColor colorWithRed:(98/255.0) green:(147/255.0) blue:(216/255.0) alpha:1];
            label.textColor = [UIColor whiteColor];
            containerView.backgroundColor = [UIColor clearColor];
}

它會很好地更新標簽的背景,但是當我使用此代碼更新 textcolor 時,它會顯示這樣的錯誤

由於未捕獲的異常“NSInvalidArgumentException”而終止應用程序,原因:“-[UIView setTextColor:]:無法識別的選擇器發送到實例 0xbaa3b30”

有什么方法可以設置文本顏色? 請幫忙。

從 1 而不是 0 開始你的標簽標簽,因為默認情況下,所有視圖的標簽值都是 0。

lbl.tag = i+1; 

因為

UILabel *label = (UILabel *)[containerView viewWithTag:0];  

它將返回containerView本身,而UIView沒有textColor屬性:P

[containerView viewWithTag:2];

檢查容器視圖只有一個帶有標簽 2 的標簽的視圖。可能是其他一些視圖設置了標簽 2 並可能導致問題。

NSLog(@"%@",label);

可以給你這里的標簽給出的輸出。它應該指出一個UILabel本身

使用isKindOfClass:確定它是您正在更新的標簽

    for (UILable *lblTemp in containerView.subviews){
     if ([lblTemp isKindOfClass:[UILable class]] && lblTemp.tag==2){
         // change the lbl color
        break;
     }
  }

使用此代碼檢查;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM