[IOS]UIPickerView(自定义选择器)

2024-08-20 23:58

本文主要是介绍[IOS]UIPickerView(自定义选择器),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

[IOS]UIPickerView(自定义选择器)

Demo:http://download.csdn.net/detail/u012881779/8645725

#import <UIKit/UIKit.h>@interface WAFontStyle : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) NSMutableArray *wFontColor;//字体颜色
@property (strong, nonatomic) NSMutableArray *wFont;//字体类型
@property (strong, nonatomic) NSMutableArray *wFontSize;//字体大小
@property (nonatomic) float   wChioceSize;//选择字体大小
@property (weak, nonatomic) IBOutlet UIPickerView *wFontPickerView;
@property (weak, nonatomic) IBOutlet UILabel *wFontLab;
@property (weak, nonatomic) IBOutlet UIView *wFontView;@end@implementation WAFontStyle
@synthesize wFontColor = _wFontColor;
@synthesize wFont      = _wFont;
@synthesize wFontSize  = _wFontSize;- (void)viewDidLoad {[super viewDidLoad];[_wFontView.layer setCornerRadius:20];/**数据准备*///字体类型_wFont = (NSMutableArray *)[UIFont familyNames];//字体颜色_wFontColor = [[NSMutableArray alloc] initWithObjects:[UIColor greenColor],[UIColor blackColor],[UIColor grayColor],[UIColor redColor],[UIColor blueColor],[UIColor whiteColor],[UIColor yellowColor],[UIColor brownColor],[UIColor orangeColor],[UIColor magentaColor],[UIColor purpleColor],nil];//字体大小_wFontSize = [[NSMutableArray alloc] initWithObjects:@"12",@"14",@"16",@"18",@"20",@"22",@"24",@"26",@"28",@"30",nil];//初始默认选择for(int i = 0;i < 3;i ++){int row = 0;if(i == 0)row = (int)[_wFont count]/2;else if(i == 1)row = (int)[_wFontSize count]/2;else if(i == 2)row = (int)[_wFontColor count]/2;[_wFontPickerView selectRow:row inComponent:i animated:YES];}}//选择取消
- (IBAction)mCancelAction:(id)sender {[self.view removeFromSuperview];
}//选择确定
- (IBAction)mSelectorAction:(id)sender {[self mCancelAction:nil];
}#pragma mark UIPickerViewDataSource
//几列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{return 3;
}//几行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{if(component == 0)return [_wFont count];else if(component == 1)return [_wFontSize count];else if(component == 2)return [_wFontColor count];return -1;
}#pragma mark UIPickerViewDelegate
//component宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{if(component == 0)return 150.0f;else if(component == 1)return 50.0f;else if(component == 2)return 50.0f;return 0.0f;
}
//row高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{return 50.0f;
}//专门为定制UIPickerView用的一个函数,返回component列row行所在的定制的View,不自定义的话会有一个系统默认的格式
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{//得到Component对应的宽和高CGFloat width = [self pickerView:pickerView widthForComponent:component];CGFloat height = [self pickerView:pickerView rowHeightForComponent:component];//返回UIViewUIView *returnView = [[UIView alloc] init];[returnView setFrame:CGRectMake(0, 0, width, height-10)];//添加UILabel到UIView上,传递数据UILabel *label = [[UILabel alloc] init];label.frame = returnView.frame;[label setTextColor:[UIColor blackColor] ];label.tag = 1000;[label setFont:[UIFont systemFontOfSize:20]];[returnView addSubview:label];//对Label附加数据if(component == 0)label.text = [_wFont objectAtIndex:row];//字体else if(component == 1)label.text = [_wFontSize objectAtIndex:row];//大小else if(component == 2)label.backgroundColor = [_wFontColor objectAtIndex:row];//颜色return returnView;
}
//关联UILabel 和 UIPickerView
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{//取得选择的RowNSInteger rowZero,rowOne,rowTwo;rowZero = [pickerView selectedRowInComponent:0];rowOne = [pickerView selectedRowInComponent:1];rowTwo = [pickerView selectedRowInComponent:2];//从选择的Row取得ViewUIView *viewZero,*viewOne,*viewTwo;viewZero = [pickerView viewForRow:rowZero forComponent:0];viewOne = [pickerView viewForRow:rowOne forComponent:1];viewTwo = [pickerView viewForRow:rowTwo forComponent:2];//从取得的View取得上面UILabelUILabel *labZero,*labOne,*labTwo;labZero = (UILabel *)[viewZero viewWithTag:1000];labOne = (UILabel *)[viewOne viewWithTag:1000];labTwo = (UILabel *)[viewTwo viewWithTag:1000];//将从三列分别取得的,字体,大小,颜色,传递给在界面上显示的UILabel[_wFontLab setFont:[UIFont fontWithName:labZero.text size:[labOne.text  floatValue]]];_wChioceSize = [labOne.text  floatValue];[_wFontLab setTextColor:labTwo.backgroundColor];}@end



示图:



这篇关于[IOS]UIPickerView(自定义选择器)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091549

相关文章

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现