YYTextEmoticonExample.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "YYKit.h"
  10. @interface YYTextEmoticonExample () <YYTextViewDelegate>
  11. @property (nonatomic, strong) YYTextView *textView;
  12. @end
  13. @implementation YYTextEmoticonExample
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. self.view.backgroundColor = [UIColor whiteColor];
  17. if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
  18. self.automaticallyAdjustsScrollViewInsets = NO;
  19. }
  20. NSMutableDictionary *mapper = [NSMutableDictionary new];
  21. mapper[@":smile:"] = [self imageWithName:@"002"];
  22. mapper[@":cool:"] = [self imageWithName:@"013"];
  23. mapper[@":biggrin:"] = [self imageWithName:@"047"];
  24. mapper[@":arrow:"] = [self imageWithName:@"007"];
  25. mapper[@":confused:"] = [self imageWithName:@"041"];
  26. mapper[@":cry:"] = [self imageWithName:@"010"];
  27. mapper[@":wink:"] = [self imageWithName:@"085"];
  28. YYTextSimpleEmoticonParser *parser = [YYTextSimpleEmoticonParser new];
  29. parser.emoticonMapper = mapper;
  30. YYTextLinePositionSimpleModifier *mod = [YYTextLinePositionSimpleModifier new];
  31. mod.fixedLineHeight = 22;
  32. YYTextView *textView = [YYTextView new];
  33. 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\nSee \'YYTextEmoticonExample.m\' for example.";
  34. textView.font = [UIFont systemFontOfSize:17];
  35. textView.textParser = parser;
  36. textView.size = self.view.size;
  37. textView.linePositionModifier = mod;
  38. textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
  39. textView.delegate = self;
  40. if (kiOS7Later) {
  41. textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
  42. }
  43. textView.contentInset = UIEdgeInsetsMake((kiOS7Later ? 64 : 0), 0, 0, 0);
  44. textView.scrollIndicatorInsets = textView.contentInset;
  45. [self.view addSubview:textView];
  46. self.textView = textView;
  47. [self.textView becomeFirstResponder];
  48. }
  49. - (UIImage *)imageWithName:(NSString *)name {
  50. NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"EmoticonQQ" ofType:@"bundle"]];
  51. NSString *path = [bundle pathForScaledResource:name ofType:@"gif"];
  52. NSData *data = [NSData dataWithContentsOfFile:path];
  53. YYImage *image = [YYImage imageWithData:data scale:2];
  54. image.preloadAllAnimatedImageFrames = YES;
  55. return image;
  56. }
  57. - (void)edit:(UIBarButtonItem *)item {
  58. if (_textView.isFirstResponder) {
  59. [_textView resignFirstResponder];
  60. } else {
  61. [_textView becomeFirstResponder];
  62. }
  63. }
  64. - (void)textViewDidBeginEditing:(YYTextView *)textView {
  65. UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  66. target:self
  67. action:@selector(edit:)];
  68. self.navigationItem.rightBarButtonItem = buttonItem;
  69. }
  70. - (void)textViewDidEndEditing:(YYTextView *)textView {
  71. self.navigationItem.rightBarButtonItem = nil;
  72. }
  73. @end