zoukankan      html  css  js  c++  java
  • UITextField使用的一些细节

    UITextField使用的一些细节

    这篇博文是我自己使用UITextField的一些总结,并没有太多营养,并会持续更新。

    2014.9.15

    --------------------------------------------------------------------------------------------------------------------------------------

    源码:

    //
    //  RootViewController.m
    //  UITextField
    //
    //  Created by YouXianMing on 14-9-15.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "RootViewController.h"
    
    @interface RootViewController ()<UITextFieldDelegate>
    
    @property (nonatomic, strong) UITextField *textField;
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        _textField                      = [[UITextField alloc] initWithFrame:CGRectMake(10,  80, 300, 40)];
        _textField.layer.borderWidth    = 1.f;
        _textField.layer.borderColor    = [UIColor redColor].CGColor;
        _textField.backgroundColor      = [UIColor whiteColor];
        _textField.textAlignment        = kCTTextAlignmentLeft;    // 左对齐
        _textField.delegate             = self ;
        _textField.keyboardType         = UIKeyboardTypeNumberPad; // 数字键盘
        _textField.placeholder          = @"请输入4位验证码";
        _textField.clearsOnBeginEditing = YES;
        
        
        
        // 空出左侧边缘空隙(通过添加一个view来实现)
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10,
                                                                CGRectGetHeight(_textField.bounds))];
        _textField.leftView     = view;
        _textField.leftViewMode = UITextFieldViewModeAlways;
        
        [self.view addSubview:_textField];
        
        // 添加手势
        [self addGesture];
    }
    
    - (void)addGesture
    {
        // 手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        [self.view addGestureRecognizer:tap];
    }
    
    // 限制输入长度
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if (range.location >= 4)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    
    - (void)tapGesture:(UITapGestureRecognizer *)tap
    {
        // 取消第一响应状态
        [_textField resignFirstResponder];
    }
    
    @end

    效果:

    左侧空出空隙

    限制输入长度

    --------------------------------------------------------------------------------------------------------------------------------------

  • 相关阅读:
    深入理解并发编程 -- 多线程(一)
    使用Mybatis实现动态SQL(二)
    Java设计模式
    使用Mybatis实现动态SQL(一)
    Java
    Java安全(权限)框架
    List-LinkedList、set集合基础增强底层源码分析
    hadoop3.1.0 window win7 基础环境搭建
    springmvc传递有特殊字符的路径参数
    jhipster(springboot+datatable+jpa)后台分页,总结
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3973845.html
Copyright © 2011-2022 走看看