WBStatusComposeViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // WBStatusComposeViewController.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/9/8.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "WBStatusComposeViewController.h"
  9. #import "WBEmoticonInputView.h"
  10. #import "WBStatusComposeTextParser.h"
  11. #import "WBStatusHelper.h"
  12. #import "WBStatusLayout.h"
  13. #import "YYKit.h"
  14. #define kToolbarHeight (35 + 46)
  15. @interface WBStatusComposeViewController() <YYTextViewDelegate, YYTextKeyboardObserver, WBStatusComposeEmoticonViewDelegate>
  16. @property (nonatomic, strong) YYTextView *textView;
  17. @property (nonatomic, strong) UIView *toolbar;
  18. @property (nonatomic, strong) UIView *toolbarBackground;
  19. @property (nonatomic, strong) UIButton *toolbarPOIButton;
  20. @property (nonatomic, strong) UIButton *toolbarGroupButton;
  21. @property (nonatomic, strong) UIButton *toolbarPictureButton;
  22. @property (nonatomic, strong) UIButton *toolbarAtButton;
  23. @property (nonatomic, strong) UIButton *toolbarTopicButton;
  24. @property (nonatomic, strong) UIButton *toolbarEmoticonButton;
  25. @property (nonatomic, strong) UIButton *toolbarExtraButton;
  26. @property (nonatomic, assign) BOOL isInputEmoticon;
  27. @end
  28. @implementation WBStatusComposeViewController
  29. - (instancetype)init {
  30. self = [super init];
  31. [[YYTextKeyboardManager defaultManager] addObserver:self];
  32. return self;
  33. }
  34. - (void)dealloc {
  35. [[YYTextKeyboardManager defaultManager] removeObserver:self];
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. self.view.backgroundColor = [UIColor whiteColor];
  40. if ([self respondsToSelector:@selector( setAutomaticallyAdjustsScrollViewInsets:)]) {
  41. self.automaticallyAdjustsScrollViewInsets = NO;
  42. }
  43. [self _initNavBar];
  44. [self _initTextView];
  45. [self _initToolbar];
  46. [_textView becomeFirstResponder];
  47. }
  48. - (void)_initNavBar {
  49. UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(_cancel)];
  50. [button setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16],
  51. NSForegroundColorAttributeName : UIColorHex(4c4c4c)} forState:UIControlStateNormal];
  52. self.navigationItem.leftBarButtonItem = button;
  53. switch (_type) {
  54. case WBStatusComposeViewTypeStatus: {
  55. self.title = @"发微博";
  56. } break;
  57. case WBStatusComposeViewTypeRetweet: {
  58. self.title = @"转发微博";
  59. } break;
  60. case WBStatusComposeViewTypeComment: {
  61. self.title = @"发评论";
  62. } break;
  63. }
  64. }
  65. - (void)_initTextView {
  66. if (_textView) return;
  67. _textView = [YYTextView new];
  68. if (kSystemVersion < 7) _textView.top = -64;
  69. _textView.size = CGSizeMake(self.view.width, self.view.height);
  70. _textView.textContainerInset = UIEdgeInsetsMake(12, 16, 12, 16);
  71. _textView.contentInset = UIEdgeInsetsMake(64, 0, kToolbarHeight, 0);
  72. _textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  73. _textView.extraAccessoryViewHeight = kToolbarHeight;
  74. _textView.showsVerticalScrollIndicator = NO;
  75. _textView.alwaysBounceVertical = YES;
  76. _textView.allowsCopyAttributedString = NO;
  77. _textView.font = [UIFont systemFontOfSize:17];
  78. _textView.textParser = [WBStatusComposeTextParser new];
  79. _textView.delegate = self;
  80. _textView.inputAccessoryView = [UIView new];
  81. WBTextLinePositionModifier *modifier = [WBTextLinePositionModifier new];
  82. modifier.font = [UIFont fontWithName:@"Heiti SC" size:17];
  83. modifier.paddingTop = 12;
  84. modifier.paddingBottom = 12;
  85. modifier.lineHeightMultiple = 1.5;
  86. _textView.linePositionModifier = modifier;
  87. NSString *placeholderPlainText = nil;
  88. switch (_type) {
  89. case WBStatusComposeViewTypeStatus: {
  90. placeholderPlainText = @"分享新鲜事...";
  91. } break;
  92. case WBStatusComposeViewTypeRetweet: {
  93. placeholderPlainText = @"说说分享心得...";
  94. } break;
  95. case WBStatusComposeViewTypeComment: {
  96. placeholderPlainText = @"写评论...";
  97. } break;
  98. }
  99. if (placeholderPlainText) {
  100. NSMutableAttributedString *atr = [[NSMutableAttributedString alloc] initWithString:placeholderPlainText];
  101. atr.color = UIColorHex(b4b4b4);
  102. atr.font = [UIFont systemFontOfSize:17];
  103. _textView.placeholderAttributedText = atr;
  104. }
  105. [self.view addSubview:_textView];
  106. }
  107. - (void)_initToolbar {
  108. if (_toolbar) return;
  109. _toolbar = [UIView new];
  110. _toolbar.backgroundColor = [UIColor whiteColor];
  111. _toolbar.size = CGSizeMake(self.view.width, kToolbarHeight);
  112. _toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  113. _toolbarBackground = [UIView new];
  114. _toolbarBackground.backgroundColor = UIColorHex(F9F9F9);
  115. _toolbarBackground.size = CGSizeMake(_toolbar.width, 46);
  116. _toolbarBackground.bottom = _toolbar.height;
  117. _toolbarBackground.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
  118. [_toolbar addSubview:_toolbarBackground];
  119. _toolbarBackground.height = 300; // extend
  120. UIView *line = [UIView new];
  121. line.backgroundColor = UIColorHex(BFBFBF);
  122. line.width = _toolbarBackground.width;
  123. line.height = CGFloatFromPixel(1);
  124. line.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  125. [_toolbarBackground addSubview:line];
  126. _toolbarPOIButton = [UIButton buttonWithType:UIButtonTypeCustom];
  127. _toolbarPOIButton.size = CGSizeMake(88, 26);
  128. _toolbarPOIButton.centerY = 35 / 2.0;
  129. _toolbarPOIButton.left = 5;
  130. _toolbarPOIButton.clipsToBounds = YES;
  131. _toolbarPOIButton.layer.cornerRadius = _toolbarPOIButton.height / 2;
  132. _toolbarPOIButton.layer.borderColor = UIColorHex(e4e4e4).CGColor;
  133. _toolbarPOIButton.layer.borderWidth = CGFloatFromPixel(1);
  134. _toolbarPOIButton.titleLabel.font = [UIFont systemFontOfSize:14];
  135. _toolbarPOIButton.adjustsImageWhenHighlighted = NO;
  136. [_toolbarPOIButton setTitle:@"显示位置 " forState:UIControlStateNormal];
  137. [_toolbarPOIButton setTitleColor:UIColorHex(939393) forState:UIControlStateNormal];
  138. [_toolbarPOIButton setImage:[WBStatusHelper imageNamed:@"compose_locatebutton_ready"] forState:UIControlStateNormal];
  139. [_toolbarPOIButton setBackgroundImage:[UIImage imageWithColor:UIColorHex(f8f8f8)] forState:UIControlStateNormal];
  140. [_toolbarPOIButton setBackgroundImage:[UIImage imageWithColor:UIColorHex(e0e0e0)] forState:UIControlStateHighlighted];
  141. [_toolbar addSubview:_toolbarPOIButton];
  142. _toolbarGroupButton = [UIButton buttonWithType:UIButtonTypeCustom];
  143. _toolbarGroupButton.size = CGSizeMake(62, 26);
  144. _toolbarGroupButton.centerY = 35 / 2.0;
  145. _toolbarGroupButton.right = _toolbar.width - 5;
  146. _toolbarGroupButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  147. _toolbarGroupButton.clipsToBounds = YES;
  148. _toolbarGroupButton.layer.cornerRadius = _toolbarGroupButton.height / 2;
  149. _toolbarGroupButton.layer.borderColor = UIColorHex(e4e4e4).CGColor;
  150. _toolbarGroupButton.layer.borderWidth = CGFloatFromPixel(1);
  151. _toolbarGroupButton.titleLabel.font = [UIFont systemFontOfSize:14];
  152. _toolbarGroupButton.adjustsImageWhenHighlighted = NO;
  153. [_toolbarGroupButton setTitle:@"公开 " forState:UIControlStateNormal];
  154. [_toolbarGroupButton setTitleColor:UIColorHex(527ead) forState:UIControlStateNormal];
  155. [_toolbarGroupButton setImage:[WBStatusHelper imageNamed:@"compose_publicbutton"] forState:UIControlStateNormal];
  156. [_toolbarGroupButton setBackgroundImage:[UIImage imageWithColor:UIColorHex(f8f8f8)] forState:UIControlStateNormal];
  157. [_toolbarGroupButton setBackgroundImage:[UIImage imageWithColor:UIColorHex(e0e0e0)] forState:UIControlStateHighlighted];
  158. [_toolbar addSubview:_toolbarGroupButton];
  159. _toolbarPictureButton = [self _toolbarButtonWithImage:@"compose_toolbar_picture"
  160. highlight:@"compose_toolbar_picture_highlighted"];
  161. _toolbarAtButton = [self _toolbarButtonWithImage:@"compose_mentionbutton_background"
  162. highlight:@"compose_mentionbutton_background_highlighted"];
  163. _toolbarTopicButton = [self _toolbarButtonWithImage:@"compose_trendbutton_background"
  164. highlight:@"compose_trendbutton_background_highlighted"];
  165. _toolbarEmoticonButton = [self _toolbarButtonWithImage:@"compose_emoticonbutton_background"
  166. highlight:@"compose_emoticonbutton_background_highlighted"];
  167. _toolbarExtraButton = [self _toolbarButtonWithImage:@"message_add_background"
  168. highlight:@"message_add_background_highlighted"];
  169. CGFloat one = _toolbar.width / 5;
  170. _toolbarPictureButton.centerX = one * 0.5;
  171. _toolbarAtButton.centerX = one * 1.5;
  172. _toolbarTopicButton.centerX = one * 2.5;
  173. _toolbarEmoticonButton.centerX = one * 3.5;
  174. _toolbarExtraButton.centerX = one * 4.5;
  175. _toolbar.bottom = self.view.height;
  176. [self.view addSubview:_toolbar];
  177. }
  178. - (UIButton *)_toolbarButtonWithImage:(NSString *)imageName highlight:(NSString *)highlightImageName {
  179. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  180. button.exclusiveTouch = YES;
  181. button.size = CGSizeMake(46, 46);
  182. [button setImage:[WBStatusHelper imageNamed:imageName] forState:UIControlStateNormal];
  183. [button setImage:[WBStatusHelper imageNamed:highlightImageName] forState:UIControlStateHighlighted];
  184. button.centerY = 46 / 2;
  185. button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  186. [button addTarget:self action:@selector(_buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  187. [_toolbarBackground addSubview:button];
  188. return button;
  189. }
  190. - (void)_cancel {
  191. [self.view endEditing:YES];
  192. if (_dismiss) _dismiss();
  193. }
  194. - (void)_buttonClicked:(UIButton *)button {
  195. if (button == _toolbarPictureButton) {
  196. } else if (button == _toolbarAtButton) {
  197. NSArray *atArray = @[@"@姚晨 ", @"@陈坤 ", @"@赵薇 ", @"@Angelababy " , @"@TimCook ", @"@我的印象笔记 "];
  198. NSString *atString = [atArray randomObject];
  199. [_textView replaceRange:_textView.selectedTextRange withText:atString];
  200. } else if (button == _toolbarTopicButton) {
  201. NSArray *topic = @[@"#冰雪奇缘[电影]# ", @"#Let It Go[音乐]# ", @"#纸牌屋[图书]# ", @"#北京·理想国际大厦[地点]# " , @"#腾讯控股 kh00700[股票]# ", @"#WWDC# "];
  202. NSString *topicString = [topic randomObject];
  203. [_textView replaceRange:_textView.selectedTextRange withText:topicString];
  204. } else if (button == _toolbarEmoticonButton) {
  205. if (_textView.inputView) {
  206. _textView.inputView = nil;
  207. [_textView reloadInputViews];
  208. [_textView becomeFirstResponder];
  209. [_toolbarEmoticonButton setImage:[WBStatusHelper imageNamed:@"compose_emoticonbutton_background"] forState:UIControlStateNormal];
  210. [_toolbarEmoticonButton setImage:[WBStatusHelper imageNamed:@"compose_emoticonbutton_background_highlighted"] forState:UIControlStateHighlighted];
  211. } else {
  212. WBEmoticonInputView *v = [WBEmoticonInputView sharedView];
  213. v.delegate = self;
  214. _textView.inputView = v;
  215. [_textView reloadInputViews];
  216. [_textView becomeFirstResponder];
  217. [_toolbarEmoticonButton setImage:[WBStatusHelper imageNamed:@"compose_keyboardbutton_background"] forState:UIControlStateNormal];
  218. [_toolbarEmoticonButton setImage:[WBStatusHelper imageNamed:@"compose_keyboardbutton_background_highlighted"] forState:UIControlStateHighlighted];
  219. }
  220. } else if (button == _toolbarExtraButton) {
  221. }
  222. }
  223. #pragma mark @protocol YYTextViewDelegate
  224. - (void)textViewDidChange:(YYTextView *)textView {
  225. }
  226. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  227. [self.view endEditing:YES];
  228. }
  229. #pragma mark @protocol YYTextKeyboardObserver
  230. - (void)keyboardChangedWithTransition:(YYTextKeyboardTransition)transition {
  231. CGRect toFrame = [[YYTextKeyboardManager defaultManager] convertRect:transition.toFrame toView:self.view];
  232. if (transition.animationDuration == 0) {
  233. _toolbar.bottom = CGRectGetMinY(toFrame);
  234. } else {
  235. [UIView animateWithDuration:transition.animationDuration delay:0 options:transition.animationOption | UIViewAnimationOptionBeginFromCurrentState animations:^{
  236. _toolbar.bottom = CGRectGetMinY(toFrame);
  237. } completion:NULL];
  238. }
  239. }
  240. #pragma mark @protocol WBStatusComposeEmoticonView
  241. - (void)emoticonInputDidTapText:(NSString *)text {
  242. if (text.length) {
  243. [_textView replaceRange:_textView.selectedTextRange withText:text];
  244. }
  245. }
  246. - (void)emoticonInputDidTapBackspace {
  247. [_textView deleteBackward];
  248. }
  249. @end