YYTextCopyPasteExample.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "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 YYTextCopyPasteExample ()<YYTextViewDelegate>
  17. @property (nonatomic, assign) YYTextView *textView;
  18. @end
  19. @implementation YYTextCopyPasteExample
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.view.backgroundColor = [UIColor whiteColor];
  23. if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
  24. self.automaticallyAdjustsScrollViewInsets = NO;
  25. }
  26. 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.\n";
  27. YYTextSimpleMarkdownParser *parser = [YYTextSimpleMarkdownParser new];
  28. [parser setColorWithDarkTheme];
  29. YYTextView *textView = [YYTextView new];
  30. textView.text = text;
  31. textView.font = [UIFont systemFontOfSize:17];
  32. textView.size = self.view.size;
  33. textView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
  34. textView.delegate = self;
  35. textView.allowsPasteImage = YES; /// Pasts image
  36. textView.allowsPasteAttributedString = YES; /// Paste attributed string
  37. if (kiOS7Later) {
  38. textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
  39. }
  40. textView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
  41. textView.scrollIndicatorInsets = textView.contentInset;
  42. [self.view addSubview:textView];
  43. self.textView = textView;
  44. textView.selectedRange = NSMakeRange(text.length, 0);
  45. [textView becomeFirstResponder];
  46. }
  47. - (void)edit:(UIBarButtonItem *)item {
  48. if (_textView.isFirstResponder) {
  49. [_textView resignFirstResponder];
  50. } else {
  51. [_textView becomeFirstResponder];
  52. }
  53. }
  54. - (void)textViewDidBeginEditing:(YYTextView *)textView {
  55. UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  56. target:self
  57. action:@selector(edit:)];
  58. self.navigationItem.rightBarButtonItem = buttonItem;
  59. }
  60. - (void)textViewDidEndEditing:(YYTextView *)textView {
  61. self.navigationItem.rightBarButtonItem = nil;
  62. }
  63. @end