From c65acbd84086d83822818ada1ecba04082672d49 Mon Sep 17 00:00:00 2001 From: acton393 Date: Tue, 30 Jan 2018 17:25:35 +0800 Subject: [PATCH] * [WEEX-219][iOS] support copy action for text component [WEEX-219][iOS] support copy action for text component you can specify an attribute disableCopy='true' to enable the copy action by long press gesture, default is disable. feature:219 [WEEX-219][iOS] rename property --- .../Sources/Component/WXTextComponent.m | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m index 3dcc827d07..042f0a6272 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m +++ b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m @@ -53,6 +53,11 @@ + (Class)layerClass return [WXLayer class]; } +- (void)copy:(id)sender +{ + [[UIPasteboard generalPasteboard] setString:((WXTextComponent*)self.wx_component).text]; +} + - (void)setTextStorage:(NSTextStorage *)textStorage { if (_textStorage != textStorage) { @@ -61,6 +66,19 @@ - (void)setTextStorage:(NSTextStorage *)textStorage } } +- (BOOL)canBecomeFirstResponder +{ + return YES; +} + +- (BOOL)canPerformAction:(SEL)action withSender:(id)sender +{ + if (action == @selector(copy:)) { + return [[self.wx_component valueForKey:@"_enableCopy"] boolValue]; + } + return [super canPerformAction:action withSender:sender]; +} + - (NSString *)description { NSString *superDescription = super.description; @@ -132,6 +150,7 @@ @implementation WXTextComponent pthread_mutex_t _ctAttributedStringMutex; pthread_mutexattr_t _propertMutexAttr; BOOL _observerIconfont; + BOOL _enableCopy; } + (void)setRenderUsingCoreText:(BOOL)usingCoreText @@ -277,6 +296,9 @@ - (void)fillAttributes:(NSDictionary *)attributes [self setNeedsRepaint]; [self setNeedsLayout]; } + if (attributes[@"enableCopy"]) { + _enableCopy = [WXConvert BOOL:attributes[@"enableCopy"]]; + } } - (void)setNeedsRepaint @@ -306,11 +328,26 @@ - (void)viewDidLoad if (!useCoreText) { ((WXTextView *)self.view).textStorage = _textStorage; } + if (_enableCopy) { + UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(displayMenuController:)]; + [self.view addGestureRecognizer:longPress]; + } self.view.isAccessibilityElement = YES; [self setNeedsDisplay]; } +- (void)displayMenuController:(id)sender +{ + if ([self.view becomeFirstResponder] && ((UILongPressGestureRecognizer*)sender).state == UIGestureRecognizerStateBegan) { + UIMenuController *theMenu = [UIMenuController sharedMenuController]; + CGSize size = [self ctAttributedString].size; + CGRect selectionRect = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, size.width, size.height); + [theMenu setTargetRect:selectionRect inView:self.view.superview]; + [theMenu setMenuVisible:YES animated:YES]; + } +} + - (UIView *)loadView { return [[WXTextView alloc] init];