2010-11-03 7 views

Respuesta

47

No es posible con UILabel.

Debe usar UITextField para eso. Simplemente desactive la edición usando el método delegado textFieldShouldBeginEditing.

+0

Pero eso tendrá el borde 3D ¿no? –

+1

Utilicé UITextField hace unas semanas y recuerdo que no había borde (se creó en xib). Si su UITextField tiene un borde, simplemente lea la documentación para descubrir cómo deshabilitar el borde. – Yuras

+3

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html [textField setBorderStyle: UITextBorderStyleNone] – Yuras

21

Se usa crear una UITextView y hacer que sea .editable a NO. Luego tiene una vista de texto que (1) el usuario no puede editar (2) no tiene borde y (3) el usuario puede seleccionar texto de ella.

19

Una versión pobre de copiar y pegar, si no puede, o no necesita usar una vista de texto, sería agregar un reconocedor de gestos a la etiqueta y luego simplemente copiar el texto completo al portapapeles. No es posible hacer solo una parte a menos que use UITextView

Asegúrese de informar al usuario que ha sido copiado y que admite tanto un solo toque como una pulsación prolongada, ya que retomará a los usuarios que intentan para resaltar una porción de texto. Aquí es un poco de código de ejemplo para empezar:

Registrar los reconocedores gesto en su etiqueta cuando la crea:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textTapped:)]; 
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(textPressed:)]; 
       [myLabel addGestureRecognizer:tap]; 
       [myLabel addGestureRecognizer:longPress]; 
       [myLabel setUserInteractionEnabled:YES]; 

El siguiente manejar los gestos:

- (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]]) { 
      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 
      ... 
    } 
} 
+2

buena respuesta, pero debe agregar una línea de código: myLabel.userInteractionEnabled = YES; – Ilario

Cuestiones relacionadas