VLCNetworkLoginViewFieldCell.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #import "VLC-Swift.h"
  13. NSString * const kVLCNetworkLoginViewFieldCellIdentifier = @"VLCNetworkLoginViewFieldCellIdentifier";
  14. @interface VLCNetworkLoginViewFieldCell () <UITextFieldDelegate>
  15. @end
  16. @implementation VLCNetworkLoginViewFieldCell
  17. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  18. {
  19. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  20. if (self) {
  21. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChange) name:kVLCThemeDidChangeNotification object:nil];
  22. [self themeDidChange];
  23. [self setupSubviews];
  24. }
  25. return self;
  26. }
  27. - (void)setupSubviews
  28. {
  29. _darkView = [[UIView alloc] init];
  30. _darkView.translatesAutoresizingMaskIntoConstraints = NO;
  31. [self addSubview:_darkView];
  32. self.textField = [[UITextField alloc] initWithFrame:CGRectZero];
  33. self.textField.translatesAutoresizingMaskIntoConstraints = NO;
  34. self.textField.delegate = self;
  35. self.textField.textColor = PresentationTheme.current.colors.cellTextColor;
  36. [self addSubview:_textField];
  37. id<VLCLayoutAnchorContainer> guide = self;
  38. if (@available(iOS 11.0, *)) {
  39. guide = self.safeAreaLayoutGuide;
  40. }
  41. [NSLayoutConstraint activateConstraints:@[
  42. [_darkView.leftAnchor constraintEqualToAnchor:self.leftAnchor],
  43. [_darkView.topAnchor constraintEqualToAnchor:self.topAnchor],
  44. [_darkView.rightAnchor constraintEqualToAnchor:self.rightAnchor],
  45. [_darkView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:-1],
  46. [self.textField.leftAnchor constraintEqualToAnchor:guide.leftAnchor constant:8.0],
  47. [self.textField.topAnchor constraintEqualToAnchor:guide.topAnchor],
  48. [self.textField.rightAnchor constraintEqualToAnchor:guide.rightAnchor constant:8.0],
  49. [self.textField.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor],
  50. ]];
  51. }
  52. - (void)prepareForReuse
  53. {
  54. [super prepareForReuse];
  55. UITextField *textField = self.textField;
  56. textField.keyboardType = UIKeyboardTypeDefault;
  57. textField.text = nil;
  58. textField.secureTextEntry = NO;
  59. self.placeholderString = nil;
  60. }
  61. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  62. [super setSelected:selected animated:animated];
  63. if (selected) {
  64. [self.textField becomeFirstResponder];
  65. }
  66. }
  67. - (void)themeDidChange
  68. {
  69. self.backgroundColor = PresentationTheme.current.colors.background;
  70. self.textField.textColor = PresentationTheme.current.colors.cellTextColor;
  71. _darkView.backgroundColor = PresentationTheme.current.colors.background;
  72. }
  73. #pragma mark - Properties
  74. - (void)setPlaceholderString:(NSString *)placeholderString
  75. {
  76. UIColor *color = PresentationTheme.current.colors.lightTextColor;
  77. self.textField.attributedPlaceholder = placeholderString ? [[NSAttributedString alloc] initWithString:placeholderString attributes:@{NSForegroundColorAttributeName: color}] : nil;
  78. }
  79. - (NSString *)placeholderString
  80. {
  81. return self.textField.placeholder;
  82. }
  83. #pragma mark - UITextFieldDelegate
  84. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  85. {
  86. BOOL shouldReturn = [self.delegate loginViewFieldCellShouldReturn:self];
  87. if (shouldReturn) {
  88. [textField resignFirstResponder];
  89. }
  90. return NO;
  91. }
  92. - (void)textFieldDidEndEditing:(UITextField *)textField
  93. {
  94. [self.delegate loginViewFieldCellDidEndEditing:self];
  95. }
  96. @end