Center UIPickerView Text

后端 未结 9 1526
清歌不尽
清歌不尽 2020-12-25 10:27

So I have a uipickerview with rows that only contain the number 0-24 and it looks a bit silly since the numbers are left aligned leaving a huge gap on the right of the picke

相关标签:
9条回答
  • 2020-12-25 11:22

    Just set the width to the width of the view and the text will center automatically:

    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
        return self.view.bounds.size.width; 
    }
    
    0 讨论(0)
  • 2020-12-25 11:24
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
        label.text = [NSString stringWithFormat:@"something here"];
        label.textAlignment = NSTextAlignmentCenter; //Changed to NS as UI is deprecated.
        label.backgroundColor = [UIColor clearColor];
        [label autorelease];
        return label;
    }
    

    or something like this.

    0 讨论(0)
  • 2020-12-25 11:30

    You can implement this delegate method, which returns a CGFloat

    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

    This is called on the picker view to determine row width.

    0 讨论(0)
提交回复
热议问题