SBCustomStyleViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // SBCustomStyleViewController.m
  3. // JDStatusBarNotificationExample
  4. //
  5. // Created by Markus on 08.11.13.
  6. // Copyright (c) 2013 Markus. All rights reserved.
  7. //
  8. #import <QuartzCore/QuartzCore.h>
  9. #import "JDStatusBarLayoutMarginHelper.h"
  10. #import "JDStatusBarNotification.h"
  11. #import "FTFontSelectorController.h"
  12. #import "SBSelectPropertyViewController.h"
  13. #import "InfColorPicker.h"
  14. #import "SBCustomStyleViewController.h"
  15. @interface SBCustomStyleViewController () <UITextFieldDelegate, FTFontSelectorControllerDelegate, InfColorPickerControllerDelegate>
  16. @property (nonatomic, assign) NSInteger colorMode;
  17. @property (nonatomic, assign) CGFloat progress;
  18. @property (nonatomic, weak) NSTimer *timer;
  19. @property (nonatomic, assign) JDStatusBarHeightForIPhoneX heightForIPhoneX;
  20. @property (nonatomic, assign) JDStatusBarAnimationType animationType;
  21. @property (nonatomic, assign) JDStatusBarProgressBarPosition progressBarPosition;
  22. @end
  23. @implementation SBCustomStyleViewController
  24. - (void)viewDidLoad;
  25. {
  26. [super viewDidLoad];
  27. self.animationType = JDStatusBarAnimationTypeMove;
  28. self.heightForIPhoneX = JDStatusBarHeightForIPhoneXFullNavBar;
  29. self.progressBarPosition = JDStatusBarProgressBarPositionBottom;
  30. self.textColorPreview.backgroundColor = self.fontButton.titleLabel.textColor;
  31. self.barColorPreview.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.000];
  32. self.progressBarColorPreview.backgroundColor = [UIColor redColor];
  33. self.textColorPreview.layer.cornerRadius = round(CGRectGetHeight(self.textColorPreview.frame)/3.0);
  34. self.barColorPreview.layer.cornerRadius = self.textColorPreview.layer.cornerRadius;
  35. self.progressBarColorPreview.layer.cornerRadius = self.textColorPreview.layer.cornerRadius;
  36. [self updateFontText];
  37. [self updateStyle];
  38. [self adjustForLayoutMargin];
  39. }
  40. - (void)adjustForLayoutMargin
  41. {
  42. // adjust bottom bar to respect layout margins
  43. CGFloat bottomLayoutMargin = JDStatusBarRootVCLayoutMargin().bottom;
  44. CGRect frame = self.bottomBarView.frame;
  45. frame.origin.y -= bottomLayoutMargin;
  46. frame.size.height += bottomLayoutMargin;
  47. self.bottomBarView.frame = frame;
  48. CGRect scrollViewFrame = self.scrollView.frame;
  49. scrollViewFrame.size.height -= bottomLayoutMargin;
  50. self.scrollView.frame = scrollViewFrame;
  51. }
  52. - (void)viewDidLayoutSubviews;
  53. {
  54. [super viewDidLayoutSubviews];
  55. self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width,
  56. self.lastRow.frame.origin.y + self.lastRow.frame.size.height + 10.0);
  57. }
  58. #pragma mark UI Updates
  59. - (void)updateFontText;
  60. {
  61. NSString *title = [NSString stringWithFormat: @"Change font (%.1f pt)",
  62. self.fontButton.titleLabel.font.pointSize];
  63. [self.fontButton setTitle:title forState:UIControlStateNormal];
  64. self.textColorPreview.backgroundColor = self.fontButton.titleLabel.textColor;
  65. }
  66. - (void)updateStyle;
  67. {
  68. [JDStatusBarNotification addStyleNamed:@"style" prepare:^JDStatusBarStyle *(JDStatusBarStyle *style) {
  69. style.font = self.fontButton.titleLabel.font;
  70. style.textColor = self.textColorPreview.backgroundColor;
  71. style.barColor = self.barColorPreview.backgroundColor;
  72. style.animationType = self.animationType;
  73. style.heightForIPhoneX = self.heightForIPhoneX;
  74. style.progressBarColor = self.progressBarColorPreview.backgroundColor;
  75. style.progressBarPosition = self.progressBarPosition;
  76. NSString *height = [self.barHeightLabel.text stringByReplacingOccurrencesOfString:@"ProgressBarHeight (" withString:@""];
  77. height = [height stringByReplacingOccurrencesOfString:@" pt)" withString:@""];
  78. style.progressBarHeight = [height doubleValue];
  79. return style;
  80. }];
  81. }
  82. #pragma mark UITextFieldDelegate
  83. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  84. {
  85. [textField resignFirstResponder];
  86. if (textField.text.length == 0) {
  87. textField.text = @"Notification Text";
  88. }
  89. [self show:nil];
  90. return YES;
  91. }
  92. #pragma mark FTFontSelectorControllerDelegate
  93. - (void)fontSelectorControllerShouldBeDismissed:(FTFontSelectorController *)controller;
  94. {
  95. [self dismissViewControllerAnimated:YES completion:nil];
  96. }
  97. - (void)fontSelectorController:(FTFontSelectorController *)controller didChangeSelectedFontName:(NSString *)fontName;
  98. {
  99. self.fontButton.titleLabel.font = [UIFont fontWithName:fontName size:self.fontButton.titleLabel.font.pointSize];
  100. [self updateFontText];
  101. [self updateStyle];
  102. }
  103. #pragma mark InfColorPicker
  104. - (void)showColorPickerWithColor:(UIColor*)color;
  105. {
  106. InfColorPickerController *colorController = [InfColorPickerController colorPickerViewController];
  107. colorController.delegate = self;
  108. colorController.sourceColor = color;
  109. colorController.resultColor = color;
  110. [colorController presentModallyOverViewController:self];
  111. if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
  112. UIView *view = colorController.view;
  113. colorController.view = [[UIView alloc] initWithFrame:view.frame];
  114. colorController.view.backgroundColor = [UIColor blackColor];
  115. view.frame = CGRectMake(0, 64, view.bounds.size.width, view.bounds.size.height-64);
  116. [colorController.view addSubview:view];
  117. }
  118. }
  119. #pragma mark InfColorPickerControllerDelegate
  120. - (void)colorPickerControllerDidChangeColor:(InfColorPickerController *)controller;
  121. {
  122. switch (self.colorMode) {
  123. case 0: {
  124. [self.fontButton setTitleColor:controller.resultColor forState:UIControlStateNormal];
  125. self.textColorPreview.backgroundColor = controller.resultColor;
  126. [self updateFontText];
  127. break;
  128. }
  129. case 1: {
  130. self.barColorPreview.backgroundColor = controller.resultColor;
  131. break;
  132. }
  133. case 2: {
  134. self.progressBarColorPreview.backgroundColor = controller.resultColor;
  135. break;
  136. }
  137. }
  138. [self updateStyle];
  139. }
  140. - (void)colorPickerControllerDidFinish:(InfColorPickerController *)controller;
  141. {
  142. [self dismissViewControllerAnimated:YES completion:nil];
  143. }
  144. #pragma mark Actions
  145. - (IBAction)selectFont:(id)sender;
  146. {
  147. FTFontSelectorController *fontController = [[FTFontSelectorController alloc] initWithSelectedFontName:self.fontButton.titleLabel.font.fontName];
  148. [fontController setFontDelegate:self];
  149. [self presentViewController:fontController
  150. animated:YES completion:nil];
  151. }
  152. - (IBAction)selectFontSize:(UIStepper*)sender;
  153. {
  154. self.fontButton.titleLabel.font = [UIFont fontWithName:self.fontButton.titleLabel.font.fontName size:sender.value];
  155. [self updateFontText];
  156. [self updateStyle];
  157. }
  158. - (IBAction)selectTextColor:(id)sender;
  159. {
  160. self.colorMode = 0;
  161. [self showColorPickerWithColor:self.textColorPreview.backgroundColor];
  162. }
  163. - (IBAction)selectBarColor:(id)sender;
  164. {
  165. self.colorMode = 1;
  166. [self showColorPickerWithColor:self.barColorPreview.backgroundColor];
  167. }
  168. - (IBAction)selectAnimationStyle:(id)sender;
  169. {
  170. NSArray *data = @[@"JDStatusBarAnimationTypeNone",
  171. @"JDStatusBarAnimationTypeMove",
  172. @"JDStatusBarAnimationTypeBounce",
  173. @"JDStatusBarAnimationTypeFade"];
  174. SBSelectPropertyViewController *controller = [[SBSelectPropertyViewController alloc] initWithData:data resultBlock:^(NSInteger selectedRow) {
  175. self.animationType = selectedRow;
  176. [self.animationStyleButton setTitle:data[selectedRow] forState:UIControlStateNormal];
  177. [self.navigationController popViewControllerAnimated:YES];
  178. [self updateStyle];
  179. }];
  180. controller.title = @"Animation Type";
  181. controller.activeRow = self.animationType;
  182. [self.navigationController pushViewController:controller animated:YES];
  183. }
  184. - (IBAction)selectIPhoneXHeight:(id)sender;
  185. {
  186. NSArray *data = @[@"JDStatusBarHeightForIPhoneXHalf",
  187. @"JDStatusBarHeightForIPhoneXFullNavBar"];
  188. SBSelectPropertyViewController *controller = [[SBSelectPropertyViewController alloc] initWithData:data resultBlock:^(NSInteger selectedRow) {
  189. self.heightForIPhoneX = selectedRow;
  190. [self.heightForIPhoneXButton setTitle:data[selectedRow] forState:UIControlStateNormal];
  191. [self.navigationController popViewControllerAnimated:YES];
  192. [self updateStyle];
  193. }];
  194. controller.title = @"Height for iPhoneX";
  195. controller.activeRow = self.heightForIPhoneX;
  196. [self.navigationController pushViewController:controller animated:YES];
  197. }
  198. - (IBAction)selectProgressBarColor:(id)sender;
  199. {
  200. self.colorMode = 2;
  201. [self showColorPickerWithColor:self.progressBarColorPreview.backgroundColor];
  202. }
  203. - (IBAction)selectProgressBarPosition:(id)sender;
  204. {
  205. NSArray *data = @[@"JDStatusBarProgressBarPositionBottom",
  206. @"JDStatusBarProgressBarPositionCenter",
  207. @"JDStatusBarProgressBarPositionTop",
  208. @"JDStatusBarProgressBarPositionBelow",
  209. @"JDStatusBarProgressBarPositionNavBar"];
  210. SBSelectPropertyViewController *controller = [[SBSelectPropertyViewController alloc] initWithData:data resultBlock:^(NSInteger selectedRow) {
  211. self.progressBarPosition = selectedRow;
  212. [self.barPositionButton setTitle:data[selectedRow] forState:UIControlStateNormal];
  213. [self.navigationController popViewControllerAnimated:YES];
  214. [self updateStyle];
  215. }];
  216. controller.title = @"Progress Bar Position";
  217. controller.activeRow = self.progressBarPosition;
  218. [self.navigationController pushViewController:controller animated:YES];
  219. }
  220. - (IBAction)setProgressBarHeight:(UIStepper*)sender;
  221. {
  222. if (sender.value < 1) sender.value = 0.5;
  223. if (sender.value >= 1) sender.value = round(sender.value);
  224. self.barHeightLabel.text = [NSString stringWithFormat: @"ProgressBarHeight (%.1f pt)", sender.value];
  225. [self updateStyle];
  226. }
  227. #pragma mark Presentation
  228. - (IBAction)show:(id)sender;
  229. {
  230. [JDStatusBarNotification showWithStatus:self.textField.text dismissAfter:2.0 styleName:@"style"];
  231. }
  232. - (IBAction)showWithProgress:(id)sender;
  233. {
  234. double delayInSeconds = [JDStatusBarNotification isVisible] ? 0.0 : 0.25;
  235. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
  236. dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  237. self.progress = 0.0;
  238. [self startTimer];
  239. });
  240. [JDStatusBarNotification showWithStatus:self.textField.text dismissAfter:1.3 styleName:@"style"];
  241. }
  242. #pragma mark Progress Timer
  243. - (void)startTimer;
  244. {
  245. [JDStatusBarNotification showProgress:self.progress];
  246. [self.timer invalidate];
  247. self.timer = nil;
  248. if (self.progress < 1.0) {
  249. CGFloat step = 0.02;
  250. self.timer = [NSTimer scheduledTimerWithTimeInterval:step target:self
  251. selector:@selector(startTimer)
  252. userInfo:nil repeats:NO];
  253. self.progress += step;
  254. } else {
  255. [self performSelector:@selector(hideProgress)
  256. withObject:nil afterDelay:0.5];
  257. }
  258. }
  259. - (void)hideProgress;
  260. {
  261. [JDStatusBarNotification showProgress:0.0];
  262. }
  263. @end