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/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM