You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
13 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. //
  2. // ViewController.m
  3. // HC
  4. //
  5. // Created by huilinLi on 2025/11/17.
  6. //
  7. #import "ViewController.h"
  8. #import "HomeViewController.h"
  9. @interface ViewController ()
  10. @property (nonatomic, strong) UIImageView *logoImage;
  11. @property (nonatomic, strong) UILabel *headLabel;// 账号密码登录
  12. @property (nonatomic, strong) UILabel *idLabel;// Homily ID
  13. @property (nonatomic, strong) UILabel *pwdLabel;// 密码
  14. @property (nonatomic, strong) UITextField *account;// 输入框
  15. @property (nonatomic, strong) UITextField *pwd;
  16. @property (nonatomic, strong) UIButton *loginButton;// 登录
  17. @property (nonatomic, strong) UIButton *agreementButton;// 协议
  18. @property (nonatomic, strong) UILabel *agreementLabel;// 协议文字
  19. @property (nonatomic, strong) UILabel *loseLabel;// 忘记密码?
  20. @property (nonatomic, strong) UILabel *questionLabel;// 登录遇到问题?
  21. @property (nonatomic, strong) UIButton *emailButton;// 邮箱登录
  22. @end
  23. @implementation ViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. self.view.backgroundColor = [UIColor blackColor];
  27. self.view.userInteractionEnabled = YES;// 开启视图交互
  28. [self setupSubviews];
  29. [self setupConstraints];
  30. NSLog(@"看看导航控制器:%@", self.navigationController ? @"有" : @"哪去了");
  31. }
  32. #pragma mark - 控件
  33. -(void) setupSubviews{
  34. _headLabel = [[UILabel alloc] init];
  35. _headLabel.text = @"账号密码登录";
  36. _headLabel.textColor = [UIColor whiteColor];
  37. _headLabel.font = [UIFont systemFontOfSize:20];
  38. _headLabel.translatesAutoresizingMaskIntoConstraints = NO;
  39. [self.view addSubview:_headLabel];
  40. // 账号
  41. _idLabel = [[UILabel alloc] init];
  42. _idLabel.text = @"Homily ID | ";
  43. _idLabel.textColor = [UIColor whiteColor];
  44. _idLabel.font = [UIFont systemFontOfSize:16];
  45. _idLabel.translatesAutoresizingMaskIntoConstraints = NO;
  46. [self.view addSubview:_idLabel];
  47. // 密码
  48. _pwdLabel = [[UILabel alloc] init];
  49. _pwdLabel.text = @"密码 | ";
  50. _pwdLabel.textColor = [UIColor whiteColor];
  51. _pwdLabel.font = [UIFont systemFontOfSize:16];
  52. _pwdLabel.translatesAutoresizingMaskIntoConstraints = NO;
  53. [self.view addSubview:_pwdLabel];
  54. // 账号输入
  55. _account = [[UITextField alloc] init];
  56. NSDictionary *placeholder = @{
  57. NSForegroundColorAttributeName: [UIColor whiteColor],
  58. NSFontAttributeName: _account.font
  59. };
  60. _account.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请输入帐号" attributes:placeholder];
  61. _account.textColor = [UIColor whiteColor];
  62. _account.layer.borderWidth = 0;
  63. _account.layer.cornerRadius = 5;
  64. _account.delegate = self;
  65. _account.translatesAutoresizingMaskIntoConstraints = NO;
  66. [self.view addSubview:_account];
  67. // 密码输入
  68. _pwd = [[UITextField alloc] init];
  69. NSDictionary *placeholderAttrs1 = @{
  70. NSForegroundColorAttributeName: [UIColor whiteColor],
  71. NSFontAttributeName: _pwd.font
  72. };
  73. _pwd.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 请输入密码" attributes:placeholderAttrs1];
  74. _pwd.secureTextEntry = YES;
  75. _pwd.textColor = [UIColor whiteColor];
  76. _pwd.layer.borderWidth = 0;
  77. _pwd.layer.cornerRadius = 5;
  78. _pwd.delegate = self;
  79. _pwd.translatesAutoresizingMaskIntoConstraints = NO;
  80. [self.view addSubview:_pwd];
  81. // 登录
  82. _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
  83. [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
  84. [_loginButton setTitle:@"这是一个高亮" forState:UIControlStateHighlighted];
  85. [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  86. [_loginButton setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
  87. _loginButton.backgroundColor = [UIColor blueColor];
  88. _loginButton.layer.cornerRadius = 25;// 圆角是高度一半就是圆形按钮
  89. _loginButton.translatesAutoresizingMaskIntoConstraints = NO;
  90. [_loginButton addTarget:self
  91. action:@selector(handleLogin)
  92. forControlEvents:UIControlEventTouchUpInside];
  93. [self.view addSubview:_loginButton];
  94. // 协议勾选
  95. _agreementButton = [UIButton buttonWithType:UIButtonTypeCustom];
  96. _agreementButton.layer.borderColor = [UIColor whiteColor].CGColor;
  97. _agreementButton.layer.borderWidth = 1;
  98. _agreementButton.layer.cornerRadius = 3;
  99. _agreementButton.translatesAutoresizingMaskIntoConstraints = NO;
  100. [_agreementButton addTarget:self
  101. action:@selector(agreementButtonSelect:)
  102. forControlEvents:UIControlEventTouchUpInside];
  103. [self.view addSubview:_agreementButton];
  104. // 协议
  105. _agreementLabel = [[UILabel alloc] init];
  106. _agreementLabel.text = @"我已阅读且同意《注册协议》和《隐私政策》";
  107. _agreementLabel.textColor = [UIColor whiteColor];
  108. _agreementLabel.font = [UIFont systemFontOfSize:12];
  109. _agreementLabel.translatesAutoresizingMaskIntoConstraints = NO;
  110. [self.view addSubview:_agreementLabel];
  111. // 忘记密码?
  112. _loseLabel = [[UILabel alloc] init];
  113. _loseLabel.text = @"忘记密码?";
  114. _loseLabel.textColor = [UIColor blueColor];
  115. _loseLabel.font = [UIFont systemFontOfSize:14];
  116. _loseLabel.translatesAutoresizingMaskIntoConstraints = NO;
  117. [self.view addSubview:_loseLabel];
  118. // 登录遇到问题?
  119. _questionLabel = [[UILabel alloc] init];
  120. _questionLabel.text = @"登录遇到问题?";
  121. _questionLabel.textColor = [UIColor whiteColor];
  122. _questionLabel.font = [UIFont systemFontOfSize:14];
  123. _questionLabel.translatesAutoresizingMaskIntoConstraints = NO;
  124. [self.view addSubview:_questionLabel];
  125. // 邮箱登录
  126. _emailButton = [UIButton buttonWithConfiguration:[self createEmailButtonConfig] primaryAction:nil];
  127. _emailButton.translatesAutoresizingMaskIntoConstraints = NO;// 禁用自动布局!!!!
  128. [self.view addSubview:_emailButton];
  129. // logo的image
  130. _logoImage = [[UIImageView alloc] init];
  131. _logoImage.image = [UIImage imageNamed:@"logo"];
  132. _logoImage.contentMode = UIViewContentModeScaleAspectFit;
  133. _logoImage.translatesAutoresizingMaskIntoConstraints = NO;// 禁用自动布局!!!!
  134. [self.view addSubview:_logoImage];
  135. [self.view bringSubviewToFront:_account];
  136. [self.view bringSubviewToFront:_pwd];
  137. }
  138. #pragma mark - 协议选中
  139. - (void) agreementButtonSelect:(UIButton *) button {
  140. button.selected = !button.selected;
  141. if (button.selected) {
  142. [button setImage:[UIImage systemImageNamed:@"checkmark"] forState:UIControlStateNormal];
  143. button.tintColor = [UIColor whiteColor];
  144. button.backgroundColor = [UIColor blueColor];
  145. } else {
  146. [button setImage:nil forState:UIControlStateNormal];
  147. button.backgroundColor = [UIColor clearColor];
  148. }
  149. }
  150. #pragma mark - 弹窗提示
  151. // 弹窗
  152. - (void) showAlertWithMessage:(NSString *)message {
  153. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
  154. message:message
  155. preferredStyle:UIAlertControllerStyleAlert];
  156. [alert addAction:[UIAlertAction actionWithTitle:@"确定"
  157. style:UIAlertActionStyleDefault
  158. handler:nil]];
  159. [self presentViewController:alert animated:YES completion:nil];
  160. }
  161. - (void) handleLogin {
  162. // NSString *account = _account.text;
  163. // if (account.length == 0 || ![account isEqualToString:@"90047681"]) {
  164. // [self showAlertWithMessage:@"请输入正确的账号"];
  165. // return;
  166. // }
  167. //
  168. // NSString *pwdText = _pwd.text;
  169. // if (pwdText.length == 0 || ![pwdText isEqualToString:@"123456"]) {
  170. // [self showAlertWithMessage:@"请输入正确的密码"];
  171. // return;
  172. // }
  173. //
  174. // if (!_agreementButton.selected) {
  175. // [self showAlertWithMessage:@"请勾选协议"];
  176. // return;
  177. // }
  178. HomeViewController *homeViewController = [[HomeViewController alloc] init];
  179. [self.navigationController pushViewController:homeViewController animated:YES];
  180. }
  181. #pragma mark - 约束
  182. - (void) setupConstraints {
  183. [NSLayoutConstraint activateConstraints:@[
  184. // logo
  185. [_logoImage.bottomAnchor constraintEqualToAnchor:_headLabel.topAnchor constant:-30],
  186. [_logoImage.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40],
  187. [_logoImage.widthAnchor constraintEqualToConstant:50],
  188. [_logoImage.heightAnchor constraintEqualToConstant:50],
  189. // 标题 顶210,左40,宽200,高20
  190. [_headLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:210],
  191. [_headLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40],
  192. [_headLabel.widthAnchor constraintEqualToConstant:200],
  193. [_headLabel.heightAnchor constraintEqualToConstant:20],
  194. // ID310,左40,宽200,高20
  195. [_idLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:310],
  196. [_idLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40],
  197. [_idLabel.widthAnchor constraintEqualToConstant:200],
  198. [_idLabel.heightAnchor constraintEqualToConstant:20],
  199. // 密码 ID下60,左40,宽200,高20
  200. [_pwdLabel.topAnchor constraintEqualToAnchor:_idLabel.topAnchor constant:60],
  201. [_pwdLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40],
  202. [_account.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-40],
  203. [_pwdLabel.heightAnchor constraintEqualToConstant:20],
  204. // 账号输入框:ID上10,左120,宽200,高40
  205. [_account.topAnchor constraintEqualToAnchor:_idLabel.topAnchor constant:-10],
  206. [_account.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:120],
  207. [_pwd.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-40],
  208. [_account.heightAnchor constraintEqualToConstant:40],
  209. // 密码输入框:密码上10,左120,宽200,高40
  210. [_pwd.topAnchor constraintEqualToAnchor:_pwdLabel.topAnchor constant:-10],
  211. [_pwd.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:120],
  212. [_pwd.widthAnchor constraintEqualToConstant:200],
  213. [_pwd.heightAnchor constraintEqualToConstant:40],
  214. // 登录按钮:密码框下80,左右40,高50
  215. [_loginButton.topAnchor constraintEqualToAnchor:_pwd.bottomAnchor constant:60],
  216. [_loginButton.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40],
  217. [_loginButton.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-40],
  218. [_loginButton.heightAnchor constraintEqualToConstant:50],
  219. // 协议按钮:登录下70,左40,宽高20
  220. [_agreementButton.topAnchor constraintEqualToAnchor:_loginButton.bottomAnchor constant:30],
  221. [_agreementButton.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:40],
  222. [_agreementButton.widthAnchor constraintEqualToConstant:20],
  223. [_agreementButton.heightAnchor constraintEqualToConstant:20],
  224. // 协议:同协议按钮顶,左70,右40
  225. [_agreementLabel.topAnchor constraintEqualToAnchor:_agreementButton.topAnchor],
  226. [_agreementLabel.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:70],
  227. [_agreementLabel.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-40],
  228. [_agreementLabel.heightAnchor constraintEqualToConstant:20],
  229. // 忘记密码:协议按钮下65,水平居中,高20
  230. [_loseLabel.topAnchor constraintEqualToAnchor:_agreementButton.bottomAnchor constant:65],
  231. [_loseLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
  232. [_loseLabel.heightAnchor constraintEqualToConstant:20],
  233. // 登录问题:忘记密码下45,水平居中,高20
  234. [_questionLabel.topAnchor constraintEqualToAnchor:_loseLabel.bottomAnchor constant:15],
  235. [_questionLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
  236. [_questionLabel.heightAnchor constraintEqualToConstant:20],
  237. // 邮箱登录
  238. [_emailButton.topAnchor constraintEqualToAnchor:_loseLabel.bottomAnchor constant:55],
  239. [_emailButton.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
  240. [_emailButton.widthAnchor constraintEqualToConstant:100],
  241. [_emailButton.heightAnchor constraintEqualToConstant:100],
  242. ]];
  243. }
  244. // 单写一个
  245. - (UIButtonConfiguration *) createEmailButtonConfig {
  246. UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration];
  247. config.title = @"邮箱登录";
  248. config.image = [UIImage imageNamed:@"email"];
  249. config.imagePlacement = NSDirectionalRectEdgeTop;// 这里也不对????
  250. config.imagePadding = 8;
  251. return config;
  252. }
  253. #pragma mark - UITextFieldDelegate
  254. - (BOOL) textFieldShouldReturn:(UITextField *)textField {
  255. [textField resignFirstResponder];// 点击return键收起键盘
  256. return YES;
  257. }
  258. - (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  259. [self.view endEditing:YES];// 点空白也收起键盘
  260. }
  261. @end