iOS - UIPickerView 选中背景颜色设置

2022-01-07  本文已影响0人  HanZhiZzzzz
/// 代理方法
<UIPickerViewDelegate, UIPickerViewDataSource>
 
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
 
_pickerView.delegate = self;
_pickerView.dataSource = self;

#pragma mark-pickerView的代理方法
/// 设置行高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 50;
}
 
 //设置有多少列
 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
 {
     return 1;
 }
 
 //设置每列多少行
 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
 {
     return self.texts.count;
 
 }
 
 //设置每行每列显示的内容
 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
 {
 
     return KNSStringFormat(@"%@K",_texts[row]);
 
 }
 
 //通过代理方法didSelectRows获取数据
 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
 {
     
     UILabel *pickerLabel =  (UILabel *)[pickerView viewForRow:row forComponent:component];
 
     pickerLabel.attributedText
     = [self pickerView:pickerView attributedTitleForRow:row forComponent:component];
     
     NSLog(@"列:%ld,行:%ld",component,row);
     
     _selectText = KNSStringFormat(@"%@",_texts[row]);
     
 }
 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    [self changeSpearatorLineColor];
    
    /// 未选中颜色
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        pickerLabel.font = [UIFont systemFontOfSize:17];//[UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
        pickerLabel.textColor = [UIColor colorWithRed:12.f/255.f green:14.f/255.f blue:14.f/255.f alpha:1];
    }
    
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    
    return pickerLabel;
}
 
#pragma mark - 改变分割线的颜色 和 选中的背景色
- (void)changeSpearatorLineColor {
    
    for(UIView *speartorView in _pickerView.subviews) {
 
        if (speartorView.frame.size.height < 80) {//找出当前的 View
            /// 背景
//            speartorView.backgroundColor = RGBColor(239, 239, 239);
            // 添加分割线 (判断只添加一次  滑动不断刷新)
            if (speartorView.subviews.count ==0){
                UIView *line = [self lineView];
                line.frame = CGRectMake(0, 0, speartorView.mj_w, 0.5);
                [speartorView addSubview:line];
                
                UIView *line2 = [self lineView];
                line2.frame = CGRectMake(0, speartorView.mj_h-1, speartorView.mj_w, 0.5);
                [speartorView addSubview:line2];
            }
 
            speartorView.backgroundColor = [UIColor clearColor];
        }else{
            speartorView.backgroundColor = [UIColor clearColor];
        }
    }
}
/// 分割线
- (UIView *)lineView {
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0.5)];
    line.backgroundColor = RGBColor(239, 239, 239);
    return line;
}
 
 
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSString *titleString = KNSStringFormat(@"%@K",_texts[row]);
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:titleString];
    NSRange range = [titleString rangeOfString:titleString];
    [attributedString addAttribute:NSForegroundColorAttributeName value:RGBColor(14, 14, 14) range:range];
    
    return attributedString;

}

摘自:https://blog.csdn.net/qq_29305413/article/details/109044308

上一篇 下一篇

猜你喜欢

热点阅读