"UIButton implementation on the display of pictures, the next display text" the need to believe that everyone in the development will be more or less met. For example, when customizing shared view. Of course, you can also encapsulate an item, top ImageView, and a label underneath. But since there is a ready, slightly modified under the set can be, why also repeat the wheel. Sometimes a lot of things are not they didn't give the settings, but temporarily did not find how to set the method.
Example
1. At the outset, my plan
Custom classes inherit UIButton, and then
-(void) layoutsubviews{[Super Layoutsubviews];cgfloat midx = self.frame.size.width/2; cgfloat midy = self.frame.size.height/2; self.titlelabel.center = Span class= "hljs-built_in" >cgpointmake (midx, Midy + 15); self.imageview.center = cgpointmake (Midx, Midy-10);}
The effect, but also good, now the development of the project has been in use. But in the mind always feel such a little heterodoxy appearance, so there is another realization below.
2. Then tried several times to get the results
UIButton Adding extensions
- (void) Verticalimageandtitle: (CGFloat) spacing{Self. Titlelabel. backgroundcolor = [Uicolor Greencolor];Cgsize imageSize =Self. ImageView. Frame. size;Cgsize titlesize =Self. Titlelabel. Frame. size;Cgsize textSize = [Self. Titlelabel. Text Sizewithfont:Self. Titlelabel. Font];Cgsize framesize =Cgsizemake (CEILF (textSize. width), CEILF (textSize. height));if (titlesize. Width +0.5 < Framesize. width) {titlesize.width = Framesize.width;} cgfloat totalheight = (imagesize.height + TitleSize.height + spacing); self.imageedgeinsets = Uiedgeinsetsmake (-(Totalheight-imagesize.height), 0.0, Span class= "Hljs-number" >0.0,-titlesize.width); self.titleedgeinsets = Uiedgeinsetsmake (0,-imagesize.width,-(TotalHeight- Titlesize.height), 0);
The effect is as follows. The size of the ImageView and label is determined by the content, and the whole is always in the center, and the spacing between the upper and lower can be set. when using, make sure that the width of the button is greater than or equal to the image width.
2
3
IOS View Customization Tips--uibutton Implementation on display pictures, text below