2010-11-03 19 views
40

कॉपी करने के लिए टेक्स्ट का चयन करने दें, मेरे पास UILabel है, लेकिन मैं उपयोगकर्ता को इसके टेक्स्ट का एक हिस्सा चुनने की अनुमति कैसे दे सकता हूं। मैं नहीं चाहता कि उपयोगकर्ता टेक्स्ट को संपादित करने में सक्षम हो और न ही लेबल/टेक्स्टफील्ड को सीमा हो।उपयोगकर्ता को UILabel से

+0

उपयोग https:

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]; 

आगे इशारों संभाल यहाँ नमूना कोड का एक सा पाने के लिए आप शुरू कर दिया है: //github.com/hoteltonight/HTCopyableLabel –

उत्तर

47

UILabel के साथ यह संभव नहीं है।

इसके लिए आपको UITextField का उपयोग करना चाहिए। बस textFieldShouldBeginEditing प्रतिनिधि विधि का उपयोग करके संपादन अक्षम करें।

+0

लेकिन इसमें 3 डी सीमा नहीं होगी? –

+1

मैंने कुछ सप्ताह पहले UITextField का उपयोग किया था और मुझे याद है कि कोई सीमा नहीं थी (यह xib में बनाई गई थी)। यदि आपकी यूआईटीएक्स्टफिल्ड की सीमा है, तो सीमा को अक्षम करने के तरीके को जानने के लिए बस दस्तावेज़ पढ़ें। – Yuras

+3

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html [टेक्स्टफ़ील्ड सेटबॉर्डर स्टाइल: UITextBorderStyleNone] – Yuras

21

आप यूआईटीएक्स्टव्यू बनाने का उपयोग करते हैं और इसके .editable को NO में बनाते हैं। फिर आपके पास एक टेक्स्ट व्यू है जो (1) उपयोगकर्ता संपादित नहीं कर सकता (2) कोई सीमा नहीं है और (3) उपयोगकर्ता इससे टेक्स्ट का चयन कर सकता है।

19

कॉपी और पेस्ट का एक गरीब व्यक्ति का संस्करण, यदि आप नहीं कर सकते हैं, या टेक्स्ट व्यू का उपयोग करने की आवश्यकता नहीं है, तो लेबल में एक इशारा पहचानकर्ता जोड़ना होगा और फिर पूरे टेक्स्ट को पेस्टबोर्ड पर कॉपी करना होगा। जब तक आप UITextView

सुनिश्चित करें कि आप उपयोगकर्ता को यह जानते हैं कि इसकी प्रतिलिपि बनाई गई है और आप एक ही टैप इशारा के साथ-साथ एक लंबी प्रेस दोनों का समर्थन करते हैं, क्योंकि यह उपयोगकर्ताओं को कोशिश करने वाले उपयोगकर्ताओं को उठाएगा पाठ के एक हिस्से को हाइलाइट करने के लिए।

रजिस्टर अपने लेबल पर इशारा recognizers जब आप इसे बनाने के लिए::

- (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

अच्छा जवाब, लेकिन आपको कोड की एक पंक्ति जोड़नी चाहिए: myLabel.userInteractionEnabled = हाँ; – Ilario