VLCNetworkLoginViewFieldCell.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2016 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Vincent L. Cone <vincent.l.cone # tuta.io>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCNetworkLoginViewFieldCell.h"
  12. NSString * const kVLCNetworkLoginViewFieldCellIdentifier = @"VLCNetworkLoginViewFieldCellIdentifier";
  13. @interface VLCNetworkLoginViewFieldCell () <UITextFieldDelegate>
  14. @end
  15. @implementation VLCNetworkLoginViewFieldCell
  16. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  17. {
  18. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  19. if (self) {
  20. self.backgroundColor = [UIColor blackColor];
  21. [self setupSubviews];
  22. }
  23. return self;
  24. }
  25. - (void)setupSubviews
  26. {
  27. UIView *darkView = [[UIView alloc] init];
  28. darkView.translatesAutoresizingMaskIntoConstraints = NO;
  29. [self addSubview:darkView];
  30. darkView.backgroundColor = [UIColor colorWithRed:57.0/256.0 green:57.0/256.0 blue:57.0/256.0 alpha:1.0];
  31. self.textField = [[UITextField alloc] initWithFrame:CGRectZero];
  32. self.textField.translatesAutoresizingMaskIntoConstraints = NO;
  33. self.textField.delegate = self;
  34. self.textField.textColor = [UIColor whiteColor];
  35. [self addSubview:_textField];
  36. UILayoutGuide *guide = self.layoutGuides.firstObject;
  37. if (@available(iOS 11.0, *)) {
  38. guide = self.safeAreaLayoutGuide;
  39. }
  40. [NSLayoutConstraint activateConstraints:@[
  41. [darkView.leftAnchor constraintEqualToAnchor:self.leftAnchor],
  42. [darkView.topAnchor constraintEqualToAnchor:self.topAnchor],
  43. [darkView.rightAnchor constraintEqualToAnchor:self.rightAnchor],
  44. [darkView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-1],
  45. [self.textField.leftAnchor constraintEqualToAnchor:guide.leftAnchor constant:8.0],
  46. [self.textField.topAnchor constraintEqualToAnchor:guide.topAnchor],
  47. [self.textField.rightAnchor constraintEqualToAnchor:guide.rightAnchor constant:8.0],
  48. [self.textField.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor],
  49. ]];
  50. }
  51. - (void)prepareForReuse
  52. {
  53. [super prepareForReuse];
  54. UITextField *textField = self.textField;
  55. textField.keyboardType = UIKeyboardTypeDefault;
  56. textField.text = nil;
  57. textField.secureTextEntry = NO;
  58. self.placeholderString = nil;
  59. }
  60. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  61. [super setSelected:selected animated:animated];
  62. if (selected) {
  63. [self.textField becomeFirstResponder];
  64. }
  65. }
  66. #pragma mark - Properties
  67. - (void)setPlaceholderString:(NSString *)placeholderString
  68. {
  69. UIColor *color = [UIColor VLCLightTextColor];
  70. self.textField.attributedPlaceholder = placeholderString ? [[NSAttributedString alloc] initWithString:placeholderString attributes:@{NSForegroundColorAttributeName: color}] : nil;
  71. }
  72. - (NSString *)placeholderString
  73. {
  74. return self.textField.placeholder;
  75. }
  76. #pragma mark - UITextFieldDelegate
  77. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  78. {
  79. BOOL shouldReturn = [self.delegate loginViewFieldCellShouldReturn:self];
  80. if (shouldReturn) {
  81. [textField resignFirstResponder];
  82. }
  83. return NO;
  84. }
  85. - (void)textFieldDidEndEditing:(UITextField *)textField
  86. {
  87. [self.delegate loginViewFieldCellDidEndEditing:self];
  88. }
  89. @end