KKTextTool.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // KKTextTool.m
  3. // WWImageEdit
  4. //
  5. // Created by 邬维 on 2017/1/18.
  6. // Copyright © 2017年 kook. All rights reserved.
  7. //
  8. #import "KKTextTool.h"
  9. #import "KKTextView.h"
  10. #define COLOR_W 26
  11. #define COLOR_INSET 20
  12. @interface KKTextTool()<UITextViewDelegate>
  13. @property (nonatomic, strong) UIView *colorView; //颜色选择view
  14. @property (nonatomic, strong) UIButton *lastButton;
  15. @property (nonatomic, strong) UIColor *textColor;
  16. @property (nonatomic, strong) NSArray *colors;
  17. @property (nonatomic, assign) CGFloat keyBoardH;
  18. @end
  19. @implementation KKTextTool{
  20. UIImage *_originalImage; //原始图片
  21. UIView *_workingView; //上层工作区
  22. UIView *_textMenuView; //底部工具
  23. UITextView *_textEditView; //文字编辑view
  24. }
  25. #pragma -mark KKImageToolProtocol
  26. + (UIImage*)defaultIconImage{
  27. return [UIImage imageNamed:@"ToolText"];
  28. }
  29. + (NSString*)defaultTitle{
  30. return Localized(@"JX_ImageEditText");
  31. }
  32. + (NSUInteger)orderNum{
  33. return KKToolIndexNumberThird;
  34. }
  35. #pragma mark- implementation
  36. - (void)setup {
  37. _colors = @[[UIColor whiteColor],[UIColor blackColor],[UIColor redColor],[UIColor yellowColor],[UIColor greenColor],[UIColor blueColor],[UIColor purpleColor]];
  38. self.textColor = [_colors firstObject];
  39. _originalImage = self.editor.imageView.image;
  40. [self.editor fixZoomScaleWithAnimated:YES];
  41. _textMenuView = [[UIView alloc] initWithFrame:self.editor.menuView.frame];
  42. _textMenuView.backgroundColor = self.editor.menuView.backgroundColor;
  43. [self.editor.view addSubview:_textMenuView];
  44. _workingView = [[UIView alloc] initWithFrame:[self.editor.view convertRect:self.editor.imageView.frame fromView:self.editor.imageView.superview]];
  45. _workingView.clipsToBounds = YES;
  46. [self.editor.view addSubview:_workingView];
  47. self.selectedTextView = nil;
  48. [self setMenu];
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText) name:kTextViewActiveViewDidTapNotification object:nil];
  50. _textMenuView.transform = CGAffineTransformMakeTranslation(0, self.editor.view.height-_textMenuView.top);
  51. [UIView animateWithDuration:kImageToolAnimationDuration
  52. animations:^{
  53. _textMenuView.transform = CGAffineTransformIdentity;
  54. }];
  55. //监听当键盘将要出现时
  56. [[NSNotificationCenter defaultCenter] addObserver:self
  57. selector:@selector(keyboardWillShow:)
  58. name:UIKeyboardWillShowNotification
  59. object:nil];
  60. //监听当键将要退出时
  61. [[NSNotificationCenter defaultCenter] addObserver:self
  62. selector:@selector(keyboardWillHide:)
  63. name:UIKeyboardWillHideNotification
  64. object:nil];
  65. [self performSelector:@selector(addNewText) withObject:nil afterDelay:.1f];
  66. }
  67. - (void)updateTextColor:(UIButton *)button {
  68. if (button.tag == self.lastButton.tag) {
  69. return;
  70. }else {
  71. //还原上个按钮
  72. self.lastButton.frame = CGRectMake(self.lastButton.frame.origin.x+4, 0, COLOR_W, COLOR_W);
  73. self.lastButton.layer.cornerRadius = button.frame.size.width/2;
  74. self.lastButton.layer.borderWidth = 2.f;
  75. button.frame = CGRectMake(button.frame.origin.x-4, -4, COLOR_W+8, COLOR_W+8);
  76. button.layer.cornerRadius = button.frame.size.width/2;
  77. button.layer.borderWidth = 3.f;
  78. self.textColor = self.colors[button.tag];
  79. //记录一下当前点击按钮
  80. self.lastButton = button;
  81. }
  82. }
  83. - (void)cleanup
  84. {
  85. [self.editor resetZoomScaleWithAnimated:YES];
  86. [[NSNotificationCenter defaultCenter] removeObserver:self];
  87. [_workingView removeFromSuperview];
  88. [_textEditView removeFromSuperview];
  89. [_colorView removeFromSuperview];
  90. [UIView animateWithDuration:kImageToolAnimationDuration
  91. animations:^{
  92. _textMenuView.transform = CGAffineTransformMakeTranslation(0, self.editor.view.height-_textMenuView.top);
  93. }
  94. completion:^(BOOL finished) {
  95. [_textMenuView removeFromSuperview];
  96. }];
  97. }
  98. - (void)executeWithCompletionBlock:(void (^)(UIImage *, NSError *, NSDictionary *))completionBlock
  99. {
  100. [KKTextView setActiveTextView:nil];
  101. dispatch_async(dispatch_get_main_queue(), ^{
  102. UIImage *image = [self buildImage:_originalImage];
  103. completionBlock(image, nil, nil);
  104. });
  105. }
  106. #pragma mark- click Action
  107. - (void)addNewText
  108. {
  109. self.selectedTextView = nil;
  110. [self showTextEditView:@""];
  111. [self setNavigationItem:YES];
  112. [self createColorButtonWithArray:self.colors];
  113. }
  114. - (void)changeTextColor{
  115. }
  116. - (void)changeText{
  117. [self showTextEditView:[_selectedTextView getLableText]];
  118. [self setNavigationItem:YES];
  119. }
  120. - (void)textSaveBtn{
  121. [_textEditView resignFirstResponder];
  122. [self setNavigationItem:NO];
  123. //修改还是添加
  124. if (self.selectedTextView) {
  125. [_selectedTextView setLableText:_textEditView.text];
  126. }else{
  127. if ([_textEditView.text isEqualToString:@""]) {
  128. return;
  129. }
  130. KKTextView *view = [[KKTextView alloc] initWithTool:self];
  131. view.textColor = self.textColor;
  132. view.center = CGPointMake(_workingView.width/2, _workingView.height/2);
  133. [view setLableText:_textEditView.text];
  134. [_workingView addSubview:view];
  135. [KKTextView setActiveTextView:view];
  136. }
  137. }
  138. - (void)textCancelBtn{
  139. [self setNavigationItem:NO];
  140. }
  141. #pragma mark-
  142. - (UIImage*)buildImage:(UIImage*)image
  143. {
  144. UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
  145. [image drawAtPoint:CGPointZero];
  146. CGFloat scale = image.size.width / _workingView.width;
  147. CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
  148. [_workingView.layer renderInContext:UIGraphicsGetCurrentContext()];
  149. UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();
  150. UIGraphicsEndImageContext();
  151. return tmp;
  152. }
  153. - (void)setMenu{
  154. // UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 80, 40)];
  155. //// btn.backgroundColor = [UIColor redColor];
  156. // [btn setTitle:@"添加文字" forState:UIControlStateNormal];
  157. // btn.titleLabel.textColor = [UIColor whiteColor];
  158. // btn.titleLabel.font = [UIFont systemFontOfSize:15];
  159. // [btn addTarget:self action:@selector(addNewText) forControlEvents:UIControlEventTouchUpInside];
  160. // [_textMenuView addSubview:btn];
  161. // UIButton *btnColor = [[UIButton alloc] initWithFrame:CGRectMake(100, 20, 40, 40)];
  162. // btnColor.backgroundColor = [UIColor redColor];
  163. // [btnColor setTitle:@"颜色" forState:UIControlStateNormal];
  164. // btnColor.titleLabel.textColor = [UIColor whiteColor];
  165. // btnColor.titleLabel.font = [UIFont systemFontOfSize:15];
  166. // [btnColor addTarget:self action:@selector(changeTextColor) forControlEvents:UIControlEventTouchUpInside];
  167. // [_textMenuView addSubview:btnColor];
  168. }
  169. - (void)setNavigationItem:(BOOL)isEdit{
  170. if(isEdit){
  171. UINavigationItem *item = self.editor.navigationItem;
  172. item.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:Localized(@"JX_Finish") style:UIBarButtonItemStyleDone target:self action:@selector(textSaveBtn)];
  173. item.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:Localized(@"JX_Cencal") style:UIBarButtonItemStylePlain target:self action:@selector(textCancelBtn)];
  174. }else{
  175. UINavigationItem *item = self.editor.navigationItem;
  176. item.rightBarButtonItem = [[UIBarButtonItem alloc] init];
  177. //修改UINavigationItem
  178. NSNotification *n = [NSNotification notificationWithName:KTextEditDoneNotification object:self userInfo:nil];
  179. [[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:n waitUntilDone:NO];
  180. [UIView animateWithDuration:kImageToolAnimationDuration
  181. animations:^{
  182. _textEditView.transform = CGAffineTransformMakeTranslation(0, 600);
  183. }
  184. completion:^(BOOL finished) {
  185. [_textEditView removeFromSuperview];
  186. [_colorView removeFromSuperview];
  187. }];
  188. }
  189. }
  190. //文字编辑view
  191. - (void)showTextEditView:(NSString *)text{
  192. if (!_textEditView) {
  193. _textEditView = [[UITextView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP, [UIScreen mainScreen].bounds.size.width,800)];
  194. UIColor *textViewBgColor = [UIColor blackColor];
  195. _textEditView.backgroundColor = [textViewBgColor colorWithAlphaComponent:0.85];
  196. [_textEditView setTextColor:[UIColor whiteColor]];
  197. [_textEditView setFont:[UIFont systemFontOfSize:30]];
  198. [_textEditView setReturnKeyType:UIReturnKeyDone];
  199. _textEditView.delegate = self;
  200. }
  201. [_textEditView setText:text];
  202. [self.editor.view addSubview:_textEditView];
  203. _textEditView.transform = CGAffineTransformMakeTranslation(0, 600);
  204. [UIView animateWithDuration:kImageToolAnimationDuration
  205. animations:^{
  206. _textEditView.transform = CGAffineTransformIdentity;
  207. }];
  208. [_textEditView becomeFirstResponder];
  209. }
  210. #pragma mark- UITextViewDelegate
  211. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  212. {
  213. if ([text isEqualToString:@"\n"]) {
  214. [self textSaveBtn];
  215. return NO;
  216. }
  217. return YES;
  218. }
  219. - (void)createColorButtonWithArray:(NSArray *)colorArr {
  220. self.colorView = [[UIView alloc] init];
  221. self.colorView.frame = CGRectMake(self.colorView.frame.origin.x, JX_SCREEN_HEIGHT-JX_SCREEN_TOP-self.keyBoardH-44, JX_SCREEN_WIDTH, 44);
  222. for (int i = 0; i < colorArr.count; i ++) {
  223. UIButton *btn = [[UIButton alloc] init];
  224. int X = ((i+1)*COLOR_W)+(i*COLOR_INSET);
  225. btn.frame = CGRectMake(X, 0, COLOR_W, COLOR_W);
  226. btn.tag = i;
  227. btn.layer.masksToBounds = YES;
  228. btn.layer.cornerRadius = btn.frame.size.width/2;
  229. btn.layer.borderColor = [[UIColor whiteColor] CGColor];
  230. btn.layer.borderWidth = 2.f;
  231. btn.backgroundColor = colorArr[i];
  232. [btn addTarget:self action:@selector(updateTextColor:) forControlEvents:UIControlEventTouchUpInside];
  233. if (i <= 0) {
  234. btn.frame = CGRectMake(X-4, -4, COLOR_W+8, COLOR_W+8);
  235. btn.layer.cornerRadius = btn.frame.size.width/2;
  236. btn.layer.borderWidth = 3.f;
  237. self.lastButton = btn;
  238. }
  239. [self.colorView addSubview:btn];
  240. }
  241. [_textEditView addSubview:_colorView];
  242. }
  243. //当键盘出现
  244. - (void)keyboardWillShow:(NSNotification *)notification {
  245. //获取键盘的高度
  246. NSDictionary *userInfo = [notification userInfo];
  247. NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  248. CGRect keyboardRect = [value CGRectValue];
  249. self.keyBoardH = keyboardRect.size.height;
  250. self.colorView.frame = CGRectMake(self.colorView.frame.origin.x, JX_SCREEN_HEIGHT-JX_SCREEN_TOP-self.keyBoardH-44, JX_SCREEN_WIDTH, 44);
  251. }
  252. //当键退出
  253. - (void)keyboardWillHide:(NSNotification *)notification {
  254. //获取键盘的高度
  255. NSDictionary *userInfo = [notification userInfo];
  256. NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  257. CGRect keyboardRect = [value CGRectValue];
  258. self.keyBoardH = keyboardRect.size.height;
  259. self.colorView.frame = CGRectMake(self.colorView.frame.origin.x, JX_SCREEN_HEIGHT-JX_SCREEN_TOP-self.keyBoardH-44, JX_SCREEN_WIDTH, 44);
  260. }
  261. @end