How to lay out a uibutton that contains the image and title

Source: Internet
Author: User

The Titleedgeinsets and imageedgeinsets in UIButton can manage the layout of the image and title in the button.
If the understanding of it is not deep enough, with pure digital layout management, after continuous debugging, or can try out, but if the picture size or text length, but also to do it again.
As a procedural ape, we should not give up any chance of laziness.

    • By default, the picture is left, the text is in the right, and the vertical center is displayed, such as:
button.titleEdgeInsets = UIEdgeInsetsZero;button.imageEdgeInsets = UIEdgeInsetsZero;

    • When you set the following layout, the pictures and text are centered.
button.Titleedgeinsets=Uiedgeinsetsmake(0,-button.ImageView.Framesize. Width00 Button.imageedgeinsets = uiedgeinsetsmake (0, 0, 0,-button.titlelabel.frame.size.width); //because of iOS8 in the size of 0, with the above set up a problem, modify it can titlelabel button. Imageedgeinsets = uiedgeinsetsmake (0 00-button. Titlelabel. Intrinsiccontentsize. Width               

    • If you want the picture to be on, the text is below, the horizontal center is displayed, then the following settings are required:
button.Titleedgeinsets=Uiedgeinsetsmake(0,-button.ImageView.Frame.Size.Width,-button.ImageView.Frame.Size.Height,0);Button.imageedgeinsets = Uiedgeinsetsmake (-button.titlelabel.frame.size.height, 0, 0,- Button.titleLabel.frame.size.width);//Because the size of the Titlelabel in the iOS8 is 0, there is a problem with the above setting, modify the button.  Imageedgeinsets = uiedgeinsetsmake(-button.  Titlelabel.  Intrinsiccontentsize.  Height, 0, 0, -button.  Titlelabel.  Intrinsiccontentsize.  width);                  

If you think the picture and text are too close, a little bit apart:

CGFloatOffset=40.0f;button.Titleedgeinsets=Uiedgeinsetsmake(0,-button.ImageView.Frame.Size.Width,-button.ImageView.Frame.Size.Height-Offset/2,0);Button.imageedgeinsets = Uiedgeinsetsmake (-BUTTON.TITLELABEL.FRAME.SIZE.HEIGHT-OFFSET/2, 0, 0,- Button.titleLabel.frame.size.width);//Because the size of the Titlelabel in the iOS8 is 0, there is a problem with the above setting, modify the button.  Imageedgeinsets = uiedgeinsetsmake(-button.  Titlelabel.  Intrinsiccontentsize.  Height-offset/2, 0, 0, -button.  Titlelabel.  Intrinsiccontentsize.  width);                  

    • Text left-aligned, picture right-aligned
button.Titleedgeinsets=Uiedgeinsetsmake(0,-button.ImageView.Frame.Size.Width-button.Frame.Size.Width+button.Titlelabel.Intrinsiccontentsize.Width,0,0);button.Imageedgeinsets=uiedgeinsetsmake(0, 0, 0, -button.  Titlelabel.  Frame.  Size.  Width - button.  Frame.  Size.  Width + button.  ImageView.  Frame.  Size.  width);                  

How to lay out a uibutton containing the image and title

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.