居中 UIPickerView 文本

发布于 2024-10-18 03:52:58 字数 127 浏览 9 评论 0原文

所以我有一个 uipickerview,其中的行仅包含数字 0-24,它看起来有点傻,因为数字左对齐,在 pickerview 的右侧留下了巨大的间隙。

有没有一种简单的方法可以在 uipickerview 中居中对齐文本?

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 pickerview.

Is there an easy way to center align text in a uipickerview?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

来世叙缘 2024-10-25 03:52:58
- (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;
}

或类似的东西。

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

or something like this.

风吹雪碎 2024-10-25 03:52:58

现在在 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;
}

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;
}
时光磨忆 2024-10-25 03:52:58

您可以实现此委托方法,该方法返回 CGFloat

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

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

You can implement this delegate method, which returns a CGFloat

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

This is called on the picker view to determine row width.

初吻给了烟 2024-10-25 03:52:58

下面的一个也工作正常 -

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

干杯!

Below one also working fine -

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

Cheers!!

桃气十足 2024-10-25 03:52:58

请记住,其中的视图

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

Remember, the view in

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

is actually a UITableViewCell, so you can work with it like:

- (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;
}
夜司空 2024-10-25 03:52:58

只需将宽度设置为视图的宽度,文本就会自动居中:

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return self.view.bounds.size.width; 
}

Just set the width to the width of the view and the text will center automatically:

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return self.view.bounds.size.width; 
}
梨涡 2024-10-25 03:52:58

我知道这个问题没有标记为 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
}

I know this question is not tagged for swift, but just in case somebody is looking for the Swift 3.0 version, here it is.

/* 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
}
原来分手还会想你 2024-10-25 03:52:58

将 James Boucher 现代化为 swift 5。
我在 OP 直接在 attributeTitleForRow 中进行归因时进行预计算

    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
    }

Modernized James Boutcher to swift 5.
I precompute while the OP does an attribution directly in 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
    }
请止步禁区 2024-10-25 03:52:58

我花了很长时间才意识到我只是缺少这一行代码:

pickerView.autoresizingMask = .flexibleWidth

It took me a long time to realize I'm just missing this single line of code:

pickerView.autoresizingMask = .flexibleWidth

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文