Center UIPickerView Text

后端 未结 9 1525
清歌不尽
清歌不尽 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;
    }
    
    0 讨论(0)
  • 2020-12-25 11:05

    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!!

    0 讨论(0)
  • 2020-12-25 11:07

    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
        }
    
    0 讨论(0)
  • 2020-12-25 11:07

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

    pickerView.autoresizingMask = .flexibleWidth

    0 讨论(0)
  • 2020-12-25 11:10

    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;
    }
    
    0 讨论(0)
  • 2020-12-25 11:15

    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
    }
    
    0 讨论(0)
提交回复
热议问题