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
    
    //pickerLabel.text = [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{
            
            pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
        }
        
    }else if (component == 1){
        
        if(row == _secondIndex){
            
            pickerLabel.attributedText
            = [self pickerView:pickerView attributedTitleForRow:_secondIndex forComponent:component];
        }else{
            
            pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
        }
        
    }else if (component == 2){
        
        if(row == _thirdIndex){
            
            pickerLabel.attributedText
            = [self pickerView:pickerView attributedTitleForRow:_thirdIndex forComponent:component];
            
        }else{
            
            pickerLabel.text = [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 = shi.region_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];
    [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];
        return md.region_name;
    }
    else if (component == 1){
        
        CityModel *md = _dataSource[_firstIndex];
        _shiArr = md.regionlist;
        CityModel *shi = _shiArr[row];
        return shi.region_name;
    }
    else if (component == 2){
        
        CityModel *md = _shiArr[_secondIndex];
        _quArr = md.regionlist;
        CityModel *qu = _quArr[row];
        return qu.region_name;
        
    }
    
    return nil;
}
复制代码

 

效果如下图:

 同时可以根据以上的思路进行自定义你需要的样式,谁有更好的方法我们可以共同学习哦!(关注博客:http://www.cnblogs.com/Rong-Shengcom/

 

posted @   #零下一度&  阅读(20108)  评论(1编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示