UIPickerView⾃定义选中的字体颜⾊、字号、字体
UIPickerView⾃定义选中的字体颜⾊、字号、字体
在使⽤UIPickerView显⽰时间,城市等的选择时,系统定义的样式总是与⾃⼰的页⾯不搭配需要进⾏精加⼯,就给⼤家介绍⼀下怎样⾃定义UIPickerView选中的字体颜⾊、字
号、字体等属性
⾃定义UIPickerView选中的字体颜⾊、字号、字体等属性时需要定义
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
__TVOS_PROHIBITED;⽅法在此⽅法中根据需要重写下⾯的两个⽅法
//重写 - (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED; ⽅法加载title
/*重写- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED; ⽅法加载 attributedText*/
具体看实例:(实例以加载三级城市选择为例)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
//设置分割线的颜⾊
for(UIView *singleLine in pickerView.subviews)
{
if (singleLine.frame.size.height < 1)
{
singleLine.backgroundColor = [UIColor grayColor];
}
}
/*重新定义row 的UILabel*/
UILabel *pickerLabel = (UILabel*)view;
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
//adjustsFontSizeToFitWidth property to YES
[pickerLabel setTextColor:[UIColor darkGrayColor]];
pickerLabel.adjustsFontSizeToFitWidth = YES;
[pickerLabel setTextAlignment:NSTextAlignmentCenter];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont systemFontOfSize:16.0f]];
/
/ [pickerLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
}
// Fill the label text here
//重写 - (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED; ⽅法加载title
// = [self pickerView:pickerView titleForRow:row forComponent:component];
if(component == 0){
if(row == _firstIndex){
/*选中后的row的字体颜⾊*/
/*重写- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED; ⽅法加载 attributedText            pickerLabel.attributedText
= [self pickerView:pickerView attributedTitleForRow:_firstIndex forComponent:component];
}else{
< = [self pickerView:pickerView titleForRow:row forComponent:component];
}
}else if (component == 1){
if(row == _secondIndex){
pickerLabel.attributedText
= [self pickerView:pickerView attributedTitleForRow:_secondIndex forComponent:component];
}else{
< = [self pickerView:pickerView titleForRow:row forComponent:component];
}
}else if (component == 2){
if(row == _thirdIndex){
pickerLabel.attributedText
= [self pickerView:pickerView attributedTitleForRow:_thirdIndex forComponent:component];
}else{
< = [self pickerView:pickerView titleForRow:row forComponent:component];
}
}
return pickerLabel;
}
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *titleString;
if (component == 0) {
CityModel *md = _dataSource[row];
titleString = md.region_name;
}
else if (component == 1){
CityModel *md = _dataSource[_firstIndex];
_shiArr = md.regionlist;
CityModel *shi = _shiArr[row];
titleString = ion_name;
}
else if (component == 2){
CityModel *md = _shiArr[_secondIndex];
_quArr = md.regionlist;
CityModel *qu = _quArr[row];
titleString = qu.region_name;
}
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:titleString];
NSRange range = [titleString rangeOfString:titleString];
view ui框架
[attributedString addAttribute:NSForegroundColorAttributeName value:navBGColor range:range];
return attributedString;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0) {
CityModel *md = _dataSource[row];
ion_name;
}
else if (component == 1){
CityModel *md = _dataSource[_firstIndex];
_shiArr = md.regionlist;
CityModel *shi = _shiArr[row];
ion_name;
}
else if (component == 2){
CityModel *md = _shiArr[_secondIndex];
_quArr = md.regionlist;
CityModel *qu = _quArr[row];
ion_name;
}
return nil;
}
效果如下图:
同时可以根据以上的思路进⾏⾃定义你需要的样式,谁有更好的⽅法我们可以共同学习哦!(关注博客:)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。