VLCNetworkLoginViewFieldCell.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. self.backgroundColor = PresentationTheme.current.colors.background;
  22. [self setupSubviews];
  23. }
  24. return self;
  25. }
  26. - (void)setupSubviews
  27. {
  28. UIView *darkView = [[UIView alloc] init];
  29. darkView.translatesAutoresizingMaskIntoConstraints = NO;
  30. [self addSubview:darkView];
  31. darkView.backgroundColor = PresentationTheme.current.colors.background;
  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. #pragma mark - Properties
  68. - (void)setPlaceholderString:(NSString *)placeholderString
  69. {
  70. UIColor *color = PresentationTheme.current.colors.lightTextColor;
  71. self.textField.attributedPlaceholder = placeholderString ? [[NSAttributedString alloc] initWithString:placeholderString attributes:@{NSForegroundColorAttributeName: color}] : nil;
  72. }
  73. - (NSString *)placeholderString
  74. {
  75. return self.textField.placeholder;
  76. }
  77. #pragma mark - UITextFieldDelegate
  78. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  79. {
  80. BOOL shouldReturn = [self.delegate loginViewFieldCellShouldReturn:self];
  81. if (shouldReturn) {
  82. [textField resignFirstResponder];
  83. }
  84. return NO;
  85. }
  86. - (void)textFieldDidEndEditing:(UITextField *)textField
  87. {
  88. [self.delegate loginViewFieldCellDidEndEditing:self];
  89. }
  90. @end