如何更改 UIPickerView 中的字体大小?

Posted

技术标签:

【中文标题】如何更改 UIPickerView 中的字体大小?【英文标题】:How to change the Font size in UIPickerView? 【发布时间】:2011-11-03 08:49:12 【问题描述】:

我有一个 UIPickerView。这有近 200 个项目,每个项目都有长文本,所以,我想调整 UIPickerView 的字体大小。我怎样才能改变它?有可能的? 谁能帮我?谢谢!

Yuva.M

【问题讨论】:

检查***.com/questions/256556/… 有可能,但我认为 UIPickerView 不应该显示长文本。用户阅读长文本将是乏味的,即使它们的数量为 200。也许你可以重新考虑你的设计。 谢谢 Narayanan Ramamoorthy 和 Akshay,我解决了这个问题。非常感谢您的回复.. 【参考方案1】:

你需要在picker的委托中实现pickerView:viewForRow:forComponent:reusingView:方法

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    UILabel* tView = (UILabel*)view;
    if (!tView)
        tView = [[UILabel alloc] init];
            // Setup label properties - frame, font, colors etc
            ...
    
    // Fill the label text here
    ...
    return tView;

【讨论】:

【参考方案2】:

试试这个,应该会有帮助:

  - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
UILabel* tView = (UILabel*)view;
if (!tView)
tView = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
...
//adjustsFontSizeToFitWidth property to YES
tView.minimumFontSize = 8.;
tView.adjustsFontSizeToFitWidth = YES;

// Fill the label text here
...
return tView;



// altro modo completo sembrerebbe...
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view 

UILabel *pickerLabel = (UILabel *)view;

if (pickerLabel == nil) 
CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
[pickerLabel setTextAlignment:UITextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];


[pickerLabel setText:[pickerDataArray objectAtIndex:row]];

return pickerLabel;


【讨论】:

请注意 .minimumFontSize 已被弃用,它应该被替换为 .setMinimumScaleFactor。 使用 NSTextAlignmentLeft,因为 UITextAlignmentLeft 在 ios 6.0 中已被弃用 [pickerLabel setTextAlignment:NSTextAlignmentLeft];【参考方案3】:

UIPickerView Rows 的字体调整

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

    UILabel* pickerLabel = (UILabel*)view;

    if (!pickerLabel)
    
        pickerLabel = [[UILabel alloc] init];

        pickerLabel.font = [UIFont fontWithName:@"SourceSansPro-Semibold"                size:16];

        pickerLabel.textAlignment=NSTextAlignmentCenter;
    
    [pickerLabel setText:[self.data objectAtIndex:row]];

    return pickerLabel;

【讨论】:

【参考方案4】:

在 iOS8 的 Swift 中更新,您可以将其添加到您的委托:

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

【讨论】:

只有当我执行类似...pickerLabel?.font = UIFont.systemFont(ofSize: 20)【参考方案5】:

对于目标 c

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

    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel)
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        [pickerLabel setFont:[UIFont fontWithName:LATO_REGULAR_FONT size:SIZE_SEMIBOLD_FONT]];
        pickerLabel.textColor = primaryTextColor;
        pickerLabel.textAlignment = NSTextAlignmentCenter;


    
    // Fill the label text here
    pickerLabel.text = self.dataSourceArray[row];

    return pickerLabel;

对于 Swift 2.3

  func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView

        var label = view as! UILabel!
        if label == nil 
            label = UILabel()
        

        label.font = LATO_REGULAR_FONT_17
        label.text =  dataArray[row] as? String
        label.textAlignment = .Center
        return label

    

【讨论】:

【参考方案6】:

斯威夫特 3 | 自动收缩

设置adjustsFontSizeToFitWidth=trueminimumScaleFactor=0.5

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView 

    var label: UILabel
    if let view = view as? UILabel  label = view 
    else  label = UILabel() 

    label.text = "My Picker Text"
    label.textAlignment = .center
    label.font = UIFont.boldSystemFont(ofSize: 20)
    label.adjustsFontSizeToFitWidth = true
    label.minimumScaleFactor = 0.5

    return label

【讨论】:

每行的行文本显示为 ...。嗯。 没有看到任何这些解决方案的外观变化。这确实将文本更改为...,运行代码也是如此。也许 UIPickerView 需要以某种方式调整大小或在 InterfaceBuilder 中进行设置? @codeslapper 是的,我认为 UIPickerView 的大小是导致文本显示为“...”的原因。尝试更改 yourPickerView.frame.size.width 或通过 Interface Builder 设置大小。【参考方案7】:

Swift 4.x

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView 
        var label = UILabel()
        if let v = view 
            label = v as! UILabel
        
        label.font = UIFont (name: "Helvetica Neue", size: 10)
        label.text =  dataArray[row]
        label.textAlignment = .center
        return label
    

【讨论】:

【参考方案8】:

斯威夫特 4: 更新@Alessandro Ornano 答案并避免 Force_cast 违规错误:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView 
    var label = UILabel()
    if let v = view as? UILabel  label = v 
    label.font = UIFont (name: "Helvetica Neue", size: 10)
    label.text =  dataArray[row]
    label.textAlignment = .center
    return label

【讨论】:

最佳答案总是在 SO 的底部!! 您在这里创建了一个不必要的标签...尝试更改 let label - (view as? Label) ?? UILabel() 或者更好地调用一些构建器变量,该变量会返回一个带有字体和对齐设置的新标签。那么您不必每次都设置它们。【参考方案9】:

要设置系统字体,需要做一些不同的事情,如下所示:

let pickerLabel = UILabel()
pickerLabel.font = UIFont.systemFont(ofSize: 20.0, weight: .medium)

您可以选择任何粗细,例如常规、粗体等

【讨论】:

【参考方案10】:

清洁 Swift 解决方案没有强制解包

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView 
    let label = (view as? UILabel) ?? UILabel()
    
    // setup label
    
    return label

【讨论】:

【参考方案11】:

大多数答案都提供了中心对齐标签的解决方案。我不得不将文本与左侧对齐,并面临文本已被截断的事实。向左添加边距为我解决了这个问题。解决方案适用于 swift 5。

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView 
        
        let pickerLabel: UILabel
        
        if let label = view as? UILabel 
            pickerLabel = label
         else 
            pickerLabel = UILabel()
            // Customize text
            pickerLabel.font = pickerLabel.font.withSize(20)
            pickerLabel.textAlignment = .left
            pickerLabel.textColor = UIColor.secondaryLabel
            // Create a paragraph with custom style
            // We only need indents to prevent text from being cut off
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.firstLineHeadIndent = 12 // May vary
            // Create a string and append style to it
            let attributedString = NSMutableAttributedString(string: subjects[row])
            attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
            // Update label's text
            pickerLabel.attributedText = attributedString
        
        
        return pickerLabel
    

【讨论】:

以上是关于如何更改 UIPickerView 中的字体大小?的主要内容,如果未能解决你的问题,请参考以下文章

如何在iOS 7中更改UIPickerView中的文本字体?

如何更改 uipickerview 标题文本中的字体样式? [复制]

PhoneGap:iOS 7 中的 UIPickerView 不会像在 iOS 6 中那样自动调整字体大小。关于如何在 iOS 7 中实现的任何解决方案?

Iphone Sdk:UIPickerView,可以为不同的组件分配不同的字体大小吗?

快速更改选择器的字体及其大小

如何根据 UIPickerview 选择运行代码?