问题
Is there any way to do this?
I tried putting the following into the subclass:
- (BOOL)canBecomeFirstResponder
{
return YES;
}
But when I sent the object a becomeFirstResponder message, it still didn't become the first responder.
回答1:
Yes, it is possible. You should:
Override
becomeFirstResponder
to returnYES
.Set
userInteractionEnabled
toYES
.Add a
UITapGestureRecognizer
to handle taps.Call
becomeFirstResponder
from the tap handler.
You can even override inputView
to get a input control at the bottom of the screen. Otherwise there will be nothing.
回答2:
I have no definite answer, just a few (maybe crazy) ideas: Have you also tried to override becomeFirstResponder
to return YES
? Does it have userInteractionEnabled
also set to YES
?
Otherwise, make it a custom UIButton instead of a custom UILabel...
回答3:
This worked for me.
Subclass UILabel and override these methods:
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action
withSender:(id)sender
{
return (action == @selector(copy:));
}
- (void)copy:(id)sender {
[[UIPasteboard generalPasteboard] setString:self.text];
}
Add Gesture to your label to detect tap.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressDetected:)];
[_messageLabel addGestureRecognizer:longPress];
Handle gesture event, present a UIMenuController with the desired frame:
- (void)LongPressDetected:(UILongPressGestureRecognizer*)gesture {
[_messageLabel becomeFirstResponder];
[[UIMenuController sharedMenuController] setTargetRect:_messageLabel.bounds inView:_messageLabel];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:NO];
}
来源:https://stackoverflow.com/questions/4089682/ios-is-it-possible-to-force-a-uilabel-subclass-object-to-become-the-first-res