AAPLTextFieldViewController.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. File: AAPLTextFieldViewController.m
  3. Abstract: A view controller that demonstrates how to use UITextField.
  4. Version: 2.12
  5. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
  6. Inc. ("Apple") in consideration of your agreement to the following
  7. terms, and your use, installation, modification or redistribution of
  8. this Apple software constitutes acceptance of these terms. If you do
  9. not agree with these terms, please do not use, install, modify or
  10. redistribute this Apple software.
  11. In consideration of your agreement to abide by the following terms, and
  12. subject to these terms, Apple grants you a personal, non-exclusive
  13. license, under Apple's copyrights in this original Apple software (the
  14. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  15. Software, with or without modifications, in source and/or binary forms;
  16. provided that if you redistribute the Apple Software in its entirety and
  17. without modifications, you must retain this notice and the following
  18. text and disclaimers in all such redistributions of the Apple Software.
  19. Neither the name, trademarks, service marks or logos of Apple Inc. may
  20. be used to endorse or promote products derived from the Apple Software
  21. without specific prior written permission from Apple. Except as
  22. expressly stated in this notice, no other rights or licenses, express or
  23. implied, are granted by Apple herein, including but not limited to any
  24. patent rights that may be infringed by your derivative works or by other
  25. works in which the Apple Software may be incorporated.
  26. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  27. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  28. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  29. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  30. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  31. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  32. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  35. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  36. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  37. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  38. POSSIBILITY OF SUCH DAMAGE.
  39. Copyright (C) 2014 Apple Inc. All Rights Reserved.
  40. */
  41. #import "AAPLTextFieldViewController.h"
  42. @interface AAPLTextFieldViewController()<UITextFieldDelegate>
  43. @property (nonatomic, weak) IBOutlet UITextField *textField;
  44. @property (nonatomic, weak) IBOutlet UITextField *tintedTextField;
  45. @property (nonatomic, weak) IBOutlet UITextField *secureTextField;
  46. @property (nonatomic, weak) IBOutlet UITextField *specificKeyboardTextField;
  47. @property (nonatomic, weak) IBOutlet UITextField *customTextField;
  48. @end
  49. #pragma mark -
  50. @implementation AAPLTextFieldViewController
  51. - (void)viewDidLoad {
  52. [super viewDidLoad];
  53. [self configureTextField];
  54. [self configureTintedTextField];
  55. [self configureSecureTextField];
  56. [self configureSpecificKeyboardTextField];
  57. [self configureCustomTextField];
  58. }
  59. #pragma mark - Configuration
  60. - (void)configureTextField {
  61. self.textField.placeholder = NSLocalizedString(@"Placeholder text", nil);
  62. self.textField.autocorrectionType = UITextAutocorrectionTypeYes;
  63. self.textField.returnKeyType = UIReturnKeyDone;
  64. self.textField.clearButtonMode = UITextFieldViewModeNever;
  65. }
  66. - (void)configureTintedTextField {
  67. self.tintedTextField.tintColor = UIColor.aapl_applicationBlueColor;
  68. self.tintedTextField.textColor = UIColor.aapl_applicationGreenColor;
  69. self.tintedTextField.placeholder = NSLocalizedString(@"Placeholder text", nil);
  70. self.tintedTextField.returnKeyType = UIReturnKeyDone;
  71. self.tintedTextField.clearButtonMode = UITextFieldViewModeNever;
  72. }
  73. - (void)configureSecureTextField {
  74. self.secureTextField.secureTextEntry = YES;
  75. self.secureTextField.placeholder = NSLocalizedString(@"Placeholder text", nil);
  76. self.secureTextField.returnKeyType = UIReturnKeyDone;
  77. self.secureTextField.clearButtonMode = UITextFieldViewModeAlways;
  78. }
  79. /// There are many different types of keyboards that you may choose to use.
  80. /// The different types of keyboards are defined in UITextInputTraits.h.
  81. /// This example shows how to display a keyboard to help enter email addresses.
  82. - (void)configureSpecificKeyboardTextField {
  83. self.specificKeyboardTextField.keyboardType = UIKeyboardTypeEmailAddress;
  84. self.specificKeyboardTextField.placeholder = NSLocalizedString(@"Placeholder text", nil);
  85. self.specificKeyboardTextField.returnKeyType = UIReturnKeyDone;
  86. }
  87. - (void)configureCustomTextField {
  88. // Text fields with custom image backgrounds must have no border.
  89. self.customTextField.borderStyle = UITextBorderStyleNone;
  90. self.customTextField.background = [UIImage imageNamed:@"text_field_background"];
  91. // Create purple button that, when selected, turns the custom text field's text color to purple.
  92. UIImage *purpleImage = [UIImage imageNamed:@"text_field_purple_right_view"];
  93. UIButton *purpleImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
  94. purpleImageButton.bounds = CGRectMake(0, 0, purpleImage.size.width, purpleImage.size.height);
  95. purpleImageButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
  96. [purpleImageButton setImage:purpleImage forState:UIControlStateNormal];
  97. [purpleImageButton addTarget:self action:@selector(customTextFieldPurpleButtonClicked) forControlEvents:UIControlEventTouchUpInside];
  98. self.customTextField.rightView = purpleImageButton;
  99. self.customTextField.rightViewMode = UITextFieldViewModeAlways;
  100. // Add an empty view as the left view to ensure inset between the text and the bounding rectangle.
  101. UIView *leftPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
  102. leftPaddingView.backgroundColor = UIColor.clearColor;
  103. self.customTextField.leftView = leftPaddingView;
  104. self.customTextField.leftViewMode = UITextFieldViewModeAlways;
  105. self.customTextField.placeholder = NSLocalizedString(@"Placeholder text", nil);
  106. self.customTextField.autocorrectionType = UITextAutocorrectionTypeNo;
  107. self.customTextField.returnKeyType = UIReturnKeyDone;
  108. }
  109. #pragma mark - UITextFieldDelegate (set in Interface Builder)
  110. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  111. [textField resignFirstResponder];
  112. return YES;
  113. }
  114. #pragma mark - Actions
  115. - (void)customTextFieldPurpleButtonClicked {
  116. self.customTextField.textColor = UIColor.aapl_applicationPurpleColor;
  117. NSLog(@"The custom text field's purple right view button was clicked.");
  118. }
  119. @end