【问题标题】:UIPickerView FontUIPickerView 字体
【发布时间】:2011-07-27 19:06:41
【问题描述】:

有谁知道是否可以更改单个 UIPickerView 项的字体和/或颜色?

【问题讨论】:

    标签: iphone cocoa-touch ios ipad


    【解决方案1】:
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
        UILabel* tView = (UILabel*)view;
        if (!tView){
            tView = [[UILabel alloc] init];
                // Setup label properties - frame, font, colors etc
                ...
        }
        // Fill the label text here
        ...
        return tView;
    }
    

    【讨论】:

      【解决方案2】:

      是的,这是可能的。为此,您需要在选择器的委托中实现 pickerView:viewForRow:forComponent:reusingView: 方法 - 在此处创建 UILabel 实例并使用您想要的任何属性对其进行设置。

      【讨论】:

      • 很好,但是你怎么知道标签的默认属性而不用猜测呢?
      【解决方案3】:

      为此,我会简单地使用属性字符串。

      实现这个方法是你的委托:

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

      【讨论】:

      • 试过这个;没有工作(iOS 9.3),虽然它被调用了。
      • 它适用于 iOS 9.3。你确定你正确地创建了 NSAttributedString 吗?
      • 我肯定做错了什么,虽然我看不出是什么。最后我按照其他答案中的建议做了,并返回了一个配置的标签。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 2015-06-26
      • 2011-12-30
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多