UIView+Toast.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // UIView+Toast.h
  3. // Toast
  4. //
  5. // Copyright (c) 2011-2016 Charles Scalesse.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a
  8. // copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included
  16. // in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. #import <UIKit/UIKit.h>
  26. extern const NSString * CSToastPositionTop;
  27. extern const NSString * CSToastPositionCenter;
  28. extern const NSString * CSToastPositionBottom;
  29. @class CSToastStyle;
  30. /**
  31. Toast is an Objective-C category that adds toast notifications to the UIView
  32. object class. It is intended to be simple, lightweight, and easy to use. Most
  33. toast notifications can be triggered with a single line of code.
  34. The `makeToast:` methods create a new view and then display it as toast.
  35. The `showToast:` methods display any view as toast.
  36. */
  37. @interface UIView (Toast)
  38. /**
  39. Creates and presents a new toast view with a message and displays it with the
  40. default duration and position. Styled using the shared style.
  41. @param message The message to be displayed
  42. */
  43. - (void)makeToast:(NSString *)message;
  44. /**
  45. Creates and presents a new toast view with a message. Duration and position
  46. can be set explicitly. Styled using the shared style.
  47. @param message The message to be displayed
  48. @param duration The toast duration
  49. @param position The toast's center point. Can be one of the predefined CSToastPosition
  50. constants or a `CGPoint` wrapped in an `NSValue` object.
  51. */
  52. - (void)makeToast:(NSString *)message
  53. duration:(NSTimeInterval)duration
  54. position:(id)position;
  55. /**
  56. Creates and presents a new toast view with a message. Duration, position, and
  57. style can be set explicitly.
  58. @param message The message to be displayed
  59. @param duration The toast duration
  60. @param position The toast's center point. Can be one of the predefined CSToastPosition
  61. constants or a `CGPoint` wrapped in an `NSValue` object.
  62. @param style The style. The shared style will be used when nil
  63. */
  64. - (void)makeToast:(NSString *)message
  65. duration:(NSTimeInterval)duration
  66. position:(id)position
  67. style:(CSToastStyle *)style;
  68. /**
  69. Creates and presents a new toast view with a message, title, and image. Duration,
  70. position, and style can be set explicitly. The completion block executes when the
  71. toast view completes. `didTap` will be `YES` if the toast view was dismissed from
  72. a tap.
  73. @param message The message to be displayed
  74. @param duration The toast duration
  75. @param position The toast's center point. Can be one of the predefined CSToastPosition
  76. constants or a `CGPoint` wrapped in an `NSValue` object.
  77. @param title The title
  78. @param image The image
  79. @param style The style. The shared style will be used when nil
  80. @param completion The completion block, executed after the toast view disappears.
  81. didTap will be `YES` if the toast view was dismissed from a tap.
  82. */
  83. - (void)makeToast:(NSString *)message
  84. duration:(NSTimeInterval)duration
  85. position:(id)position
  86. title:(NSString *)title
  87. image:(UIImage *)image
  88. style:(CSToastStyle *)style
  89. completion:(void(^)(BOOL didTap))completion;
  90. /**
  91. Creates a new toast view with any combination of message, title, and image.
  92. The look and feel is configured via the style. Unlike the `makeToast:` methods,
  93. this method does not present the toast view automatically. One of the showToast:
  94. methods must be used to present the resulting view.
  95. @warning if message, title, and image are all nil, this method will return nil.
  96. @param message The message to be displayed
  97. @param title The title
  98. @param image The image
  99. @param style The style. The shared style will be used when nil
  100. @return The newly created toast view
  101. */
  102. - (UIView *)toastViewForMessage:(NSString *)message
  103. title:(NSString *)title
  104. image:(UIImage *)image
  105. style:(CSToastStyle *)style;
  106. /**
  107. Dismisses all active toast views. Any toast that is currently being displayed on the
  108. screen is considered active.
  109. @warning this does not clear toast views that are currently waiting in the queue. The next queued
  110. toast will appear immediately after `hideToasts` completes the dismissal animation.
  111. */
  112. - (void)hideToasts;
  113. /**
  114. Dismisses an active toast view.
  115. @param toast The active toast view to dismiss. Any toast that is currently being displayed
  116. on the screen is considered active.
  117. @warning this does not clear a toast view that is currently waiting in the queue.
  118. */
  119. - (void)hideToast:(UIView *)toast;
  120. /**
  121. Creates and displays a new toast activity indicator view at a specified position.
  122. @warning Only one toast activity indicator view can be presented per superview. Subsequent
  123. calls to `makeToastActivity:` will be ignored until hideToastActivity is called.
  124. @warning `makeToastActivity:` works independently of the showToast: methods. Toast activity
  125. views can be presented and dismissed while toast views are being displayed. `makeToastActivity:`
  126. has no effect on the queueing behavior of the showToast: methods.
  127. @param position The toast's center point. Can be one of the predefined CSToastPosition
  128. constants or a `CGPoint` wrapped in an `NSValue` object.
  129. */
  130. - (void)makeToastActivity:(id)position;
  131. /**
  132. Dismisses the active toast activity indicator view.
  133. */
  134. - (void)hideToastActivity;
  135. /**
  136. Displays any view as toast using the default duration and position.
  137. @param toast The view to be displayed as toast
  138. */
  139. - (void)showToast:(UIView *)toast;
  140. /**
  141. Displays any view as toast at a provided position and duration. The completion block
  142. executes when the toast view completes. `didTap` will be `YES` if the toast view was
  143. dismissed from a tap.
  144. @param toast The view to be displayed as toast
  145. @param duration The notification duration
  146. @param position The toast's center point. Can be one of the predefined CSToastPosition
  147. constants or a `CGPoint` wrapped in an `NSValue` object.
  148. @param completion The completion block, executed after the toast view disappears.
  149. didTap will be `YES` if the toast view was dismissed from a tap.
  150. */
  151. - (void)showToast:(UIView *)toast
  152. duration:(NSTimeInterval)duration
  153. position:(id)position
  154. completion:(void(^)(BOOL didTap))completion;
  155. @end
  156. /**
  157. `CSToastStyle` instances define the look and feel for toast views created via the
  158. `makeToast:` methods as well for toast views created directly with
  159. `toastViewForMessage:title:image:style:`.
  160. @warning `CSToastStyle` offers relatively simple styling options for the default
  161. toast view. If you require a toast view with more complex UI, it probably makes more
  162. sense to create your own custom UIView subclass and present it with the `showToast:`
  163. methods.
  164. */
  165. @interface CSToastStyle : NSObject
  166. /**
  167. The background color. Default is `[UIColor blackColor]` at 80% opacity.
  168. */
  169. @property (strong, nonatomic) UIColor *backgroundColor;
  170. /**
  171. The title color. Default is `[UIColor whiteColor]`.
  172. */
  173. @property (strong, nonatomic) UIColor *titleColor;
  174. /**
  175. The message color. Default is `[UIColor whiteColor]`.
  176. */
  177. @property (strong, nonatomic) UIColor *messageColor;
  178. /**
  179. A percentage value from 0.0 to 1.0, representing the maximum width of the toast
  180. view relative to it's superview. Default is 0.8 (80% of the superview's width).
  181. */
  182. @property (assign, nonatomic) CGFloat maxWidthPercentage;
  183. /**
  184. A percentage value from 0.0 to 1.0, representing the maximum height of the toast
  185. view relative to it's superview. Default is 0.8 (80% of the superview's height).
  186. */
  187. @property (assign, nonatomic) CGFloat maxHeightPercentage;
  188. /**
  189. The spacing from the horizontal edge of the toast view to the content. When an image
  190. is present, this is also used as the padding between the image and the text.
  191. Default is 10.0.
  192. */
  193. @property (assign, nonatomic) CGFloat horizontalPadding;
  194. /**
  195. The spacing from the vertical edge of the toast view to the content. When a title
  196. is present, this is also used as the padding between the title and the message.
  197. Default is 10.0.
  198. */
  199. @property (assign, nonatomic) CGFloat verticalPadding;
  200. /**
  201. The corner radius. Default is 10.0.
  202. */
  203. @property (assign, nonatomic) CGFloat cornerRadius;
  204. /**
  205. The title font. Default is `[UIFont boldSystemFontOfSize:16.0]`.
  206. */
  207. @property (strong, nonatomic) UIFont *titleFont;
  208. /**
  209. The message font. Default is `[UIFont systemFontOfSize:16.0]`.
  210. */
  211. @property (strong, nonatomic) UIFont *messageFont;
  212. /**
  213. The title text alignment. Default is `NSTextAlignmentLeft`.
  214. */
  215. @property (assign, nonatomic) NSTextAlignment titleAlignment;
  216. /**
  217. The message text alignment. Default is `NSTextAlignmentLeft`.
  218. */
  219. @property (assign, nonatomic) NSTextAlignment messageAlignment;
  220. /**
  221. The maximum number of lines for the title. The default is 0 (no limit).
  222. */
  223. @property (assign, nonatomic) NSInteger titleNumberOfLines;
  224. /**
  225. The maximum number of lines for the message. The default is 0 (no limit).
  226. */
  227. @property (assign, nonatomic) NSInteger messageNumberOfLines;
  228. /**
  229. Enable or disable a shadow on the toast view. Default is `NO`.
  230. */
  231. @property (assign, nonatomic) BOOL displayShadow;
  232. /**
  233. The shadow color. Default is `[UIColor blackColor]`.
  234. */
  235. @property (strong, nonatomic) UIColor *shadowColor;
  236. /**
  237. A value from 0.0 to 1.0, representing the opacity of the shadow.
  238. Default is 0.8 (80% opacity).
  239. */
  240. @property (assign, nonatomic) CGFloat shadowOpacity;
  241. /**
  242. The shadow radius. Default is 6.0.
  243. */
  244. @property (assign, nonatomic) CGFloat shadowRadius;
  245. /**
  246. The shadow offset. The default is `CGSizeMake(4.0, 4.0)`.
  247. */
  248. @property (assign, nonatomic) CGSize shadowOffset;
  249. /**
  250. The image size. The default is `CGSizeMake(80.0, 80.0)`.
  251. */
  252. @property (assign, nonatomic) CGSize imageSize;
  253. /**
  254. The size of the toast activity view when `makeToastActivity:` is called.
  255. Default is `CGSizeMake(100.0, 100.0)`.
  256. */
  257. @property (assign, nonatomic) CGSize activitySize;
  258. /**
  259. The fade in/out animation duration. Default is 0.2.
  260. */
  261. @property (assign, nonatomic) NSTimeInterval fadeDuration;
  262. /**
  263. Creates a new instance of `CSToastStyle` with all the default values set.
  264. */
  265. - (instancetype)initWithDefaultStyle NS_DESIGNATED_INITIALIZER;
  266. /**
  267. @warning Only the designated initializer should be used to create
  268. an instance of `CSToastStyle`.
  269. */
  270. - (instancetype)init NS_UNAVAILABLE;
  271. @end
  272. /**
  273. `CSToastManager` provides general configuration options for all toast
  274. notifications. Backed by a singleton instance.
  275. */
  276. @interface CSToastManager : NSObject
  277. /**
  278. Sets the shared style on the singleton. The shared style is used whenever
  279. a `makeToast:` method (or `toastViewForMessage:title:image:style:`) is called
  280. with with a nil style. By default, this is set to `CSToastStyle`'s default
  281. style.
  282. @param sharedStyle the shared style
  283. */
  284. + (void)setSharedStyle:(CSToastStyle *)sharedStyle;
  285. /**
  286. Gets the shared style from the singlton. By default, this is
  287. `CSToastStyle`'s default style.
  288. @return the shared style
  289. */
  290. + (CSToastStyle *)sharedStyle;
  291. /**
  292. Enables or disables tap to dismiss on toast views. Default is `YES`.
  293. @param tapToDismissEnabled YES or NO
  294. */
  295. + (void)setTapToDismissEnabled:(BOOL)tapToDismissEnabled;
  296. /**
  297. Returns `YES` if tap to dismiss is enabled, otherwise `NO`.
  298. Default is `YES`.
  299. @return BOOL YES or NO
  300. */
  301. + (BOOL)isTapToDismissEnabled;
  302. /**
  303. Enables or disables queueing behavior for toast views. When `YES`,
  304. toast views will appear one after the other. When `NO`, multiple Toast
  305. views will appear at the same time (potentially overlapping depending
  306. on their positions). This has no effect on the toast activity view,
  307. which operates independently of normal toast views. Default is `YES`.
  308. @param queueEnabled YES or NO
  309. */
  310. + (void)setQueueEnabled:(BOOL)queueEnabled;
  311. /**
  312. Returns `YES` if the queue is enabled, otherwise `NO`.
  313. Default is `YES`.
  314. @return BOOL
  315. */
  316. + (BOOL)isQueueEnabled;
  317. /**
  318. Sets the default duration. Used for the `makeToast:` and
  319. `showToast:` methods that don't require an explicit duration.
  320. Default is 3.0.
  321. @param duration The toast duration
  322. */
  323. + (void)setDefaultDuration:(NSTimeInterval)duration;
  324. /**
  325. Returns the default duration. Default is 3.0.
  326. @return duration The toast duration
  327. */
  328. + (NSTimeInterval)defaultDuration;
  329. /**
  330. Sets the default position. Used for the `makeToast:` and
  331. `showToast:` methods that don't require an explicit position.
  332. Default is `CSToastPositionBottom`.
  333. @param position The default center point. Can be one of the predefined
  334. CSToastPosition constants or a `CGPoint` wrapped in an `NSValue` object.
  335. */
  336. + (void)setDefaultPosition:(id)position;
  337. /**
  338. Returns the default toast position. Default is `CSToastPositionBottom`.
  339. @return position The default center point. Will be one of the predefined
  340. CSToastPosition constants or a `CGPoint` wrapped in an `NSValue` object.
  341. */
  342. + (id)defaultPosition;
  343. @end