iOS常用技術-登入介面

來源:互聯網
上載者:User

標籤:

//
//  SXTTextField.h
//  04-UITextField練習
//
//  Created by andezhou on 16/1/8.
//  Copyright (c) 2016年 周安德. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SXTTextField : UITextField

// 設定左邊圖片名字
@property (copy, nonatomic) NSString *leftImgName;

/**
 *     @brief  構造方法
 *
 *     @param frame   位置寬高
 *     @param imgName
 *
 */
- (instancetype)initWithFrame:(CGRect)frame
                  leftImgName:(NSString *)leftImgName;

@end
/************************************************/

//
//  SXTTextField.m
//  04-UITextField練習
//
//  Created by andezhou on 16/1/8.
//  Copyright (c) 2016年 周安德. All rights reserved.
//

#import "SXTTextField.h"

@interface SXTTextField ()

@property (strong, nonatomic) UIImageView *leftImageView;

@end

@implementation SXTTextField

#pragma mark -
#pragma mark lifecycle
- (instancetype)initWithFrame:(CGRect)frame
                  leftImgName:(NSString *)leftImgName
{
    self.leftImgName = leftImgName;
    
    return [self initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 設定xx
        self.clearButtonMode = UITextFieldViewModeAlways;
        // 第二次輸入清除內容
        self.clearsOnBeginEditing = YES;
        
        // return模式
        self.returnKeyType = UIReturnKeyDone;
        
        // 設定背景圖片
        self.background = [UIImage imageNamed:@"background"];
        
        // 設定左邊view
        self.leftView = self.leftImageView;
        // 設定左邊view一直存在
        self.leftViewMode = UITextFieldViewModeAlways;
    }
    return self;
}

#pragma mark -
#pragma mark init methods
- (UIImageView *)leftImageView
{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 45, 36)];
        _leftImageView.backgroundColor = [UIColor clearColor];
        _leftImageView.contentMode = UIViewContentModeCenter;
    }
    return _leftImageView;
}

#pragma mark -
#pragma mark set
- (void)setLeftImgName:(NSString *)leftImgName
{
    _leftImgName = leftImgName;
    
    // 把圖片展示在leftView上面
    self.leftImageView.image = [UIImage imageNamed:leftImgName];
}

@end
/***********************************************************/

//
//  UIColor+Extension.h
//  九宮格
//
//  Created by andezhou on 16/1/3.
//  Copyright © 2016年 周安德. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIColor (Extension)

+ (UIColor *)colorWithHexString:(NSString *)color;
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha;


@end
/*************************************************************/

//
//  UIColor+Extension.m
//  九宮格
//
//  Created by andezhou on 16/1/3.
//  Copyright © 2016年 周安德. All rights reserved.
//

#import "UIColor+Extension.h"
#define DEFAULT_VOID_COLOR [UIColor whiteColor]

@implementation UIColor (Extension)

// #000000
+ (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if ([cString length] < 6)
        return DEFAULT_VOID_COLOR;
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return DEFAULT_VOID_COLOR;
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:alpha];
}

+ (UIColor *)colorWithHexString:(NSString *)color
{
    return [UIColor colorWithHexString:color alpha:1.0];
}
@end
/**************************************************/

//
//  ViewController.m
//  04-UITextField練習
//
//  Created by andezhou on 16/1/8.
//  Copyright (c) 2016年 周安德. All rights reserved.
//

#import "ViewController.h"
#import "UIColor+Extension.h"
#import "SXTTextField.h"

static NSUInteger kMargin = 20;
#define kTextFieldWidth [UIScreen mainScreen].bounds.size.width - 2*kMargin

@interface ViewController () <UITextFieldDelegate>

@property (strong, nonatomic) SXTTextField *userNameTextField, *passwordTextField;
@property (strong, nonatomic) UIButton *loginBtn;

@end

@implementation ViewController


- (SXTTextField *)userNameTextField
{
    if (!_userNameTextField) {
        _userNameTextField = [[SXTTextField alloc] initWithFrame:CGRectMake(kMargin, 80, kTextFieldWidth, 44)];
        _userNameTextField.delegate = self;
        _userNameTextField.leftImgName = @"userName";
        _userNameTextField.placeholder = @"請輸入手機號";
        _userNameTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _userNameTextField;
}

- (SXTTextField *)passwordTextField
{
    if (!_passwordTextField) {
        CGRect frame = CGRectMake(kMargin, CGRectGetMaxY(_userNameTextField.frame) + 30, kTextFieldWidth, 44);
        _passwordTextField = [[SXTTextField alloc] initWithFrame:frame leftImgName:@"password"];
        _passwordTextField.delegate = self;
        // Cipher 模式
        _passwordTextField.secureTextEntry = YES;
        _passwordTextField.placeholder = @"請輸入密碼";
    }
    return _passwordTextField;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        CGFloat pointY = CGRectGetMaxY(_passwordTextField.frame) + 50;
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.frame = CGRectMake(kMargin, pointY, kTextFieldWidth, 44);
        [_loginBtn setTitle:@"登入" forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [_loginBtn setBackgroundImage:[UIImage imageNamed:@"beijing"] forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.layer.cornerRadius = 4.0f;
        _loginBtn.layer.masksToBounds = YES;
        _loginBtn.enabled = NO;
    }
    return _loginBtn;
}

#pragma mark -
#pragma mark loginAction
- (void)loginAction:(UIButton *)btn
{
    NSLog(@"userName:%@ password:%@", _userNameTextField.text, _passwordTextField.text);
}

#pragma mark -
#pragma mark UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([_passwordTextField.text isEqualToString:@""] || [_userNameTextField.text isEqualToString:@""]) {
        _loginBtn.enabled = NO;
    }else{
        _loginBtn.enabled = YES;
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    return YES;
}

#pragma mark -
#pragma mark lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f7f8f3"];
    [self.view addSubview:self.userNameTextField];
    [self.view addSubview:self.passwordTextField];
    [self.view addSubview:self.loginBtn];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
/***************************************************************/

iOS常用技術-登入介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.