UIButton 上默認是圖片在左文字在右,而大多數(shù)情況這樣默認的的顯示形式都不能滿足我們的需求,接下來我就這個問題分享一下我的心得。
默認情況下氯材,不設置的效果,都是居中實現(xiàn)
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);
上面的四個數(shù)值是基于原位置而改變的例如:
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 60, 0, 0)];
title 的CGFloat right改變的60 是基于原位置的titleLabel的右邊框向左平移60也就是到右邊框的距離硝岗。
image也是一樣氢哮,距離原來imageView的左邊框向右平移了60。
*********************************************************
[button setTitleEdgeInsets:UIEdgeInsetsMake(30, 0, 0, 30)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 30, 30, 0)];
上下移動的原理同左右型檀;
***********************************************
* 注意:這4個數(shù)值的位移都是基于原來的位置進行移動的例如第? *
* 一個數(shù)就是基于原來上邊框的位置向下移動命浴,正數(shù)向下移動負數(shù) *
* 向上移動;左右同理贱除;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *
***********************************************