iOS UIPickerView使用技巧
修改字型大小及顏色
- 方法一
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSDictionary* titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:12.f/255.f green:14.f/255.f blue:14.f/255.f alpha:1], NSForegroundColorAttributeName,
[UIFont systemFontOfSize:16 weight:UIFontWeightSemibold], NSFontAttributeName,
nil];
NSString *str = self.dataAry[row];
NSAttributedString *restr = [[NSAttributedString alloc] initWithString:str attributes:titleTextAttributes];
return restr;
}
注意:不能和titleForRow方法同時使用
- 方法二
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* pickerLabel = (UILabel*)view;
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];;
pickerLabel.adjustsFontSizeToFitWidth = YES;
pickerLabel.textAlignment = NSTextAlignmentCenter;
pickerLabel.font = [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;
}
修改分割線顏色
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* pickerLabel = (UILabel*)view;
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];;
pickerLabel.adjustsFontSizeToFitWidth = YES;
pickerLabel.textAlignment = NSTextAlignmentCenter;
pickerLabel.font = [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];
[self changeSpearatorLineColor];
return pickerLabel;
}
#pragma mark - 改變分割線的顏色
- (void)changeSpearatorLineColor {
for(UIView *speartorView in _dataPickerView.subviews) {
if (speartorView.frame.size.height < 1) {
speartorView.backgroundColor = [UIColor redColor];
}
}
}
注意:這個方法只有放到下面的方法才有效果,獲取pickerView:viewForRow:forComponent:reusingView:中定義的View,當pickerView:viewForRow:forComponent:reusingView:未實現或者行或分組不可用時返回nil
參考
相關文章
- iOS AutoLayout使用技巧iOS
- 01-UIPickerViewUIView
- UIDatePicker 和 UIPickerViewUIView
- 去掉UIPickerView的彎曲弧度UIView
- iOS開發之NavBar和TarBar使用技巧iOS
- 【ios】關於[self.tableView reloadData]使用技巧iOSView
- iOS 動畫技巧 (一)iOS動畫
- iOS 開發小技巧iOS
- iOS開發技巧:應用巨集定義使用字型iOS
- 用UIPickerView簡單定製一個UIDatePickerUIView
- 蘋果手機使用技巧:iPhone怎麼升級iOS8.2?蘋果iPhoneiOS
- 最佳化iOS小技巧iOS
- iOS開發中的技巧iOS
- iOS開發奇淫技巧iOS
- iOS 核心動畫高階技巧 - 1iOS動畫
- iOS開發小技巧合集iOS
- iOS 基礎開發技巧 (一)iOS
- iOS開發—技巧總結(一)iOS
- chrome使用技巧Chrome
- Git 使用技巧Git
- uedit使用技巧
- HBuilder使用技巧UI
- Excel 使用技巧Excel
- ultraedit使用技巧
- CSS使用技巧CSS
- secureCRT使用技巧Securecrt
- webstorm使用技巧WebORM
- Excel使用技巧Excel
- rose使用技巧ROS
- PPT使用技巧
- clion使用技巧
- iOS高階開發者面試必過技巧iOS面試
- iOS這些小技巧你都知道嗎iOS
- iOS 知識-常用小技巧彙總iOS
- iOS Autolayout 介紹 2 Interface Builder 技巧iOSUI
- 【轉】iOS保持介面流暢的技巧iOS
- iOS開發程式猿必備技巧iOS
- Python使用技巧Python