美文网首页
自定义UIPickerView中row的字体样式

自定义UIPickerView中row的字体样式

作者: 牛程程 | 来源:发表于2018-02-05 22:18 被阅读0次

UIPickerView

A view that uses a spinning-wheel or slot-machine metaphor to show one or more sets of values.

UIPickerView显示用户操纵选择项目的一个或多个轮子。 每个轮子(称为组件)都有一系列表示可选项目的索引行。 每行显示一个字符串或视图,以便于用户可以识别该行上的项目。 用户通过将轮子旋转到与选择指示器对齐中所需要的值来选择项目。

下面是自定义样式的主要代码片段:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *str = nil;
    if(self.dataArray.count > row)
    {
      str = self.dataArray[row];
    }
    else
    {
      NSLog(@"the row of dataArray is nil.");
    }
    return str;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //自定义Label设置pickerview展示数据的样式。同理也可以自定义view显示图标+文字。
    UILabel* pickerLabel = (UILabel*)view;

    if (pickerLabel == nil)
    {
        pickerLabel = [UILabel new];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        pickerLabel.backgroundColor = [UIColor lightGrayColor];
        pickerLabel.font = [UIFont systemFontOfSize:15];
    }

    //调用委托方法,获取需要显示的数据。
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

相关文章

网友评论

      本文标题:自定义UIPickerView中row的字体样式

      本文链接:https://www.haomeiwen.com/subject/gjeizxtx.html