Adjust the positions of images and text on UIButton and uibutton

Source: Internet
Author: User

UIButton Image

On UIButton, by default, the picture is in the left text on the right, and in most cases, the default display format cannot meet our needs. Next I will share my experiences on this issue.

By default, the effects that are not set are centered.

UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom];

Button. frame = CGRectMake (50, 50,150,100 );

Button. backgroundColor = [UIColor yellowColor];

[Button setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];

[Button setTitle: @ "title" forState: UIControlStateNormal];

[Button setImage: [UIImage imageNamed: @ "tab5"] forState: UIControlStateNormal];

[Self. view addSubview: button];

**************************************** *****************

UIEdgeInsetsMake (CGFloat top, CGFloat left, CGFloat bottom, CGFloat right );

The above four values are changed based on the original position, for example:

[Button setTitleEdgeInsets: UIEdgeInsetsMake (0, 0, 0, 60)];

[Button setImageEdgeInsets: UIEdgeInsetsMake (0, 60, 0, 0)];

The change of CGFloat right in title 60 is based on the right Border of titleLabel in the original position, which is the distance from the left border to the right border.

The same is true for images. The left border of the original imageView is shifted to 60 to the right.

**************************************** *****************

[Button setTitleEdgeInsets: UIEdgeInsetsMake (30, 0, 0, 30)];

[Button setImageEdgeInsets: UIEdgeInsetsMake (0, 30, 30, 0)];

The principle of up and down movement is the same as that of the left and right sides;

 

**************************************** *******

* Note: The displacement of the four values is based on the original position, for example *

* A number is to move downward based on the position of the original upper border, and a positive number moves downward to a negative number *

* Move up. The same applies to the left and right sides ;*

**************************************** *******

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.