2014-10-31 120 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