发表于: 2019-10-19 23:11:11
3 898
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
1)输入框的样式中没有下划线,需要自定义。
创建一个CustomField接口并继承UITextField,
CustomField.h
#import <UIKit/UIKit.h>
@interface CustomField : UITextField
@end
在实现类中重写父类的drawRect:(CGRect)rect方法:
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
遇到的问题:
对objective-c语言还是不太熟悉,百度东凑西拼勉强把任务一登录页面实现。
布局这块还是有问题,使用的是绝对位置,机型适配效果不太好。
代码太乱,XCode没有代码格式化的功能吗?
明日计划:
整理优化一下代码;
布局这块再搞一下;
开始任务二。
收获:
对ios开发有了大概的那么一点了解。
评论