iOS兑换码框的实现(OC)
来源:互联网 发布:手机电视同步软件 编辑:程序博客网 时间:2024/04/20 00:13
主要实现兑换框功能,支持回删、自动跳转输入框等功能,界面效果在文章的最后面
#import "ViewController.h"@interface ViewController ()<UITextFieldDelegate>{ UITextField *_firstTF; UITextField *_secondTF; UITextField *_thirdTF; UITextField *_lastTF;}@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; [self setTFView];}//创建界面控件- (void)setTFView{ _firstTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 60, 30)]; _firstTF.delegate = self; _firstTF.font = [UIFont systemFontOfSize:13.0f]; _firstTF.borderStyle = UITextBorderStyleRoundedRect; [_firstTF addTarget:self action:@selector(TextFieldEnditing:) forControlEvents:UIControlEventEditingChanged]; [self.view addSubview:_firstTF]; _secondTF = [[UITextField alloc] initWithFrame:CGRectMake(90, 100, 60, 30)]; _secondTF.delegate = self; _secondTF.font = [UIFont systemFontOfSize:13.0f]; [_secondTF addTarget:self action:@selector(TextFieldEnditing:) forControlEvents:UIControlEventEditingChanged]; _secondTF.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:_secondTF]; _thirdTF = [[UITextField alloc] initWithFrame:CGRectMake(170, 100, 60, 30)]; _thirdTF.delegate = self; _thirdTF.font = [UIFont systemFontOfSize:13.0f]; [_thirdTF addTarget:self action:@selector(TextFieldEnditing:) forControlEvents:UIControlEventEditingChanged]; _thirdTF.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:_thirdTF]; _lastTF = [[UITextField alloc] initWithFrame:CGRectMake(250, 100, 60, 30)]; _lastTF.delegate = self; _lastTF.font = [UIFont systemFontOfSize:13.0f]; [_lastTF addTarget:self action:@selector(TextFieldEnditing:) forControlEvents:UIControlEventEditingChanged]; _lastTF.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:_lastTF]; //添加确定按钮 float weight = [UIScreen mainScreen].bounds.size.width; UIButton *certainBtn = [UIButton buttonWithType:UIButtonTypeCustom]; certainBtn.frame = CGRectMake(30, 160, weight - 60, 40); [certainBtn setTitle:@"确定" forState:UIControlStateNormal]; certainBtn.backgroundColor = [UIColor orangeColor]; certainBtn.layer.cornerRadius = 3.0f; certainBtn.layer.masksToBounds = YES; [certainBtn addTarget:self action:@selector(certainButtonClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:certainBtn]; //让输入框中的内容变成大写 _firstTF.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; _secondTF.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; _thirdTF.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; _lastTF.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 115, 10, 3)]; firstLabel.text = @"-"; [self.view addSubview:firstLabel]; UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(155, 115, 10, 3)]; secondLabel.text = @"-"; [self.view addSubview:secondLabel]; UILabel *thirdLabel = [[UILabel alloc] initWithFrame:CGRectMake(235, 115, 10, 3)]; thirdLabel.text = @"-"; [self.view addSubview:thirdLabel];}#pragma mark - UITextFieldDelegate//输入框开始编辑时会调用此方法- (void)textFieldDidBeginEditing:(UITextField *)textField{ //给输入框添加一个空格,这样的话输入框中没有内容也也能回删 if ([textField.text isEqualToString:@""]) { textField.text = @" "; }}//当输入框中的内容发生改变时会调用此方法- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ /** *如果输入框是第一个输入框,阻止删除第一个空格 *防止第一个空格输入5个字符的情况 */ if (textField == _firstTF) { if (range.location == 0) { return NO; }else { return YES; } } NSInteger strLength = textField.text.length - range.length + string.length; return (strLength <= 5);}//输入框内容改变时调用的方法- (void)TextFieldEnditing:(UITextField *)textField{ NSInteger strLength = textField.text.length; if (textField == _firstTF && strLength >= 5) { [_secondTF becomeFirstResponder]; } if (textField == _secondTF && strLength >= 5) { [_thirdTF becomeFirstResponder]; } if (textField == _thirdTF && strLength >= 5) { [_lastTF becomeFirstResponder]; } if (textField == _lastTF && strLength >= 5) { [_lastTF resignFirstResponder]; } if (textField == _secondTF && strLength == 0) { NSString *tempStr = [[NSString alloc] initWithString:_firstTF.text]; if (![tempStr isEqualToString:@""]) { _firstTF.text = [tempStr substringToIndex:tempStr.length - 1]; } [_firstTF becomeFirstResponder]; }else if (textField == _thirdTF && strLength == 0) { NSString *tempStr = [[NSString alloc] initWithString:_secondTF.text]; if (![tempStr isEqualToString:@""]) { _secondTF.text = [tempStr substringToIndex:tempStr.length - 1]; } [_secondTF becomeFirstResponder]; }else if (textField == _lastTF && strLength == 0) { NSString *tempStr = [[NSString alloc] initWithString:_thirdTF.text]; if (![tempStr isEqualToString:@""]) { _thirdTF.text = [tempStr substringToIndex:tempStr.length - 1]; } [_thirdTF becomeFirstResponder]; }}#pragma mark - UIButtonClick- (void)certainButtonClick{ //删除输入框中字符串中的空格后就是需要使用的兑换码 NSString *code = [NSString stringWithFormat:@"%@-%@-%@-%@",_firstTF.text,_secondTF.text,_thirdTF.text,_lastTF.text]; code = [code stringByReplacingOccurrencesOfString:@" " withString:@""]; code = [code uppercaseString]; NSString *upCodeStr = [code uppercaseString]; NSLog(@"upCodeStr = %@", upCodeStr);}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
效果图:
0 0
- iOS兑换码框的实现(OC)
- 【iOS】Web Color 的 OC 实现
- 【iOS/OC】互斥button的实现
- 【iOS】简述下OC block的实现
- 对于卡券类兑换的,随机生成兑换码
- ios oc实现排序算法
- iOS之OC随笔-九宫格的算法简单实现
- iOS开发(OC)——分段按钮的实现
- iOS中HTML与OC的交互实现
- iOS敏感词过滤,DFA算法的OC实现
- iOS OC与JS的交互(JavaScriptCore实现)
- iOS OC与JS的交互(WebViewJavascriptBridge实现)
- iOS下OC与JS的交互(WKWebview-MessageHandler实现)
- IOS中 使用JavaScriptCore 实现OC与JS的交互
- IOS中 使用JavaScriptCore 实现OC与JS的交互
- 实现iOS设备的唯一标识(OC版)
- 简单的汇率兑换
- 人民币的兑换
- Linux下Tun/Tap设备通信原理
- 【编码】Python2.x的中文显示方法
- BFS+队列优先
- 如何获取非本activity绑定的布局控件
- sicily 1004.I Conduit!
- iOS兑换码框的实现(OC)
- 内部类解析
- git --git命令操作
- ZOJ 3547 The Boss on Mars(容斥)
- MAPJOIN来解决实际的问题
- 手把手教你打造一个Material Design风格的App(二)
- 简介隔离层级
- 机器学习
- C++ 虚函数表解析(皓哥)