中心UIPickerView文本

所以我有一个只包含数字0-24的行的uipickerview,它看起来有点傻,因为数字左alignment,在pickerview的右侧留下了巨大的差距。

有一个简单的方法来居中alignmentuipickerview中的文本?

- (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; } 

或者类似的东西

现在在iOS 6中稍微容易一些…你可以实现一个新的方法来返回一个属性string…你可以在属性string中定义alignment方式。

 - (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; } 

你可以实现这个委托方法,它返回一个CGFloat

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

这在选取器视图上被调用以确定行宽。

下面的一个也工作正常 –

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)]; lbl.text = [reservePickerArray objectAtIndex:row]; lbl.adjustsFontSizeToFitWidth = YES; lbl.textAlignment=UITextAlignmentCenter; lbl.font=[UIFont systemFontOfSize:20]; return lbl; } 

干杯!!

select器没有设置alignment能力的属性。

所以你需要为此进行硬编码。

看到这个代码

 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { return 25; } - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { NSString *title=[NSString stringWithFormat:@" %i",row];//here you give extra spaces. return title; } 

请记住,在中的观点

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 

实际上是一个UITableViewCell,所以你可以像它一样使用它:

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { static NSString *cellIdentifier = @"pickerViewCell"; UITableViewCell *cell = (UITableViewCell*)view; if(cell==nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; /** customize the cell **/ } /** set the label **/ cell.textLabel.text = [dataSource objectAtIndex:row]; return cell; } 

我知道这个问题没有标记为swift,但为了万一有人正在寻找Swift 3.0版本,在这里。

 /* Called by the picker view when it needs the view to use for a given row in a given component. If the previously used view (the view parameter) is adequate, return that. If you return a different view, the previously used view is released. The picker view centers the returned view in the rectangle for row. */ func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { var label = UILabel(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(300), height: CGFloat(37))) let booking = self.bookingInfo[row] //bookingInfo is an array of elements label.text = String(booking.BookingNumber) + String(booking.DateAndTime) label.textAlignment = .left return label }