PageRenderTime 73ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/UICatalog/ViewControllers/ButtonsViewController.m

https://gitlab.com/praveenvelanati/ios-demo
Objective C | 447 lines | 280 code | 90 blank | 77 comment | 21 complexity | f0993e4b6fcda4263c87f2467bfdf2b5 MD5 | raw file
  1. /*
  2. File: ButtonsViewController.m
  3. Abstract: The table view controller for hosting the UIButton features of this sample.
  4. Version: 2.11
  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) 2013 Apple Inc. All Rights Reserved.
  40. */
  41. #import "ButtonsViewController.h"
  42. #import "Constants.h"
  43. #define kStdButtonWidth 106.0
  44. #define kStdButtonHeight 40.0
  45. #define kViewTag 1 // for tagging our embedded controls for removal at cell recycle time
  46. static NSString *kSectionTitleKey = @"sectionTitleKey";
  47. static NSString *kLabelKey = @"labelKey";
  48. static NSString *kSourceKey = @"sourceKey";
  49. static NSString *kViewKey = @"viewKey";
  50. // tableView cell id constants
  51. static NSString *kDisplayCellID = @"DisplayCellID";
  52. static NSString *kSourceCellID = @"SourceCellID";
  53. #pragma mark -
  54. @interface ButtonsViewController ()
  55. @property (nonatomic, strong) UIButton *grayButton;
  56. @property (nonatomic, strong) UIButton *imageButton;
  57. @property (nonatomic, strong) UIButton *attrTextButton;
  58. @property (nonatomic, strong) UIButton *roundedButtonType;
  59. @property (nonatomic, strong) UIButton *detailDisclosureButtonType;
  60. @property (nonatomic, strong) UIButton *infoLightButtonType;
  61. @property (nonatomic, strong) UIButton *infoDarkButtonType;
  62. @property (nonatomic, strong) UIButton *contactAddButtonType;
  63. @property (nonatomic, strong) NSArray *dataSourceArray;
  64. @end
  65. #pragma mark -
  66. @implementation ButtonsViewController
  67. - (void)viewDidLoad
  68. {
  69. [super viewDidLoad];
  70. self.title = NSLocalizedString(@"ButtonsTitle", @"");
  71. self.dataSourceArray = @[
  72. @{ kSectionTitleKey:@"UIButton",
  73. kLabelKey:@"Background Image",
  74. kSourceKey:@"ButtonsViewController.m:\r(UIButton *)grayButton",
  75. kViewKey:self.grayButton },
  76. @{ kSectionTitleKey:@"UIButton",
  77. kLabelKey:@"Button with Image",
  78. kSourceKey:@"ButtonsViewController.m:\r(UIButton *)imageButton",
  79. kViewKey:self.imageButton },
  80. @{ kSectionTitleKey:@"UIButtonTypeRoundedRect",
  81. kLabelKey:@"Rounded Button",
  82. kSourceKey:@"ButtonsViewController.m:\r(UIButton *)roundedButtonType",
  83. kViewKey:self.roundedButtonType },
  84. @{ kSectionTitleKey:@"UIButtonTypeRoundedRect",
  85. kLabelKey:@"Attributed Text",
  86. kSourceKey:@"ButtonsViewController.m:\r(UIButton *)attrTextButton",
  87. kViewKey:self.attrTextButton },
  88. @{ kSectionTitleKey:@"UIButtonTypeDetailDisclosure",
  89. kLabelKey:@"Detail Disclosure",
  90. kSourceKey:@"ButtonsViewController.m:\r(UIButton *)detailDisclosureButton",
  91. kViewKey:self.detailDisclosureButtonType },
  92. @{ kSectionTitleKey:@"UIButtonTypeInfoLight",
  93. kLabelKey:@"Info Light",
  94. kSourceKey:@"ButtonsViewController.m:\r(UIButton *)infoLightButtonType",
  95. kViewKey:self.infoLightButtonType},
  96. @{ kSectionTitleKey:@"UIButtonTypeInfoDark",
  97. kLabelKey:@"Info Dark",
  98. kSourceKey:@"ButtonsViewController.m:\r(UIButton *)infoDarkButtonType",
  99. kViewKey:self.infoDarkButtonType},
  100. @{ kSectionTitleKey:@"UIButtonTypeContactAdd",
  101. kLabelKey:@"Contact Add",
  102. kSourceKey:@"ButtonsViewController.m:\r(UIButton *)contactAddButtonType",
  103. kViewKey:self.contactAddButtonType}
  104. ];
  105. // register our cell IDs for later when we are asked for UITableViewCells
  106. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kDisplayCellID];
  107. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kSourceCellID];
  108. }
  109. #pragma mark - UITableViewDataSource
  110. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  111. {
  112. return [self.dataSourceArray count];
  113. }
  114. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  115. {
  116. return [[self.dataSourceArray objectAtIndex: section] valueForKey:kSectionTitleKey];
  117. }
  118. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  119. {
  120. return 2;
  121. }
  122. // to determine specific row height for each cell, override this.
  123. // In this example, each row is determined by its subviews that are embedded.
  124. //
  125. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. return ([indexPath row] == 0) ? 50.0 : 38.0;
  128. }
  129. // to determine which UITableViewCell to be used on a given row.
  130. //
  131. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. UITableViewCell *cell = nil;
  134. if ([indexPath row] == 0)
  135. {
  136. cell = [tableView dequeueReusableCellWithIdentifier:kDisplayCellID forIndexPath:indexPath];
  137. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  138. // remove old embedded control
  139. UIView *viewToRemove = nil;
  140. viewToRemove = [cell.contentView viewWithTag:kViewTag];
  141. if (viewToRemove)
  142. [viewToRemove removeFromSuperview];
  143. cell.textLabel.text = [[self.dataSourceArray objectAtIndex:indexPath.section] valueForKey:kLabelKey];
  144. UIButton *button = [[self.dataSourceArray objectAtIndex:indexPath.section] valueForKey:kViewKey];
  145. // make sure this button is right-justified to the right side of the cell
  146. CGRect newFrame = button.frame;
  147. newFrame.origin.x = CGRectGetWidth(cell.contentView.frame) - CGRectGetWidth(newFrame) - 10.0;
  148. button.frame = newFrame;
  149. // if the cell is ever resized, keep the button over to the right
  150. button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  151. [cell.contentView addSubview:button];
  152. }
  153. else
  154. {
  155. cell = [tableView dequeueReusableCellWithIdentifier:kSourceCellID forIndexPath:indexPath];
  156. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  157. cell.textLabel.opaque = NO;
  158. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  159. cell.textLabel.textColor = [UIColor grayColor];
  160. cell.textLabel.numberOfLines = 2;
  161. cell.textLabel.highlightedTextColor = [UIColor blackColor];
  162. cell.textLabel.font = [UIFont systemFontOfSize:12.0];
  163. cell.textLabel.text = [[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kSourceKey];
  164. }
  165. return cell;
  166. }
  167. #pragma mark -
  168. + (UIButton *)newButtonWithTitle:(NSString *)title
  169. target:(id)target
  170. selector:(SEL)selector
  171. frame:(CGRect)frame
  172. image:(UIImage *)image
  173. imagePressed:(UIImage *)imagePressed
  174. darkTextColor:(BOOL)darkTextColor
  175. {
  176. UIButton *button = [[UIButton alloc] initWithFrame:frame];
  177. // or you can do this:
  178. // UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
  179. button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  180. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  181. [button setTitle:title forState:UIControlStateNormal];
  182. if (darkTextColor)
  183. {
  184. [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  185. }
  186. else
  187. {
  188. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  189. }
  190. UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
  191. [button setBackgroundImage:newImage forState:UIControlStateNormal];
  192. UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
  193. [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
  194. [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
  195. // in case the parent view draws with a custom color or gradient, use a transparent color
  196. button.backgroundColor = [UIColor clearColor];
  197. return button;
  198. }
  199. - (void)action:(id)sender
  200. {
  201. //NSLog(@"UIButton was clicked");
  202. }
  203. #pragma mark - Lazy creation of buttons
  204. - (UIButton *)grayButton
  205. {
  206. if (_grayButton == nil)
  207. {
  208. // create the UIButtons with various background images
  209. // white button:
  210. UIImage *buttonBackground = [UIImage imageNamed:@"whiteButton.png"];
  211. UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"blueButton.png"];
  212. CGRect frame = CGRectMake(0.0, 5.0, kStdButtonWidth, kStdButtonHeight);
  213. _grayButton = [ButtonsViewController newButtonWithTitle:@"Gray"
  214. target:self
  215. selector:@selector(action:)
  216. frame:frame
  217. image:buttonBackground
  218. imagePressed:buttonBackgroundPressed
  219. darkTextColor:YES];
  220. _grayButton.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  221. }
  222. return _grayButton;
  223. }
  224. - (UIButton *)imageButton
  225. {
  226. if (_imageButton == nil)
  227. {
  228. // create a UIButton with just an image instead of a title
  229. UIImage *buttonBackground = [UIImage imageNamed:@"whiteButton.png"];
  230. UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"blueButton.png"];
  231. CGRect frame = CGRectMake(0.0, 5.0, kStdButtonWidth, kStdButtonHeight);
  232. _imageButton = [ButtonsViewController newButtonWithTitle:@""
  233. target:self
  234. selector:@selector(action:)
  235. frame:frame
  236. image:buttonBackground
  237. imagePressed:buttonBackgroundPressed
  238. darkTextColor:YES];
  239. [_imageButton setImage:[UIImage imageNamed:@"UIButton_custom.png"] forState:UIControlStateNormal];
  240. // Add an accessibility label to the image.
  241. [_imageButton setAccessibilityLabel:NSLocalizedString(@"ArrowButton", @"")];
  242. _imageButton.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  243. }
  244. return _imageButton;
  245. }
  246. - (UIButton *)attrTextButton
  247. {
  248. if (_attrTextButton == nil)
  249. {
  250. // create a UIButton with attributed text for its title
  251. _attrTextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  252. _attrTextButton.frame = CGRectMake(0.0, 5.0, kStdButtonWidth, kStdButtonHeight);
  253. [_attrTextButton setTitle:@"Rounded" forState:UIControlStateNormal];
  254. [_attrTextButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  255. // Add an accessibility label to the image.
  256. [_attrTextButton setAccessibilityLabel:NSLocalizedString(@"AttrTextButton", @"")];
  257. // apply red text for normal state
  258. NSMutableAttributedString *normalAttrString = [[NSMutableAttributedString alloc] initWithString:@"Rounded"];
  259. [normalAttrString addAttribute:NSForegroundColorAttributeName
  260. value:[UIColor redColor]
  261. range:NSMakeRange(0, [normalAttrString length])];
  262. [_attrTextButton setAttributedTitle:normalAttrString forState:UIControlStateNormal];
  263. // apply green text for pressed state
  264. NSMutableAttributedString *highlightedAttrString = [[NSMutableAttributedString alloc] initWithString:@"Rounded"];
  265. [highlightedAttrString addAttribute:NSForegroundColorAttributeName
  266. value:[UIColor greenColor]
  267. range:NSMakeRange(0, [highlightedAttrString length])];
  268. [_attrTextButton setAttributedTitle:highlightedAttrString forState:UIControlStateHighlighted];
  269. _attrTextButton.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  270. }
  271. return _attrTextButton;
  272. }
  273. - (UIButton *)roundedButtonType
  274. {
  275. if (_roundedButtonType == nil)
  276. {
  277. // create a UIButton (UIButtonTypeRoundedRect)
  278. _roundedButtonType = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  279. _roundedButtonType.frame = CGRectMake(0.0, 5.0, kStdButtonWidth, kStdButtonHeight);
  280. [_roundedButtonType setTitle:@"Rounded" forState:UIControlStateNormal];
  281. [_roundedButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  282. _roundedButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  283. }
  284. return _roundedButtonType;
  285. }
  286. - (UIButton *)detailDisclosureButtonType
  287. {
  288. if (_detailDisclosureButtonType == nil)
  289. {
  290. // create a UIButton (UIButtonTypeDetailDisclosure)
  291. _detailDisclosureButtonType = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  292. _detailDisclosureButtonType.frame = CGRectMake(0.0, 8.0, 25.0, 25.0);
  293. [_detailDisclosureButtonType setTitle:@"Detail Disclosure" forState:UIControlStateNormal];
  294. _detailDisclosureButtonType.backgroundColor = [UIColor clearColor];
  295. [_detailDisclosureButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  296. // Add a custom accessibility label to the button because it has no associated text.
  297. [_detailDisclosureButtonType setAccessibilityLabel:NSLocalizedString(@"MoreInfoButton", @"")];
  298. _detailDisclosureButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  299. }
  300. return _detailDisclosureButtonType;
  301. }
  302. - (UIButton *)infoDarkButtonType
  303. {
  304. if (_infoDarkButtonType == nil)
  305. {
  306. // create a UIButton (UIButtonTypeInfoLight)
  307. _infoDarkButtonType = [UIButton buttonWithType:UIButtonTypeInfoDark];
  308. _infoDarkButtonType.frame = CGRectMake(0.0, 8.0, 25.0, 25.0);
  309. [_infoDarkButtonType setTitle:@"Detail Disclosure" forState:UIControlStateNormal];
  310. _infoDarkButtonType.backgroundColor = [UIColor clearColor];
  311. [_infoDarkButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  312. // Add a custom accessibility label to the button because it has no associated text.
  313. [_infoDarkButtonType setAccessibilityLabel:NSLocalizedString(@"MoreInfoButton", @"")];
  314. _infoDarkButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  315. }
  316. return _infoDarkButtonType;
  317. }
  318. - (UIButton *)infoLightButtonType
  319. {
  320. if (_infoLightButtonType == nil)
  321. {
  322. // create a UIButton (UIButtonTypeInfoLight)
  323. _infoLightButtonType = [UIButton buttonWithType:UIButtonTypeInfoLight];
  324. _infoLightButtonType.frame = CGRectMake(0.0, 8.0, 25.0, 25.0);
  325. [_infoLightButtonType setTitle:@"Detail Disclosure" forState:UIControlStateNormal];
  326. _infoLightButtonType.backgroundColor = [UIColor clearColor];
  327. [_infoLightButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  328. _infoLightButtonType.backgroundColor = [UIColor grayColor];
  329. // Add a custom accessibility label to the button because it has no associated text.
  330. [_infoLightButtonType setAccessibilityLabel:NSLocalizedString(@"MoreInfoButton", @"")];
  331. _infoLightButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  332. }
  333. return _infoLightButtonType;
  334. }
  335. - (UIButton *)contactAddButtonType
  336. {
  337. if (_contactAddButtonType == nil)
  338. {
  339. // create a UIButton (UIButtonTypeContactAdd)
  340. _contactAddButtonType = [UIButton buttonWithType:UIButtonTypeContactAdd];
  341. _contactAddButtonType.frame = CGRectMake(0.0, 8.0, 25.0, 25.0);
  342. [_contactAddButtonType setTitle:@"Detail Disclosure" forState:UIControlStateNormal];
  343. _contactAddButtonType.backgroundColor = [UIColor clearColor];
  344. [_contactAddButtonType addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
  345. // Add a custom accessibility label to the button because it has no associated text.
  346. [_contactAddButtonType setAccessibilityLabel:NSLocalizedString(@"AddContactButton", @"")];
  347. _contactAddButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
  348. }
  349. return _contactAddButtonType;
  350. }
  351. @end