所以我有一个uipickerview,其中的行只包含数字0-24而且看起来有点傻,因为数字左对齐,在pickerview的右边留下了巨大的空白。
是否有一种简单的方法可以在uipickerview中居中对齐文字?
答案 0 :(得分:67)
- (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;
}
或类似的东西。
答案 1 :(得分:21)
现在在iOS 6中稍微容易一点......你可以实现一种新的方法来返回一个属性字符串...你可以在属性字符串中定义对齐。
- (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;
}
答案 2 :(得分:12)
您可以实现此委托方法,该方法返回CGFloat
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
在拾取器视图上调用此方法以确定行宽。
答案 3 :(得分:4)
下面还可以正常工作 -
- (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;
}
干杯!!
答案 4 :(得分:3)
请记住,
中的视图- (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;
}
答案 5 :(得分:1)
只需将宽度设置为视图的宽度,文本将自动居中:
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return self.view.bounds.size.width;
}
答案 6 :(得分:0)
我知道这个问题没有标记为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
}
答案 7 :(得分:0)
现代化的詹姆斯·布彻迅速发展5。 我在OP直接在attributedTitleForRow中进行归因的同时进行了预计算
pickerData = pickerData.map {
let ps = NSMutableParagraphStyle()
ps.alignment = .center;
let paraAttributes:[NSAttributedString.Key : Any] = [ NSAttributedString.Key.paragraphStyle: ps]
let res = NSMutableAttributedString(attributedString: $0)
res.addAttributes(paraAttributes, range: NSRange(location: 0, length: res.length))
return res
}
答案 8 :(得分:0)
我花了很长时间才意识到我只是缺少了这一行代码:
pickerView.autoresizingMask = .flexibleWidth