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

[WEEX-219][iOS]support copy action for text component #1030

Merged
merged 1 commit into from
Mar 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
Original file line number Diff line number Diff line change
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