IOS View Customization Tips--uibutton Implementation on display pictures, text below

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.