发表于: 2019-10-19 14:16:29
1 908
1、今日完成
基本完成了任务一的界面
效果:
先贴代码吧:
ViewController.m
//
// ViewController.m
// UIImageViewDemo
//
// Created by ptteng on 2019/10/19.
// Copyright © 2019 ptteng. All rights reserved.
//
#import "ViewController.h"
#import "CustomField.h"
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
@interface ViewController (){
CustomField * phoneField;
CustomField * passwdField;
}
@end
@implementation ViewController
//收起键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
phoneField= [[CustomField alloc]initWithFrame:CGRectMake(20, 80, SCREEN_SIZE.width-40, 30)];
NSString *holderText = @"手机";
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
[placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, holderText.length)];
[placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, holderText.length)];phoneField.attributedPlaceholder = placeholder;
[self.view addSubview:phoneField];
passwdField= [[CustomField alloc]initWithFrame:CGRectMake(20, 130, SCREEN_SIZE.width-40, 30)];
NSString *holderText1 = @"密码";
NSMutableAttributedString *placeholder1 = [[NSMutableAttributedString alloc] initWithString:holderText1];
[placeholder1 addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, holderText1.length)];
[placeholder1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, holderText1.length)];passwdField.attributedPlaceholder = placeholder1;
[self.view addSubview:passwdField];
// 登录按钮
UIButton * loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
loginBtn.frame=CGRectMake(20, 200, SCREEN_SIZE.width-40, 40);
loginBtn.backgroundColor=[UIColor redColor];
// 设置按钮边框圆角
loginBtn.layer.cornerRadius=5;
loginBtn.layer.masksToBounds=YES;
[loginBtn setTitle:@"登录"
forState:UIControlStateNormal];
[loginBtn addTarget:self
// 设置触发事件login
action:@selector(login)
forControlEvents:UIControlEventTouchUpInside];
[loginBtn setTintColor:[UIColor whiteColor]];
[self.view addSubview:loginBtn];
// 忘记密码按钮的相关设置
UIButton * forgetButton=[[UIButton alloc] initWithFrame:CGRectMake(20, 8.5*SCREEN_SIZE.height/10, 60, 15)];
[forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
forgetButton.titleLabel.font = [UIFont systemFontOfSize: 12.0];
[self.view addSubview:forgetButton];
forgetButton.titleLabel.adjustsFontSizeToFitWidth = YES;
// 设置触发事件forget
[forgetButton addTarget:self action:@selector(forget) forControlEvents:(UIControlEventTouchUpInside)];
// 注册按钮的相关设置
UIButton * rigisterButton=[[UIButton alloc] initWithFrame:CGRectMake(SCREEN_SIZE.width-50, 8.5*SCREEN_SIZE.height/10, 30, 15)];
[rigisterButton setTitle:@"注册" forState:UIControlStateNormal];
rigisterButton.titleLabel.font = [UIFont systemFontOfSize: 12.0];
[self.view addSubview:rigisterButton];
rigisterButton.titleLabel.adjustsFontSizeToFitWidth = YES;
}
//登录前,先做校验
- (void)login{
if (phoneField.text.length == 0) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"登录名不能为空" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
return;
}
if (passwdField.text.length == 0) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
return;
}
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
}
-(void)forget{
UIAlertController *alertController1 = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"忘记密码了" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController1 addAction:action1];
[self presentViewController:alertController1 animated:YES completion:nil];
}
@end
CustomField.h
#import <UIKit/UIKit.h>
@interface CustomField : UITextField
@end
CustomField.m
#import "CustomField.h"
@implementation CustomField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(self.frame) - 0.5, CGRectGetWidth(self.frame), 0.5));
}
@end
明日计划:
评论