Center UIPickerView Text

后端 未结 9 1548
清歌不尽
清歌不尽 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:04

    A little easier now in iOS 6... There's a new method you can implement to return an attributed string... And you can define alignment in attributed strings.

    - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
        NSString *text = [NSString stringWithFormat:@"R%d C%d", row, component];
        NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithString:text];
        NSMutableParagraphStyle *mutParaStyle=[[NSMutableParagraphStyle alloc] init];
        mutParaStyle.alignment = NSTextAlignmentCenter;
        [as addAttribute:NSParagraphStyleAttributeName value:mutParaStyle range:NSMakeRange(0,[text length])];
        return as;
    }
    

提交回复
热议问题