IPhone development-UIButton setImage also displays the title

Source: Internet
Author: User

IPhone development-UIButton setImage also displays the title

If you want to create a button, the upper part of the button displays the Image, and the lower part displays the Title (the text position is not fixed). The display effect is as follows:

 

UIButton has two methods to set the Image: setImage and setBackgroundImage. Which method is used? If you are not sure, test it to see which meets your requirements.

1. First test the setImage method, drag a UIbutton control in IB (xib or storyboard), and then set the Image attribute, for example:

 

See the UIButton control style you just dragged, for example:


No matter how you zoom in the button, the size of the image in the button will not change (of course, the image will be compressed ).

If you do not like IB's friends, you can use the Code as follows:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];[btn setImage:[UIImage imageNamed:@add_album] forState:UIControlStateNormal];

2. Test the setBackgroundImage method, select the button control, delete the Image content, and add the Image name in the Background, for example:

Then observe the button and find that the button style is changed

Click the zoom in button to enlarge the image.

The code implementation is as follows:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];[btn setBackgroundImage:[UIImage imageNamed:@add_album] forState:UIControlStateNormal];

After the above test, we found that the image set using the setImage method will not be enlarged with the button. The image is always the size of the original image, but the image set using the setBackgroundImage method, the image will become larger as the button grows. Here I have selected the first method. Please feel free to visit the official website.

So how can I set the button Title position so that it is displayed at the bottom? The Title is center by default. This requires the inset attribute. Select the button, select the Title in the Edge option, and then adjust the Top and Left parameters of the Inset so that the Title position is at the bottom. For example:


This completes the effect of customizing the button title and button image position.

Code Implementation setting inset:

 

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];    [btn setImage:[UIImage imageNamed:@add_album] forState:UIControlStateNormal];    [btn setTitle:@Title forState:UIControlStateNormal];   //[btn setImageEdgeInsets:(UIEdgeInsets)]    [btn setTitleEdgeInsets:UIEdgeInsetsMake(75, -19, 0, 0)];    [self.view addSubview:btn];

 

In addition, if you do not like to adjust the Inset, you can place a Label on the button, set the Title that comes with the button to null, use the Label as the Title, and adjust the frame of the Label, this effect can also be achieved.

 

Related Article

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.