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
原创粉丝点击