Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
* [WEEX-219][iOS] support copy action for text component
Browse files Browse the repository at this point in the history
[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
  • Loading branch information
acton393 committed Mar 6, 2018
1 parent 8956110 commit c65acbd
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -132,6 +150,7 @@ @implementation WXTextComponent
pthread_mutex_t _ctAttributedStringMutex;
pthread_mutexattr_t _propertMutexAttr;
BOOL _observerIconfont;
BOOL _enableCopy;
}

+ (void)setRenderUsingCoreText:(BOOL)usingCoreText
Expand Down Expand Up @@ -277,6 +296,9 @@ - (void)fillAttributes:(NSDictionary *)attributes
[self setNeedsRepaint];
[self setNeedsLayout];
}
if (attributes[@"enableCopy"]) {
_enableCopy = [WXConvert BOOL:attributes[@"enableCopy"]];
}
}

- (void)setNeedsRepaint
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit c65acbd

Please sign in to comment.