How to set the text alignment in picker view iOS

前端 未结 4 1180
臣服心动
臣服心动 2021-01-20 22:21

I need to set the alignment of the text in picker view but my solutions doesn\'t work. This is my code so far. I used array to store values on the picker view.



        
相关标签:
4条回答
  • 2021-01-20 22:46

    i done with this code for UIlabel text show in center of tableView like:-

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
    
            if (component == 0) {
    
                label.font=[UIFont boldSystemFontOfSize:22];
                label.textAlignment = UITextAlignmentCenter;
                label.backgroundColor = [UIColor clearColor];
    
            label.text = [NSString stringWithFormat:@"%@", [source objectAtIndex:row]];
            label.font=[UIFont boldSystemFontOfSize:22];
    
            }
        [label autorelease];
        return label;
     }
    

    Code output is

    enter image description here

    0 讨论(0)
  • 2021-01-20 22:48

    For swift 3 we can use

    label.textAlignment = NSTextAlignment.center
    
    0 讨论(0)
  • 2021-01-20 22:50

    Try this:

    label.textAlignment = NSTextAlignmentCenter;
    
    0 讨论(0)
  • 2021-01-20 22:54

    use this code instead of your code..

    -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
        UIView *viewTemp = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 44)]autorelease];
        [viewTemp setBackgroundColor:[UIColor clearColor]];
    
        UILabel *lbl = [[[UILabel alloc]initWithFrame:CGRectMake(5, 0, 300, 37)]autorelease];
        [lbl setBackgroundColor:[UIColor clearColor]];
        [lbl setFont:[UIFont boldSystemFontOfSize:22]];
        lbl.text = @"Some Text";
        [lbl setTextAlignment:UITextAlignmentCenter];
        [viewTemp addSubview:lbl];
        return viewTemp;
    }
    

    Here i return one UIView in which i add UILable see above code so your whole view display with UILable properly and also set backGroundColor of UIView is clear so its display layout of UIPickerView.

    I also display star like text in this UIPickerView.

    See images Bellow..

    enter image description hereenter image description here

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