2009-07-12 22 views
28

मेरे पास UIButton है और मैं इस पर एक शीर्षक और एक छवि सेट करने की कोशिश कर रहा हूं।UIButton शीर्षक और छवि संरेखण क्वेरी

मैं शीर्षक को UIButton के बाईं ओर दाईं ओर संरेखित करना चाहता हूं और दाईं ओर एक छवि को गठबंधन करना चाहता हूं। मैं क्लॉक ऐप में टाइमर में बटन के दिखने और महसूस करने की कोशिश कर रहा हूं (वह जो "टाइमर एंड्स" कहता है)।

मैं अपने लक्ष्य को प्राप्त करने contentHorizontalAlignment, contentEdgeInsets, titleEdgeInsets और imageEdgeInsets साथ fiddled, लेकिन कोई लाभ नहीं हुआ। दस्तावेज भी इसके लिए काफी अस्पष्ट है।

मैं इसे कैसे प्राप्त कर सकता हूं?

इसके अलावा संबंधित प्रश्न, क्लॉक ऐप में टाइमर में टेक्स्ट का दो सेट है, एक बाईं ओर गठबंधन है और अन्य छवि के साथ गठबंधन है? UIButton में यह कैसे किया जा सकता है? (मुझे फिलहाल उस कार्यक्षमता की आवश्यकता नहीं है)।

उत्तर

35

बदलें UIButton और फिर आगे imageEdgeInsets संपत्ति सिर्फ का उपयोग setImage:forState:

+1

यदि आप UIButton का उपयोग कर रहे हैं, तो ऐसा करने का यह सही तरीका है। टेबल विचार एक अलग जानवर हैं। –

+5

सहमत हुए, लेकिन ओपी ने टेबल दृश्यों का जिक्र नहीं किया :) – Bill

9

याद रखें कि UIButton UIView से विरासत में है, और इसलिए आप किसी अन्य दृश्य की तरह इसे सबव्यूव जोड़ सकते हैं। अपनी स्थिति में, आप एक बटन बनाने हैं, तो बाईं ओर और सही पर एक UIImage पर एक UILabel जोड़ें:

// Create the button 
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

// Now load the image and create the image view 
UIImage *image = [UIImage imageNamed:@"yourImage.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(/*frame*/)]; 
[imageView setImage:image]; 

// Create the label and set its text 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(/*frame*/)]; 
[label setText:@"Your title"]; 

// Put it all together 
[button addSubview:label]; 
[button addSubview:imageView];
+0

यह बहुत अच्छी तरह से काम नहीं कर रहा है। मेरे पास बटन पर पृष्ठभूमि छवि है और यदि मैं पृष्ठभूमि छवि सेट करता हूं तो लेबल दिखाई नहीं देता है।अगर मैं बटन से पृष्ठभूमि छवि को हटा देता हूं, तो लेबल दिखाई देता है? मैंने UIButton के मूल दृश्य में योग्यता जोड़ा और UIButton के शीर्ष पर बैठने के लिए गठबंधन किया और यह तब काम करता है। कोई विचार यह इस तरह से क्यों व्यवहार कर रहा है? – siasl

+2

आमतौर पर उस प्रकार का व्यवहार स्वयं प्रस्तुत करता है जब पृष्ठभूमि छवि को आपके कस्टम दृश्यों के ऊपर UIButton के सबव्यूव के रूप में जोड़ा जाता है। बटन की पृष्ठभूमि इमेज प्रॉपर्टी सेट करने के बजाय, पृष्ठभूमि छवि के लिए UIImageView स्वयं को जोड़ने के बारे में सोचें, फिर उस छवि दृश्य के शीर्ष पर अन्य कस्टम सामग्री जोड़ना। – Tim

+0

अपने तर्क को रोल करने की कोई आवश्यकता नहीं - UIButton पर 'imageEdgeInsets' प्रॉपर्टी का उपयोग करें और 'setImage: forState:'। – Bill

47

यहाँ है कोड मैं उस के लिए उपयोग करें:

//Right-align the button image 
CGSize size = [[myButton titleForState:UIControlStateNormal] sizeWithFont:myButton.titleLabel.font]; 
[myButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -size.width)]; 
[myButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, myButton.imageView.image.size.width + 5)]; 
+0

यह कुछ हद तक बेहतर है, अगर कुछ हद तक संक्षिप्त है, तो कोड का उपयोग करके उत्तर दें, @ बिल द्वारा शीर्ष वोट वाले उत्तर के विरुद्ध, जो आपको इन एपीआई कॉलों का उपयोग करने के लिए कहता है लेकिन ' टी उन्हें बुलाओ दिखाओ। यह बेहतर होगा अगर यह सेट इमेज को कॉल करना दिखाता है: forState:। साथ ही, मैंने कुछ मामलों में एक्शनशीट्स जैसे पाया है कि एक बार जब आपने सेट किया है ImageEdgeInsets आपको शीर्षक EdgeInsets को समायोजित करने की आवश्यकता नहीं है - उन्होंने आपकी छवि की चौड़ाई पहले ही खाते में ली है। YMMV। – natbro

+3

यदि आप 'contentHorizontalAlignment' को 'UIControlContentHorizontalAlignmentRight' पर सेट करते हैं, तो आपको' titleEdgeInsets' में बाईं ओर एक इन्सेट जोड़ने की भी आवश्यकता है। यह वह जगह है: ' – vilanovi

-2

एक UIButton उपवर्ग बनाएँ और प्रोग्राम के रूप में अपने पाठ & छवि संरेखित करने के लिए अपने layoutSubviews विधि ओवरराइड। या https://github.com/stevestreza/BlockKit/blob/master/Source/UIView-BKAdditions.h जैसे कुछ का उपयोग करें ताकि आप ब्लॉक का उपयोग करके लेआउटसब्यूव्यू को कार्यान्वित कर सकें।

+0

Git हब के लिए लिंक काम नहीं कर रहा –

+0

https://github.com/stevestreza/BlockKit/: ' [UIEdgeInsetsMake (0, -imageSize.width, 0, imageSize.width) myButton setTitleEdgeInsets] ब्लॉब/मास्टर/स्रोत/UIView-BKAdditions.h –

+0

लिंक अपडेट करने के लिए धन्यवाद :)। अब –

13

निम्नलिखित कोड काम करता है:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = buttonRect; 
[button setTitle:@"A title" forState:UIControlStateNormal]; 
[button setImage:buttonImage forState:UIControlStateNormal]; 
button.imageEdgeInsets = UIEdgeInsetsMake(0.0, WIDTH(button.titleLabel) + 10.0, 0.0, 0.0); 
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 
4

आप भी अपने पाठ और छवि जहां भी चाहें आदेश की स्थिति में UIButton की titleRectForContentRect: और imageRectForContentRect: तरीकों ओवरराइड कर सकते हैं।

1

यहां स्विफ्ट में UIButton का उप-वर्ग है जो बाईं ओर की छवि को संरेखित करता है और केंद्र में पाठ को संरेखित करता है। अपने वांछित मूल्य पर imageLeftInset सेट करें।

class LeftImageAlignedButton : UIButton { 

var imageLeftInset : CGFloat = 10.0 

override func layoutSubviews() { 
    super.layoutSubviews() 
    self.contentHorizontalAlignment = .Left 
    if let font = titleLabel?.font { 
     let textWidth = ((titleForState(state) ?? "") as NSString).sizeWithAttributes([NSFontAttributeName:font]).width 
     let imageWidth = imageForState(state)?.size.width ?? 0.0 
     imageEdgeInsets = UIEdgeInsets(top: 0, left: imageLeftInset, bottom: 0, right: 0) 
     titleEdgeInsets = UIEdgeInsets(top: 0, left: bounds.width/2 - textWidth/2.0 - imageWidth, bottom: 0, right: 0) 
    } 
} 
} 
4

स्विफ्ट में किसी को भी कि (क 10pt अंतर के साथ) परवाह करता है के लिए:

let size = (self.titleForState(UIControlState.Normal)! as NSString).sizeWithAttributes([NSFontAttributeName:self.titleLabel!.font]) 
let offset = (size.width + 10) 
self.imageEdgeInsets = UIEdgeInsetsMake(0, offset, 0, -offset) 
self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, self.imageView!.frame.size.width + 10) 
4

आईओएस 9 पर उपयोग के लिए, मैं इस सूत्र में कई जवाब करने की कोशिश की लेकिन मेरे लिए उपयुक्त उनमें से कोई भी। मैं इस प्रकार खुद के लिए इस सवाल का जवाब मिला:

class MyButton: UIButton { 

    override func layoutSubviews() { 
    super.layoutSubviews() 

    self.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left 

    if self.imageView?.image != nil { 
     // Move icon to right side 
     self.imageEdgeInsets = UIEdgeInsets(
     top: 0, 
     left: self.bounds.size.width - self.imageView!.image!.size.width, 
     bottom: 0, 
     right: 0) 

     // Move title to left side 
     self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView!.frame.size.width + 8, 0, 0) 

    } 
    } 
} 

स्विफ्ट 3:

class MyButton: UIButton { 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     self.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left 

     if self.imageView?.image != nil { 
      // Move icon to right side 
      self.imageEdgeInsets = UIEdgeInsets(
       top: 0, 
       left: self.bounds.size.width - self.imageView!.image!.size.width, 
       bottom: 0, 
       right: 0) 

      // Move title to left side 
      self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView!.frame.size.width + 8, 0, 0) 

     } 
    } 
} 

मुझे आशा है कि यह मुझे के रूप में मामले में किसी को मदद मिलेगी!

+1

ग्रेट जॉब, मैं आपको स्विफ्ट 3 के साथ अपडेट करता हूं –

संबंधित मुद्दे