【问题标题】:Wrapping of text in UIPickerViewUIPickerView 中的文本换行
【发布时间】:2014-10-31 13:29:23
【问题描述】:

我有一个UIPickerView,其中没有显示全文。最后会被截断。

如何以 2 行或更多行显示文本?

我尝试了以下代码,但它仍然显示为单行并且不是完整的文本。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    // Fill the label text here
    NSString *title = @"";

    UILabel* tView = (UILabel*)view;
    if (!tView){
        CGRect frame = CGRectZero;
        tView = [[UILabel alloc] initWithFrame:frame];
        [tView setFont:[UIFont fontWithName:@"Helvetica" size:15]];
        tView.minimumScaleFactor = 9.0f;
        tView.adjustsFontSizeToFitWidth = YES;
        tView.numberOfLines = 2;

        [tView setText:title];
        [tView setTextAlignment:NSTextAlignmentLeft];
    }

    return tView;
}

【问题讨论】:

    标签: ios ios7 uipickerview


    【解决方案1】:

    当我通过使用 UIFont 创建类别类为应用设置自定义字体时,UIPickerView 中的文本显示为两行。

    @implementation UIFont (CustomFont)
    
    +(UIFont *)regularFontWithSize:(CGFloat)size
    {
        return [UIFont fontWithName:GOATHAM_FONT size:size];
    }
    
    +(UIFont *)boldFontWithSize:(CGFloat)size
    {
        return [UIFont fontWithName:GOATHAM_FONT size:size];
    }
    
    +(void)load
    {
        SEL original = @selector(systemFontOfSize:);
        SEL modified = @selector(regularFontWithSize:);
        SEL originalBold = @selector(boldSystemFontOfSize:);
        SEL modifiedBold = @selector(boldFontWithSize:);
    
        Method originalMethod = class_getClassMethod(self, original);
        Method modifiedMethod = class_getClassMethod(self, modified);
        method_exchangeImplementations(originalMethod, modifiedMethod);
    
        Method originalBoldMethod = class_getClassMethod(self, originalBold);
        Method modifiedBoldMethod = class_getClassMethod(self, modifiedBold);
        method_exchangeImplementations(originalBoldMethod, modifiedBoldMethod);
    }
    
    @end
    

    另外,我在 UIPickerView 委托方法中设置了字体。

     - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
        NSString *title = @"";
    
        UILabel* tView = (UILabel*)view;
        if (!tView){
            CGRect frame = CGRectZero;
            tView = [[UILabel alloc] initWithFrame:frame];
            [tView setFont:[UIFont fontWithName:GOATHAM_FONT size:14]];
            tView.minimumScaleFactor = 9.0f;
            tView.adjustsFontSizeToFitWidth = YES;
            tView.numberOfLines = 2;
    
            [tView setText:title];
            [tView setTextAlignment:NSTextAlignmentLeft];
        }
    
        return tView;
    }
    

    执行此操作后,文本在 UIPickerView 中显示为两行。

    【讨论】:

    • 这个过程似乎对我不起作用:/你可能知道为什么吗?
    • 你在使用系统字体中的其他字体吗?
    猜你喜欢
    • 2021-12-03
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多