[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

相关文章

kafka自定义分区器使用详解

《kafka自定义分区器使用详解》本文介绍了如何根据企业需求自定义Kafka分区器,只需实现Partitioner接口并重写partition()方法,示例中,包含cuihaida的数据发送到0号分区... 目录kafka自定义分区器假设现在有一个需求使用分区器的方法总结kafka自定义分区器根据企业需求

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

SpringBoot AspectJ切面配合自定义注解实现权限校验的示例详解

《SpringBootAspectJ切面配合自定义注解实现权限校验的示例详解》本文章介绍了如何通过创建自定义的权限校验注解,配合AspectJ切面拦截注解实现权限校验,本文结合实例代码给大家介绍的非... 目录1. 创建权限校验注解2. 创建ASPectJ切面拦截注解校验权限3. 用法示例A. 参考文章本文

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python利用GeoPandas打造一个交互式中国地图选择器

《Python利用GeoPandas打造一个交互式中国地图选择器》在数据分析和可视化领域,地图是展示地理信息的强大工具,被将使用Python、wxPython和GeoPandas构建的交互式中国地图行... 目录技术栈概览代码结构分析1. __init__ 方法:初始化与状态管理2. init_ui 方法:

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (