MessageBodyTextView.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "MessageBodyTextView.h"
  6. #import "NCRoomsManager.h"
  7. #import "NCUserDefaults.h"
  8. #import "NextcloudTalk-Swift.h"
  9. @implementation MessageBodyTextView
  10. - (instancetype)init
  11. {
  12. NSTextStorage *textStorage = [NSTextStorage new];
  13. NSLayoutManager *layoutManager = (NSLayoutManager *)[SwiftMarkdownObjCBridge getLayoutManager];
  14. [textStorage addLayoutManager: layoutManager];
  15. NSTextContainer *textContainer = [NSTextContainer new];
  16. [layoutManager addTextContainer: textContainer];
  17. self = [[MessageBodyTextView alloc] initWithFrame:CGRectZero textContainer:textContainer];
  18. if (!self) {
  19. return nil;
  20. }
  21. self.dataDetectorTypes = UIDataDetectorTypeAll;
  22. self.textContainer.lineFragmentPadding = 0;
  23. self.textContainerInset = UIEdgeInsetsZero;
  24. self.translatesAutoresizingMaskIntoConstraints = NO;
  25. // Set background color to clear to allow cell selection color to be visible
  26. self.backgroundColor = [UIColor clearColor];
  27. self.editable = NO;
  28. self.scrollEnabled = NO;
  29. self.delegate = self;
  30. return self;
  31. }
  32. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  33. {
  34. return NO;
  35. }
  36. // https://stackoverflow.com/a/44878203
  37. - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
  38. {
  39. UITextPosition *position = [self closestPositionToPoint:point];
  40. if (!position) {return NO;}
  41. UITextRange *range = [self.tokenizer rangeEnclosingPosition:position withGranularity:UITextGranularityCharacter inDirection:(UITextDirection)UITextLayoutDirectionLeft];
  42. if (!range) {return NO;}
  43. NSInteger startIndex = [self offsetFromPosition:self.beginningOfDocument toPosition:range.start];
  44. return [self.attributedText attribute:NSLinkAttributeName atIndex:startIndex effectiveRange:nil] != nil;
  45. }
  46. #pragma mark - UITextView delegate
  47. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction
  48. {
  49. if ([NCUtils isInstanceRoomLinkWithLink:URL.absoluteString]) {
  50. NSString *token = URL.lastPathComponent;
  51. [[NCRoomsManager sharedInstance] startChatWithRoomToken:token];
  52. return NO;
  53. }
  54. return YES;
  55. }
  56. - (void)textViewDidChangeSelection:(UITextView *)textView
  57. {
  58. if(!NSEqualRanges(textView.selectedRange, NSMakeRange(0, 0))) {
  59. textView.selectedRange = NSMakeRange(0, 0);
  60. }
  61. }
  62. @end