1: Use of uipickerView 2: return a rich text display 3: set the cell line height 4: Set return a custom view 5: Set the color of the top and bottom lines of the PickerView or hide them
Instantiation of picker View
self.pickView = [[UIPickerView alloc] init];
self.pickView.dataSource = self;
self.pickView.delegate = self;
self.pickView.userInteractionEnabled = NO;
[self.backGroundView addSubview:self.pickView];
self.pickView.showsSelectionIndicator = NO;
Copy the code
Set up the self. PickView. UserInteractionEnabled = NO; Is to make the user can not click on the rollover effect
Use several required usage functions
#pragma mark - uipickview DELEGATE- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.dataArry.count;
}
Copy the code
2.1 Returns a rich text editing effect
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString * reStr = self.dataArry[row];
// Create a rich text
NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:reStr];
// Modify the styles of different text in rich text
[attriStr addAttribute:NSForegroundColorAttributeName value:YSColor(88.164.240) range:NSMakeRange(0, reStr.length)];
//[attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, reStr.length)];
((UIView*) [self.pickView.subviews objectAtIndex:1]).backgroundColor = [YSColor(255.255.255) colorWithAlphaComponent:0.5];
return attriStr;
}
Copy the code
2.2 Setting the Row height of the Cell
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 20;
}
Copy the code
2.3 Set the Cell to a Custom View
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
CGFloat width = 125.0f;
CGFloat height = 20.0f;
UIView * myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, width, height)];
myView.backgroundColor = [UIColor clearColor];
UILabel * complateLabel = [[UILabel alloc] init];
complateLabel.center = myView.center;
complateLabel.bounds = CGRectMake(0.0, width, height);
complateLabel.textColor = YSColor(88.164.240);
complateLabel.textAlignment = NSTextAlignmentCenter;
complateLabel.font = FONTWITHSIZE_LIGHT(14);
complateLabel.text = self.dataArry[row];
[myView addSubview:complateLabel];
((UIView*) [self.pickView.subviews objectAtIndex:1]).backgroundColor = [YSColor(255.255.255) colorWithAlphaComponent:0.5];
if (IS_IOS7) {
((UIView*) [self.pickView.subviews objectAtIndex:2]).backgroundColor = [YSColor(255.255.255) colorWithAlphaComponent:0.5];
}
return myView;
}
Copy the code
Here,
((UIView*) [self.pickView.subviews objectAtIndex:1]).backgroundColor = [YSColor(255.255.255) colorWithAlphaComponent:0.5];
((UIView*) [self.pickView.subviews objectAtIndex:2]).backgroundColor = [YSColor(255.255.255) colorWithAlphaComponent:0.5];
Copy the code
Set the color of the top and bottom lines of the pickView, or hide it