How to set the text alignment in picker view iOS

前端 未结 4 1187
臣服心动
臣服心动 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: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

提交回复
热议问题