AAPLButtonViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. File: AAPLButtonViewController.m
  3. Abstract: A view controller that demonstrates how to use UIButton. The buttons are created using storyboards, but each of the system buttons can be created in code by using the +[UIButton buttonWithType:] initializer. See UIButton.h for a comprehensive list of the various UIButtonType values.
  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 "AAPLButtonViewController.h"
  42. @interface AAPLButtonViewController()
  43. @property (nonatomic, weak) IBOutlet UIButton *systemTextButton;
  44. @property (nonatomic, weak) IBOutlet UIButton *systemContactAddButton;
  45. @property (nonatomic, weak) IBOutlet UIButton *systemDetailDisclosureButton;
  46. @property (nonatomic, weak) IBOutlet UIButton *imageButton;
  47. @property (nonatomic, weak) IBOutlet UIButton *attributedTextButton;
  48. @end
  49. #pragma mark -
  50. @implementation AAPLButtonViewController
  51. - (void)viewDidLoad {
  52. [super viewDidLoad];
  53. // All of the buttons are created in the storyboard, but configured below.
  54. [self configureSystemTextButton];
  55. [self configureSystemContactAddButton];
  56. [self configureSystemDetailDisclosureButton];
  57. [self configureImageButton];
  58. [self configureAttributedTextSystemButton];
  59. }
  60. #pragma mark - Configuration
  61. - (void)configureSystemTextButton {
  62. [self.systemTextButton setTitle:NSLocalizedString(@"Button", nil) forState:UIControlStateNormal];
  63. [self.systemTextButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  64. }
  65. - (void)configureSystemContactAddButton {
  66. self.systemContactAddButton.backgroundColor = UIColor.clearColor;
  67. [self.systemContactAddButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  68. }
  69. - (void)configureSystemDetailDisclosureButton {
  70. self.systemDetailDisclosureButton.backgroundColor = UIColor.clearColor;
  71. [self.systemDetailDisclosureButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  72. }
  73. - (void)configureImageButton {
  74. // To create this button in code you can use +[UIButton buttonWithType:] with a parameter value of UIButtonTypeCustom.
  75. // Remove the title text.
  76. [self.imageButton setTitle:@"" forState:UIControlStateNormal];
  77. self.imageButton.tintColor = UIColor.aapl_applicationPurpleColor;
  78. [self.imageButton setImage:[UIImage imageNamed:@"x_icon"] forState:UIControlStateNormal];
  79. // Add an accessibility label to the image.
  80. self.imageButton.accessibilityLabel = NSLocalizedString(@"X Button", nil);
  81. [self.imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  82. }
  83. - (void)configureAttributedTextSystemButton {
  84. NSDictionary *titleAttributes = @{NSForegroundColorAttributeName: UIColor.aapl_applicationBlueColor, NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle)};
  85. NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"Button", nil) attributes:titleAttributes];
  86. [self.attributedTextButton setAttributedTitle:attributedTitle forState:UIControlStateNormal];
  87. NSDictionary *highlightedTitleAttributes = @{NSForegroundColorAttributeName : UIColor.aapl_applicationGreenColor, NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick)};
  88. NSAttributedString *highlightedAttributedTitle = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"Button", nil) attributes:highlightedTitleAttributes];
  89. [self.attributedTextButton setAttributedTitle:highlightedAttributedTitle forState:UIControlStateHighlighted];
  90. [self.attributedTextButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  91. }
  92. #pragma mark - Actions
  93. // Handler for all of AAPLButtonViewController's UIButton actions.
  94. - (void)buttonClicked:(UIButton *)button {
  95. NSLog(@"A button was clicked: %@.", button);
  96. }
  97. @end