UIButton中的三个UIEdgeInsets属性
生活随笔
收集整理的這篇文章主要介紹了
UIButton中的三个UIEdgeInsets属性
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
接著昨天的 UIButton中的三個UIEdgeInsets屬性 ,今天我們具體談談UIButton的contentEdgeInsets、titleEdgeInsets、imageEdgeInsets屬性。?
創建UIButton
UIButton *button = [[UIButton alloc] init];button.frame = CGRectMake(50, 200, 200, 50);[button setTitle:@"我是UIButton" forState:UIControlStateNormal];[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];[button setBackgroundColor:[UIColor orangeColor]];button.titleLabel.textAlignment = NSTextAlignmentLeft;button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;[self.view addSubview:button];?
創建一個button,讓button的title居左,以便觀察:
UIButton的contentEdgeInsets屬性
@property(nonatomic)UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero?
contentEdgeInsets里有一個content應該指的就是UIButton的title。
參數含義
上一篇文章我們講了UIEdgeInsets是個結構體類型。里面有四個參數,分別是:top, left, bottom, right。這四個參數表示距離上邊界、左邊界、下邊界、右邊界的距離。
這四個參數的值可以為正值,也可以為負值。拿left舉例:
left = 10; //代表以當前位置為基準,向右移動10個像素left = -10; //代表以當前位置為基準,向左移動10個像素向右移動20個像素
button.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);?
向右移動20個像素,left = 20,就可以了。
向左移動20個像素
button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);UIButton的titleEdgeInsets屬性
titleEdgeInsets和contentEdgeInsets的作用差不多。我們及設置contentEdgeInsets,又設置titleEdgeInsets,會怎樣呢?
button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);button.contentEdgeInsets = UIEdgeInsetsMake(0, 20 , 0, 0);?
看一下效果:
UIButton的titleEdgeInsets屬性
創建一個帶照片的button
UIButton *button = [[UIButton alloc] init]; button.frame = CGRectMake(50, 200, 200, 200); [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor orangeColor]]; [button setImage:[UIImage imageNamed:@"test"] forState:UIControlStateNormal]; [self.view addSubview:button];?
運行一下:
向右移動50個像素
button.imageEdgeInsets = UIEdgeInsetsMake(0, 50, 0, 0);?
看看效果:
向左移動50個像素
button.imageEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);?
看看效果:
總結
以上是生活随笔為你收集整理的UIButton中的三个UIEdgeInsets属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第1章-导言-习题1.13-1.17
- 下一篇: 命令行删除域名