Adjust the relative positions of UIButton images and text, and uibutton Images
Generally, If you directly set the image and text of UIButton, the relative positions of the two by default may not be what we want, so you need to adjust them.
The required functions are as follows:
UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
Top, left, bottom, and right indicate the movement volume in each direction respectively.
Instance description:
UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (50,100,300,300)]; // set the text [button setTitle: @ "test" forState: UIControlStateNormal]; [button setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal]; button. titleLabel. font = [UIFont systemFontOfSize: 25]; // set the background color of the button to black for differentiation. backgroundColor = [UIColor blackColor]; // set the image [button setImage: [UIImage imageNamed: @ "QQ"] forState: UIControlStateNormal]; [self. view addSubview: button];
:
Adjustment:
// If You Want To adjust the position of the title to the bottom of the image, you can adjust the title downward and move the button. titleEdgeInsets = UIEdgeInsetsMake (125,-150, 0, 0) to the left );
:
Generally, the size of the button is not so large, so you must adjust the position of the title and image at the same time in a limited space.
button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.