2015-12-21 44 views
2

我嘗試使用此代碼行更改標題中的字體。 我該如何做這項工作?UIPickerView不更改字體iOS 9.1

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
NSString *title; 
UIFont *font; 

title = [[countriesToCitiesDic objectForKey:selectedCountryName]objectAtIndex:row]; 
font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; 

UIColor *textColor; 

textColor = [UIColor whiteColor]; 

NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
paragraphStyle.alignment = NSTextAlignmentCenter; 

NSDictionary *attributes = @{NSForegroundColorAttributeName : textColor, 
          NSFontAttributeName : font, 
          NSParagraphStyleAttributeName : paragraphStyle}; 

return [[NSAttributedString alloc] initWithString:title attributes:attributes]; 
} 
+0

對我來說這不是工作) - :這一個對我 –

回答

1

你可以試試這個:

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

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)]; 
label.backgroundColor = [UIColor clearColor]; 
label.textColor = [UIColor whiteColor]; 
label.textAlignment = NSTextAlignmentCenter; 
label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; 
label.text = [[countriesToCitiesDic objectForKey:selectedCountryName]objectAtIndex:row]; 
return label; 
} 
+1

謝謝,這個作品( - : –

1

我想這和它的作品:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{ 

    NSString *title = [[countriesToCitiesDic objectForKey:selectedCountryName]objectAtIndex:row]; 
    UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]; 

    UIColor *textColor = [UIColor whiteColor]; 

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
    paragraphStyle.alignment = NSTextAlignmentCenter; 

    NSDictionary *attributes = @{NSForegroundColorAttributeName : textColor, 
              NSFontAttributeName : font, 
            NSParagraphStyleAttributeName : paragraphStyle}; 

    return [[NSAttributedString alloc] initWithString:title attributes:attributes]; 
} 
+0

沒有愛,特別字體屬性失敗。 –