iOS - 實現(xiàn)自定義UIPickerView選中的字體顏色儒旬、字號袱箱、字體

在使用UIPickerView顯示時間,城市等的選擇時义矛,系統(tǒng)定義的樣式總是與自己的頁面不搭配需要進行精加工发笔,就給大家介紹一下怎樣自定義UIPickerView選中的字體顏色、字號凉翻、字體等屬性

自定義UIPickerView選中的字體顏色了讨、字號、字體等屬性時需要定義

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view __TVOS_PROHIBITED;方法在此方法中 根據(jù)需要 重寫下面的兩個方法

//重寫 - (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{
    
    //設(shè)置分割線的顏色
    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;
}

效果如下:


自定義選中顏色效果

同時可以根據(jù)上面的思路自定義你需要的樣式

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末制轰,一起剝皮案震驚了整個濱河市前计,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌垃杖,老刑警劉巖男杈,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異调俘,居然都是意外死亡伶棒,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進店門彩库,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肤无,“玉大人,你說我怎么就攤上這事骇钦⊥鸾ィ” “怎么了?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵眯搭,是天一觀的道長窥翩。 經(jīng)常有香客問我,道長鳞仙,這世上最難降的妖魔是什么寇蚊? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮繁扎,結(jié)果婚禮上幔荒,老公的妹妹穿的比我還像新娘糊闽。我一直安慰自己,他們只是感情好爹梁,可當(dāng)我...
    茶點故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布右犹。 她就那樣靜靜地躺著,像睡著了一般姚垃。 火紅的嫁衣襯著肌膚如雪念链。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天积糯,我揣著相機與錄音掂墓,去河邊找鬼。 笑死看成,一個胖子當(dāng)著我的面吹牛君编,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播川慌,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼吃嘿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了梦重?” 一聲冷哼從身側(cè)響起兑燥,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎琴拧,沒想到半個月后降瞳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡蚓胸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年挣饥,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赢织。...
    茶點故事閱讀 40,742評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡亮靴,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出于置,到底是詐尸還是另有隱情,我是刑警寧澤贞岭,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布八毯,位于F島的核電站,受9級特大地震影響瞄桨,放射性物質(zhì)發(fā)生泄漏话速。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一芯侥、第九天 我趴在偏房一處隱蔽的房頂上張望泊交。 院中可真熱鬧乳讥,春花似錦、人聲如沸廓俭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽研乒。三九已至汹忠,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間雹熬,已是汗流浹背宽菜。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留竿报,地道東北人铅乡。 一個月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像烈菌,于是被迫代替她去往敵國和親阵幸。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內(nèi)容