You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by ac...@apache.org on 2018/03/06 04:11:00 UTC

incubator-weex git commit: * [WEEX-219][iOS] support copy action for text component

Repository: incubator-weex
Updated Branches:
  refs/heads/master 895611093 -> c65acbd84


* [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


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/c65acbd8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/c65acbd8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/c65acbd8

Branch: refs/heads/master
Commit: c65acbd84086d83822818ada1ecba04082672d49
Parents: 8956110
Author: acton393 <zh...@gmail.com>
Authored: Tue Jan 30 17:25:35 2018 +0800
Committer: acton393 <zh...@gmail.com>
Committed: Tue Mar 6 12:10:00 2018 +0800

----------------------------------------------------------------------
 .../WeexSDK/Sources/Component/WXTextComponent.m | 37 ++++++++++++++++++++
 1 file changed, 37 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c65acbd8/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
index 3dcc827..042f0a6 100644
--- a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
+++ b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
@@ -53,6 +53,11 @@
     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 @@
     }
 }
 
+- (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 @@ CGFloat WXTextDefaultLineThroughWidth = 1.2;
     pthread_mutex_t _ctAttributedStringMutex;
     pthread_mutexattr_t _propertMutexAttr;
     BOOL _observerIconfont;
+    BOOL _enableCopy;
 }
 
 + (void)setRenderUsingCoreText:(BOOL)usingCoreText
@@ -277,6 +296,9 @@ do {\
         [self setNeedsRepaint];
         [self setNeedsLayout];
     }
+    if (attributes[@"enableCopy"]) {
+        _enableCopy = [WXConvert BOOL:attributes[@"enableCopy"]];
+    }
 }
 
 - (void)setNeedsRepaint
@@ -306,11 +328,26 @@ do {\
     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];