Hello!
I have UILabel and I need it to be able support copy&paste (actually only copy as it is read only). I subclassed UILabel to support copy and it works fine. I also added text highlight so user knows what exactly is he copying when he clicks on label.
My problem is that I don't know how to cancel that highlight when user clicks somewhere else. Copy bubble disappears, but I don't get any callback so text remains highlighted. Is there special callback I missed that I can use or do I have to come up with some dirty hack? Or is there perhaps more standard way of highlighting text in UILabel that I am not aware of that would be handled automatically like Copy bubble?
Here my custom UILabel code:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if(action == @selector(copy:)) {
return YES;
}
else {
return [super canPerformAction:action withSender:sender];
}
}
- (BOOL)becomeFirstResponder {
if([super becomeFirstResponder]) {
self.highlighted = YES;
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES animated:YES];
return YES;
}
return NO;
}
- (BOOL)resignFirstResponder {
if([super resignFirstResponder]) {
self.highlighted = NO;
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuVisible:NO animated:YES];
[menu update];
return true;
}
return false;
}
- (void)copy:(id)sender {
UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setString:self.text];
self.highlighted = NO;
[self resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if([self isFirstResponder]) {
//UIMenuController *menu = [UIMenuController sharedMenuController];
//[menu setMenuVisible:NO animated:YES];
//[menu update];
[self resignFirstResponder];
}
else if([self becomeFirstResponder]) {
//UIMenuController *menu = [UIMenuController sharedMenuController];
//[menu setTargetRect:self.bounds inView:self];
//[menu setMenuVisible:YES animated:YES];
}
}
- (void)setHighlighted:(BOOL)hl {
[super setHighlighted:hl];
[self setNeedsLayout];
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
if(self.highlighted) {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(ctx, 0.3, 0.8, 1.0, 0.3);
CGContextAddRect(ctx, CGRectMake(0, 0, [self textRectForBounds:self.frame limitedToNumberOfLines:1].size.width, self.frame.size.height));
CGContextFillPath(ctx);
}
}
Any help appreciated!