2015-12-19 7 views
5

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

var imageView = UIImageView() 
var image = UIImage(named: "all.png") 
imageView.image = image 
searchFiled.leftView = imageView 

उत्तर

0

फोकस करके, आप UITextField पर उपयोगकर्ता क्लिक करता है मतलब है, तो एक UITextFieldDelegate के रूप में अपने दृश्य नियंत्रक अधिनियम के लिए क्या आप चाहते हैं। आप वैकल्पिक func textFieldDidBeginEditing (_ textField: UITextField) फ़ंक्शन को कार्यान्वित करना चाहते हैं। उस फ़ंक्शन में आप छवि को छिपाना चाहते हैं।

3

दुर्भाग्य से मैं 'बात नहीं करते है स्विफ्ट, लेकिन यह ऑब्जेक्टिव-सी संस्करण का अनुवाद करने में आसानी से संभव हो जाना चाहिए ...

आपका तथाकथित' प्लेसहोल्डर 'के बारे में बात; स्ट्रिंग (या जिम्मेदार स्ट्रिंग), जो कि UITextField में दिखाया गया है, जब कोई वास्तव में पाठ डाला नहीं जाता है।

यहां एक छवि दिखाने के लिए निम्न कोड का उपयोग करें:

// Create a NSTextAttachment with your image 
    NSTextAttachment* placeholderImageTextAttachment = [[NSTextAttachment alloc] init]; 
    placeholderImageTextAttachment.image = [UIImage imageNamed:@"Your image name"]; 

    // Use 'bound' to adjust position and size 
    placeholderImageTextAttachment.bounds = CGRectMake(0, 0, 16, 16); 
    NSMutableAttributedString* placeholderImageString = [[NSAttributedString attributedStringWithAttachment:placeholderImageTextAttachment] mutableCopy]; 

    // Append the placeholder text 
    NSMutableAttributedString* placeholderString = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@"Search", nil)]; 
    [placeholderImageString appendAttributedString:placeholderString]; 

    // set as (attributed) placeholder 
    _yourTextField.attributedPlaceholder = placeholderImageString;