2013-12-20 66 views
31

我能夠改變我的字體顏色,但我也需要改變字體大小,我該怎麼做到這一點?這裏是我的chaning顏色代碼,如何在iOS 7中更改UIPickerView中的文本字體?

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    NSString *title = _currencyName[row]; 
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; 

    return attString; 
} 

UPDATE:這並不工作:

NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:40]}]; 

回答

68
- (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

我認爲你必須在你的attributes列表中添加NSFontAttributeName,你可以使用類方法fontWithName:size:UIFont

+0

我嘗試過,不工作:NSAttributedString * attString = [[NSAttributedString的alloc] initWithString:標題屬性:@ {NSForegroundColorAttributeName:[的UIColor whiteColor] NSFontAttributeName:[UIFont fontWithName:@「HelveticaNeue-Light」size:40]}]; – user3121912

+0

你有什麼想法嗎? – user3121912

+1

對不起,我的錯。 NSFontAttributeName適用於一系列文本。所以你必須添加類似'[attString addAttribute:NSFontAttributeName value:/ *你的字體* /範圍:NSMakeRange(0,attString.length)];' – zbMax

5

您需要實現pickerView:viewForRow:forComponent:reusingView:方法選擇器的委託

- (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; 
} 
4

您可以使用下面的代碼來設置pickerview的字體..

- (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 .....]];//set font 
      // Setup label properties - frame, font, colors etc 
      ... 
    } 
    // Fill the label text here 
    ... 
    return tView; 
} 
+1

工作就像我的魅力... upvoted! – NSPratik

17

這裏的雨燕版本上iOS8上進行了測試:

更新在斯威夫特iOS8上,你可以將它添加到您的代表:

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

感謝@Richard Bown

這對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
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 
1

更新了夫特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 
} 
相關問題