Updated 13 January 2017
Suppose you want to copy the text of the label, by default, there is no functionality of copy the text of the label in iOS as provided in UItextField/ UItextView. so to copy the text of label you have to firstly make the tap gesture to it depend on you it works on long press/work on just tap that label.An example of copy the text of the label in iOS.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
UILabel *traking = [[UILabel alloc] initWithFrame:CGRectMake(8, y, SCREEN_WIDTH-4, 20)]; [traking setTextColor:[UIColor redColor]]; [traking setBackgroundColor:[UIColor clearColor]]; [traking setFont:[UIFont fontWithName: @"Trebuchet MS" size: 16.0f]]; NSString *trakingText = [NSString stringWithFormat:@"Tracking Number : %@",dict[@"shipping_number"][@"text"]]; [traking setText:trakingText]; traking.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textTapped:)]; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(textPressed:)]; [traking addGestureRecognizer:tap]; [traking addGestureRecognizer:longPress]; [myOrdersScrollView addSubview:traking]; - (void) textPressed:(UILongPressGestureRecognizer *) gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateRecognized && [gestureRecognizer.view isKindOfClass:[UILabel class]]) { UILabel *someLabel = (UILabel *)gestureRecognizer.view; UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; [pasteboard setString:someLabel.text]; //let the user know you copied the text to the pasteboard and they can no paste it somewhere else } } - (void) textTapped:(UITapGestureRecognizer *) gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateRecognized && [gestureRecognizer.view isKindOfClass:[UILabel class]]) { NSLog(@"fjeeddfj"); UILabel *someLabel = (UILabel *)gestureRecognizer.view; UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; [pasteboard setString:someLabel.text]; //let the user know you copied the text to the pasteboard and they can no paste it somewhere else } } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.