YYTextCopyPasteExample.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // YYTextCopyPasteExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/9/12.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYTextCopyPasteExample.h"
  9. #import "YYKit.h"
  10. @interface YYTextCopyPasteExample ()<YYTextViewDelegate>
  11. @property (nonatomic, assign) YYTextView *textView;
  12. @end
  13. @implementation YYTextCopyPasteExample
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. self.view.backgroundColor = [UIColor whiteColor];
  17. if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
  18. self.automaticallyAdjustsScrollViewInsets = NO;
  19. }
  20. NSString *text = @"You can copy image from browser or photo album and paste it to here. It support animated GIF and APNG. \n\nYou can also copy attributed string from other YYTextView.";
  21. YYTextSimpleMarkdownParser *parser = [YYTextSimpleMarkdownParser new];
  22. [parser setColorWithDarkTheme];
  23. YYTextView *textView = [YYTextView new];
  24. textView.text = text;
  25. textView.font = [UIFont systemFontOfSize:17];
  26. textView.size = self.view.size;
  27. textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
  28. textView.delegate = self;
  29. textView.allowsPasteImage = YES; /// Pasts image
  30. textView.allowsPasteAttributedString = YES; /// Paste attributed string
  31. if (kiOS7Later) {
  32. textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
  33. }
  34. textView.contentInset = UIEdgeInsetsMake((kiOS7Later ? 64 : 0), 0, 0, 0);
  35. textView.scrollIndicatorInsets = textView.contentInset;
  36. [self.view addSubview:textView];
  37. self.textView = textView;
  38. textView.selectedRange = NSMakeRange(text.length, 0);
  39. [textView becomeFirstResponder];
  40. }
  41. - (void)edit:(UIBarButtonItem *)item {
  42. if (_textView.isFirstResponder) {
  43. [_textView resignFirstResponder];
  44. } else {
  45. [_textView becomeFirstResponder];
  46. }
  47. }
  48. - (void)textViewDidBeginEditing:(YYTextView *)textView {
  49. UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  50. target:self
  51. action:@selector(edit:)];
  52. self.navigationItem.rightBarButtonItem = buttonItem;
  53. }
  54. - (void)textViewDidEndEditing:(YYTextView *)textView {
  55. self.navigationItem.rightBarButtonItem = nil;
  56. }
  57. @end