2014-10-31 117 views
0

我有一个UIPickerView,其中没有显示全文。它在最后被截断。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; 
} 

回答

0

当我通过与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中的两行。

+0

这个过程似乎不适合我:/你可能知道为什么吗? – achi 2015-09-09 23:35:51

+0

你是否使用系统字体中的其他字体? – 2015-09-10 07:10:17

+0

是的,iOS中包含一种字体 – achi 2015-09-10 15:48:58