YYTextEmoticonExample.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // YYTextEmoticonExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/9/3.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYTextEmoticonExample.h"
  9. #import "YYText.h"
  10. #import "YYImage.h"
  11. #import "UIImage+YYWebImage.h"
  12. #import "UIView+YYAdd.h"
  13. #import "NSBundle+YYAdd.h"
  14. #import "NSString+YYAdd.h"
  15. #import "YYTextExampleHelper.h"
  16. @interface YYTextEmoticonExample () <YYTextViewDelegate>
  17. @property (nonatomic, strong) YYTextView *textView;
  18. @end
  19. @implementation YYTextEmoticonExample
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.view.backgroundColor = [UIColor whiteColor];
  23. if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
  24. self.automaticallyAdjustsScrollViewInsets = NO;
  25. }
  26. NSMutableDictionary *mapper = [NSMutableDictionary new];
  27. mapper[@":smile:"] = [self imageWithName:@"002"];
  28. mapper[@":cool:"] = [self imageWithName:@"013"];
  29. mapper[@":biggrin:"] = [self imageWithName:@"047"];
  30. mapper[@":arrow:"] = [self imageWithName:@"007"];
  31. mapper[@":confused:"] = [self imageWithName:@"041"];
  32. mapper[@":cry:"] = [self imageWithName:@"010"];
  33. mapper[@":wink:"] = [self imageWithName:@"085"];
  34. YYTextSimpleEmoticonParser *parser = [YYTextSimpleEmoticonParser new];
  35. parser.emoticonMapper = mapper;
  36. YYTextView *textView = [YYTextView new];
  37. textView.text = @"Hahahah:smile:, it\'s emoticons::cool::arrow::cry::wink:\n\nYou can input \":\" + \"smile\" + \":\" to display smile emoticon, or you can copy and paste these emoticons.\n";
  38. textView.font = [UIFont systemFontOfSize:17];
  39. textView.textParser = parser;
  40. textView.size = self.view.size;
  41. textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
  42. textView.delegate = self;
  43. if (kiOS7Later) {
  44. textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
  45. }
  46. textView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
  47. textView.scrollIndicatorInsets = textView.contentInset;
  48. [self.view addSubview:textView];
  49. self.textView = textView;
  50. [self.textView becomeFirstResponder];
  51. }
  52. - (UIImage *)imageWithName:(NSString *)name {
  53. NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"EmoticonQQ" ofType:@"bundle"]];
  54. NSString *path = [bundle pathForScaledResource:name ofType:@"gif"];
  55. NSData *data = [NSData dataWithContentsOfFile:path];
  56. YYImage *image = [YYImage imageWithData:data scale:2];
  57. image.preloadAllAnimatedImageFrames = YES;
  58. return image;
  59. }
  60. - (void)edit:(UIBarButtonItem *)item {
  61. if (_textView.isFirstResponder) {
  62. [_textView resignFirstResponder];
  63. } else {
  64. [_textView becomeFirstResponder];
  65. }
  66. }
  67. - (void)textViewDidBeginEditing:(YYTextView *)textView {
  68. UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  69. target:self
  70. action:@selector(edit:)];
  71. self.navigationItem.rightBarButtonItem = buttonItem;
  72. }
  73. - (void)textViewDidEndEditing:(YYTextView *)textView {
  74. self.navigationItem.rightBarButtonItem = nil;
  75. }
  76. @end