【问题标题】:UIPickerView Text Color Change In This Implementation此实现中的 UIPickerView 文本颜色更改
【发布时间】:2015-01-21 21:35:25
【问题描述】:

大约一年半前,我有一个应用程序,我在开玩笑,我决定重新使用它。当我离开它时,pickerview 是可见的并且可以工作。现在它仍然有效,但文本颜色与背景一起为黑色。如何使用这些函数获得不同的文本颜色?

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if (component == SIZE)
        return [arraySize count];
    if (component == MEASURE)
        return [arrayMeasure count];
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == SIZE)
        return [arraySize objectAtIndex:row];
    if (component == MEASURE)
        return [arrayMeasure objectAtIndex:row];
    return 0;
}

【问题讨论】:

    标签: ios iphone uipickerview


    【解决方案1】:

    您可以更改选择器中每个标签的颜色。请参阅此线程以获取答案:can I change the font color of the datePicker in iOS7?

    【讨论】:

      【解决方案2】:

      添加此方法可能会对您有所帮助。

      - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
      {
          NSAttributedString *attString  = nil;
          NSString *title = @"";
      
          if (component == SIZE)
              title = [arraySize objectAtIndex:row];
          if (component == MEASURE)
              title = [arrayMeasure objectAtIndex:row];
         attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];
      
         return attString;
      
      }
      

      【讨论】:

        【解决方案3】:

        要更改标题的颜色,请使用:

        - (NSAttributedString *)pickerView:(UIPickerView *)pickerView
                     attributedTitleForRow:(NSInteger)row
                              forComponent:(NSInteger)component
        

        这使您可以为标题提供属性字符串。

        示例

        - (NSAttributedString *)pickerView:(UIPickerView *)pickerView  
                     attributedTitleForRow:(NSInteger)row 
                              forComponent:(NSInteger)component
        {
            return [[NSAttributedString alloc] initWithString:@"Your title"              
                                                   attributes:@
            {
                NSForegroundColorAttributeName:[UIColor redColor]
            }];
        }
        

        【讨论】:

        • 我已经尝试过了,但它只是将我所有的pickerview选择更改为“你的标题”。
        • Your title 只是一个例子。根据行索引将其替换为所需的字符串值。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-14
        • 1970-01-01
        • 2013-02-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多