How can I change text font in UIPickerView in iOS 7?

前端 未结 8 1556
清酒与你
清酒与你 2020-12-09 14:55

I was able to change my font color but I also need to change font size, how can I accomplish that? Here\'s my code for chaning the color,

 - (NSAttributedStr         


        
相关标签:
8条回答
  • 2020-12-09 15:12
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        UILabel* tView = (UILabel*)view;
        if (!tView)
        {
           tView = [[UILabel alloc] init];
           [tView setFont:[UIFont fontWithName:@"Helvetica" size:14]];
           //[tView setTextAlignment:UITextAlignmentLeft];
           tView.numberOfLines=3;
        }
        // Fill the label text here
        tView.text=[wishvalues objectAtIndex:row];
       return tView;
    }
    
    0 讨论(0)
  • 2020-12-09 15:12

    Here's the Swift version tested on iOS8:

    Update in Swift for iOS8, you can add this to your delegate:

    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
    
        var pickerLabel = view as? UILabel;
    
        if (pickerLabel == nil)
        {
            pickerLabel = UILabel()
    
            pickerLabel?.font = UIFont(name: "Montserrat", size: 16)
            pickerLabel?.textAlignment = NSTextAlignment.Center
        }
    
        pickerLabel?.text = fetchLabelForRowNumber(row)
    
        return pickerLabel!;
    }
    
    0 讨论(0)
  • 2020-12-09 15:12

    Updated for Swift 4:

    public func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label = view as? UILabel ?? UILabel()
        label.font = .systemFont(ofSize: 16)
        label.textColor = .white
        label.textAlignment = .center
        label.text = text(for: row, for: component)
        return label
    }
    
    0 讨论(0)
  • 2020-12-09 15:14

    Thanks for @Richard Bown

    Would this be a better answer for Swift?

    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
            if let titleLabel = view as? UILabel {
                titleLabel.text = "Your Text"
                return titleLabel
            } else {
                let titleLabel = UILabel()
                titleLabel.font = UIFont.boldSystemFontOfSize(16)//Font you want here
                titleLabel.textAlignment = NSTextAlignment.Center
                titleLabel.text = "Your Text"
                return titleLabel
            }
        }
    
    0 讨论(0)
  • 2020-12-09 15:22

    You need to implement pickerView:viewForRow:forComponent:reusingView: method in picker's delegate

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
        UILabel* lbl = (UILabel*)view;
        // Customise Font 
        if (lbl == nil) {
              //label size
              CGRect frame = CGRectMake(0.0, 0.0, 70, 30);
    
              lbl = [[UILabel alloc] initWithFrame:frame];
    
              [lbl setTextAlignment:UITextAlignmentLeft];
    
              [lbl setBackgroundColor:[UIColor clearColor]];
               //here you can play with fonts
              [lbl setFont:[UIFont fontWithName:@"Times New Roman" size:14.0]];
    
       }
          //picker view array is the datasource
       [lbl setText:[pickerViewArray objectAtIndex:row]];
    
    
            return lbl;
    }
    
    0 讨论(0)
  • 2020-12-09 15:25
    let textLabel = view as? UILabel ?? {
        let label = UILabel()
        label.font = UIFont.boldSystemFontOfSize(16)
        label.textAlignment = .Center
        return label
    }()
    textLabel.text = Array(componentDataSources[component].keys)[row]
    return textLabel
    
    0 讨论(0)
提交回复
热议问题