iOS - Copying a UITextView - ios

In my app users can send messages to each other. I use UITextView inside of a bubble image to display the chat history.
[messageTextView setFrame:CGRectMake(padding, padding+5, size.width, size.height+padding)];
[messageTextView sizeToFit];
messageTextView.backgroundColor=[UIColor clearColor];
UIImage *img = [UIImage imageNamed:#"whiteBubble"];
UIImageView *bubbleImage=[[UIImageView alloc] initWithImage:[img stretchableImageWithLeftCapWidth:24 topCapHeight:15]];
messageTextView.editable=NO;
[bubbleImage setFrame:CGRectMake(padding/2, padding+5,
messageTextView.frame.size.width+padding/2, messageTextView.frame.size.height+5)];
[cell.contentView addSubview:bubbleImage];
[cell.contentView addSubview:messageTextView];
Currently, when a user holds down on the message text, they see the 'Copy' and 'Define' options with cursors to select text.
However, I would rather have the basic iOS messaging option of holding down on a chat bubble to copy the entire message. How can this be achieved?

I would subclass UITextView to implement your own version of the copy menu. You can do it a number of ways, but one possible way is like below.
The basic idea is that the text view sets up a UILongPressGestureRecognizer that will create the popup menu when a long press is detected.
UILongPressGestureRecognizer has several default system menus that will show up unless you tell them not to. The way to do that is to return NO for any selectors that you don't want to handle in canPerformAction:withSender:. In this case, we're returning NO for any selector except for our custom copyText: selector.
Then that selector just gets a reference to the general UIPasteboard and sets it's text to the text of the TextView.
In your subclass's implementation:
#implementation CopyTextView
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (instancetype)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
self.editable = NO;
self.selectable = NO;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressDetected:)];
longPress.minimumPressDuration = 0.3f; // however long, in seconds, you want the user to have to press before the menu shows up
[self addGestureRecognizer:longPress];
}
- (void)longPressDetected:(id)sender {
[self becomeFirstResponder];
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
if (longPress.state == UIGestureRecognizerStateEnded) {
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:#"Copy" action:#selector(copyText:)];
UIMenuController *menuCont = [UIMenuController sharedMenuController];
[menuCont setTargetRect:self.frame inView:self.superview];
menuCont.arrowDirection = UIMenuControllerArrowDown;
menuCont.menuItems = [NSArray arrayWithObject:menuItem];
[menuCont setMenuVisible:YES animated:YES];
}
}
- (BOOL)canBecomeFirstResponder { return YES; }
- (void)copyText:(id)sender {
UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:self.text];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == #selector(copyText:)) return YES;
return NO;
}
#end
Useful documentation:
UILongPressGestureRecognizer Documentation
UIMenuController Documentation

Related

UILongPressGestureRecognizer not working after archiving application

When testing in iPad, I can successfully call the method attached to UILongPressGestureRecognizer in UILabel.
After I have archived the application for Enterprise deployment, UILongPressGestureRecognizer does not work anymore.
I have already added the following codes aside from manually ticking User Interaction Enabled:
In .h file:
#interface BGMSetMenuViewController : UIViewController <UIGestureRecognizerDelegate>
In .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
itemPriceLabel.userInteractionEnabled = YES;
itemNameLabel.userInteractionEnabled = YES;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressSetMenuPrice:)];
longPress.delegate = self;
[itemPriceLabel addGestureRecognizer:longPress];
}
#pragma mark - UILongPressGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (void)longPressSetMenuPrice:(UILongPressGestureRecognizer*)gesture
{
if (gesture.state == UIGestureRecognizerStateEnded) {
BGMPasscodeViewController *passcodeVC = [[BGMPasscodeViewController alloc] initWithNibName:#"BGMPasscodeViewController" bundle:nil];
self.providesPresentationContextTransitionStyle = YES;
self.definesPresentationContext = YES;
[passcodeVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:passcodeVC animated:YES completion:nil];
}
}
Anybody here who have the same experience?
Please note that this was working in Xcode 7 and iOS 9.
Now, I'm using Xcode 8 and testing in iOS 10.0.2 and it doesn't work anymore.
Try below code:set YES to label userIntractionEnabled also in storyboard
- (void)viewDidLoad {
[super viewDidLoad];
labelTouch.userInteractionEnabled = YES;
[self labelLongPressed:labelTouch];}
- (void)labelLongPressed:(UILabel *)label{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 2; //seconds
longPress.delegate = self;
[label addGestureRecognizer:longPress];}
-(void) handleLongPress : (UITapGestureRecognizer *)sender{
UIButton *button = (UIButton *)sender.view;
NSLog(#"longpress");}

How to show custom menu items from starting in iOS?

How to remove default menu items like copy, past, SelectAll when i select WebView content. How to put custom actions in middle of default menu items.Which items i'm putting these are showing in last, i want to my custom actions from starting.
I'm using below code in view didAppear method.
UIMenuItem *customMenuItem1=[[UIMenuItem alloc] initWithTitle:#"Highlight" action:#selector(customAction1:)];
UIMenuItem *customMenuItem2=[[UIMenuItem alloc] initWithTitle:#"UnHighlight" action:#selector(UnHighlighted:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:customMenuItem1,customMenuItem2,nil]];
[UIMenuController sharedMenuController].menuVisible=YES;
Please help me.
As answered here showing custom menu on selection in UIWebView in iphone
- (void)viewDidLoad {
[super viewDidLoad];
[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.google.com"]]];
self.webview.backgroundColor = [UIColor blueColor];
// Do any additional setup after loading the view.
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIMenuItem *customMenuItem1 = [[UIMenuItem alloc] initWithTitle:#"Custom 1" action:#selector(customAction1:)];
UIMenuItem *customMenuItem2 = [[UIMenuItem alloc] initWithTitle:#"Custom 2" action:#selector(customAction2:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:customMenuItem1, customMenuItem2, nil]];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[UIMenuController sharedMenuController] setMenuItems:nil];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (self.webview.superview != nil)// && ![urlTextField isFirstResponder])
{
if (action == #selector(customAction1:) || action == #selector(customAction2:))
{
return YES;
}
}
return [super canPerformAction:action withSender:sender];
}
-(void)customAction1:(UIMenuItem*)item
{}
-(void)customAction2:(UIMenuItem*)item
{}

Objective C Subclass UITextField to copy only

I'm trying to create a class that makes a read only text field that allows the user to copy its contents. Here is my code:
CopyOnly.h
#import <UIKit/UIKit.h>
#interface CopyOnly : UITextField
#end
CopyOnly.m
#import "CopyOnly.h"
#implementation CopyOnly
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self attachTapHandler];
}
return self;
}
- (void) attachTapHandler
{
[self setUserInteractionEnabled:YES];
UIGestureRecognizer *touchy = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[self addGestureRecognizer:touchy];
}
- (BOOL) canPerformAction: (SEL) action withSender: (id) sender
{
return (action == #selector(copy:));
}
- (void) handleTap: (UIGestureRecognizer*) recognizer
{
[self becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
}
- (void)copy:(id)sender
{
UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setString:self.text];
self.highlighted = NO;
[self resignFirstResponder];
}
- (BOOL) canBecomeFirstResponder
{
return YES;
}
#end
This works great, only the keyboard is coming up. I don't want any keyboard to come up.
I tried adding this to initWithFrame:
UIView* noKeyboard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
self.inputView = noKeyboard;
This does not give me the result I'm expecting. Anyone know how I can do this?
Extending on my comment. This is easily accomplished using a UITextView (not UITextField) with editable property set to NO.
UITextView* tf = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 200, 50)];
tf.editable = NO;
tf.text = #"Hey this is a test!";
[self.view addSubview:tf];
Adding this to -(BOOL)canBecomeFirtResponder seemed to do the trick. Hacky, but it works.
- (BOOL) canBecomeFirstResponder
{
UIView* noKeyboard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
self.inputView = noKeyboard;
return YES;
}
If you stuck with text field that doesn't allow editing try totally different approach. Follow instructions in this article http://nshipster.com/uimenucontroller/ to implement UILabel that supports copying.

Not getting button action/ Single tap on custom MKannotation call out view

I have created a custom annotation and a call out for my map view. I need to navigate to another view when the user clicks on call out view or he clicks to the button that added as sub view to the callout view. But both gesture recognizer and add target is not working for me in this case. The setSelected: method was invoked and the view get hidden when tap occurs in call out view.
#interface VBPunchCardAnnotation : MKAnnotationView{
UIView *calloutView;
}
- (id)initWithAnnotation:(id )annotation reuseIdentifier:(NSString *)reuseIdentifier deal:(id)punchdeal
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
calloutView = [[UIView alloc] init];
calloutView.hidden = YES;
infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[calloutView addSubview:infoButton];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(annotationTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;
[calloutView addGestureRecognizer:singleTap];
[infoButton addTarget:self action:#selector(annotationTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:calloutView];
return self;
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// show/hide callout and swap pin image
calloutView.hidden = !selected;
self.image = !selected ? normalPin : selectedPin;
// dispatch an event to alert app a pin has been selected
if(selected) [[NSNotificationCenter defaultCenter] postNotificationName:#"punchCardAnnotation" object:self];
}
-(void)annotationTapped:(id)sender{
[self.delegate punchCardAnnotationClickedForDeal:self.punchDeal];
}
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hitView = [super hitTest:point withEvent:event];
if ([hitView isKindOfClass:[UIButton class]]) {
}
}
Finally I got the answer. It;s here
Followed this tutorial. Really great solution.
https://github.com/nfarina/calloutview
Happy coding!!

IOS:Create subMenu using UIMenuItemController inside a UIWebView

I would like the behaviour is that:
When I click on my custom UIMenuItem, it would show another list of UIMenuItems to choose.
I have implemented in that way.
#implementation CustomUIWebView{
BOOL clickedShowSubMenu;
NSArray *mainMenuItems;
NSArray *subMenuItems;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
UIMenuController *mc = [UIMenuController sharedMenuController];
UIMenuItem *testSubMenu = [[UIMenuItem alloc] initWithTitle:#"TestSubMenu" action:#selector(testSubMenu:)];
mainMenuItems = [NSArray arrayWithObjects:testSubMenu, nil];
UIMenuItem *subItem1 = [[UIMenuItem alloc] initWithTitle:#"Item1" action:#selector(item1:)];
UIMenuItem *subItem2 = [[UIMenuItem alloc] initWithTitle:#"Item2" action:#selector(item2:)];
subMenuItems = [NSArray arrayWithObjects:subItem1, subItem2, nil];
[mc setMenuItems:mainMenuItems];
[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(onMenuItemDismissed:) name:UIMenuControllerDidHideMenuNotification object:nil];
clickedShowSubMenu = NO;
}
return self;
}
- (void)testSubMenu:(id)sender{
UIMenuController *mc = [UIMenuController sharedMenuController];
[mc setMenuItems:subMenuItems];
mc.menuVisible = YES; //from te doc, it say it would keep the menu not dismiss when this function end
clickedShowSubMenu = YES;
}
- (void)item1:(id)sender{
}
- (void)item2:(id)sender{
}
- (void)onMenuItemDismissed:(id)sender{
if(clickedShowSubMenu == YES){
UIMenuController *mc = [UIMenuController sharedMenuController];
[mc setMenuItems:mainMenuItems];
clickedShowSubMenu = NO;
}
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if(action == #selector(testSubMenu:))return YES;
else if(action == #selector(item1:))return YES;
else if(action == #selector(item2:))return YES;
return NO;
}
#end
It works when I select long touch some text and click the Test Sub Menu, it did appear the "subMenu": Item1 and Item2 on the same position that Test Sub Menu appear.
However, the problem is the second time and later. I select other text and the Test Sub Menu appear correctly, but when I click it ,the Item1 and Item2 is showed on the OLD position that it first appear. Their position is never changed. I do not know what cause this.
EDIT
Ok. By making use of How to get coordinates (CGRect) for the selected text in UIWebView? to get the rect boundary of the selection and say [mc setTargetRect:theRect inView:self]; before mc.menuVisible = YES;
It work and the sub menu show approximate same position of the main menu. Is it recommended? There is still little problem that when I highlight a whole paragraph, the main menu is show UNDER the paragraph, however, the sub menu show ABOVE the paragraph.

Resources