自定义国旗键盘
2016-02-20 本文已影响505人
Z了个L
// 模型数据
// LZFlagItem.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface LZFlagItem : NSObject
/** 国家名称*/
@property (nonatomic, strong) NSString *name;
/** 图片*/
@property (nonatomic, strong) UIImage *icon;
// 快速构造方法
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)flagItemDict:(NSDictionary *)dict;
@end
// LZFlagItem.m
#import "LZFlagItem.h"
@implementation LZFlagItem
// 快速构造方法
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
// KVC
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)flagItemDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
- (void)setIcon:(UIImage *)icon
{
// 转换类型
NSString *imageName = (NSString *)icon;
UIImage *iconImage = [UIImage imageNamed:imageName];
_icon = iconImage;
}
@end
LZFlagTextF继承UITextField
// LZFlagTextF.h
#import <UIKit/UIKit.h>
@class LZFlagItem;
@interface LZFlagTextF : UITextField
/** 模型数据*/
@property (nonatomic, strong) LZFlagItem *flagItem;
@end
// LZFlagTextF.m
#import "LZFlagTextF.h"
#import "LZFlagItem.h"
#import "LZFlagView.h"
@interface LZFlagTextF () <UIPickerViewDataSource, UIPickerViewDelegate>
/** 模型数据的数组*/
/** 模型数组*/
@property (nonatomic, strong) NSArray *flagArray;
@end
@implementation LZFlagTextF
#pragma mark - 懒加载数据
- (NSArray *)flagArray
{
if (_flagArray == nil) {
// 路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
// 定义可变数组
NSMutableArray *arrM = [NSMutableArray array];
// 添加模型数据
for (NSDictionary *dict in array) {
LZFlagItem *flagItem = [LZFlagItem flagItemDict:dict];
[arrM addObject:flagItem];
}
_flagArray = arrM;
}
return _flagArray;
}
// 代码创建的时候调用
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
// 从storyboard或者xib中创建的时候调用
- (void)awakeFromNib
{
[self setup];
}
// 设置
- (void)setup
{
UIPickerView *pickV = [[UIPickerView alloc] init];
pickV.dataSource = self;
pickV.delegate = self;
//修改键盘的类型
self.inputView = pickV;
}
#pragma mark - UIPickerViewDataSource 方法
// 多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// 每列多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.flagArray.count;
}
// 每行显示什么内容
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
// 获取模型
LZFlagItem *flagItem = self.flagArray[row];
// 创建LZFlagView对象
LZFlagView *flagV = [LZFlagView flagView];
flagV.flagItem = flagItem;
// 返回一个UIView对象
return flagV;
}
// 行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 80;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// 取出当前选中的行模型
LZFlagItem *item = self.flagArray[row];
self.text = item.name;
}
@end
视图:
// LZFlagView.h
#import <UIKit/UIKit.h>
@class LZFlagItem;
@interface LZFlagView : UIView
+ (instancetype)flagView;
/**
* 模型数据
*/
/** 国旗*/
@property (nonatomic, strong) LZFlagItem *flagItem;
@end
// LZFlagView.m
#import "LZFlagView.h"
#import "LZFlagItem.h"
@interface LZFlagView ()
/** 国籍*/
@property (weak, nonatomic) IBOutlet UILabel *name;
/** 国籍图片*/
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@end
@implementation LZFlagView
/**
* [NSBundle mainBundle] 可以简单理解为就是一个包,找到对应程序的app,右击显示包内容,显示的所有内容都是属于它的,表示的是一个程序包里面的所有东西,在这里它会自动去搜索里面的资源文件,如果在第一级目录里面没有找到,那么它会自动去下一级目录里面找到,一直找到为止
*/
+ (instancetype)flagView
{
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}
- (void)setFlagItem:(LZFlagItem *)flagItem
{
_flagItem = flagItem;
self.name.text = flagItem.name;
self.iconImageView.image = flagItem.icon;
}
@end
LZFlagView.xib图片:
运行效果图片: