UIPickerView 不改变字体 iOS 9.1
UIPickerView not change font 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];
}
我试过了,效果很好:
- (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];
}
你可以试试这个:
- (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;
}
我尝试用这行代码更改标题中的字体。 我怎样才能完成这项工作?
- (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];
}
我试过了,效果很好:
- (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];
}
你可以试试这个:
- (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;
}