iOS button settings picture on the text below

Source: Internet
Author: User

UIButton at the same time set title and image, the default is the picture in the left text on the right, such as 1, in many cases we want the picture in the next, such as 2, just a few lines of code to be simple, you can achieve.

(1) Because more than one button needs to be processed, the implementation code is encapsulated as a method, passing each UIButton instance as a parameter, with the following code:


    1. -(void) Initbutton: (uibutton*) btn{
    2. Btn.contenthorizontalalignment = uicontrolcontenthorizontalalignmentcenter;//to center the picture and text horizontally
    3. [Btn Settitleedgeinsets:uiedgeinsetsmake (Btn.imageView.frame.size.height,-btn.imageview.frame.size.width, 0.0,0.0)];//text distance from the top border increases the height of the ImageView, the distance from the left box reduces the width of the ImageView, the distance from the bottom and right borders is unchanged
    4. [Btn setimageedgeinsets:uiedgeinsetsmake (0.0, 0.0,0.0,-btn.titlelabel.bounds.size.width)];//picture distance to the right border to reduce the width of the picture, Other non-side
    5. }


(2) then pass the button

[OBJC]View Plain Copy
    1. [Self initButton:self.btn1];
    2. [Self initButton:self.btn2];
    3. [Self initButton:self.btn3];
    4. [Self initButton:self.btn4];
    5. [Self initButton:self.btn5];
    6. [Self initButton:self.btn6];
    7. [Self initButton:self.btn7];


(3) Run the viewing effect.

iOS button settings picture on the 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.