码迷,mamicode.com
首页 > 其他好文 > 详细

UIButton文字和图片偏移

时间:2015-05-18 20:18:44      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:

UIButton 设置偏移

@propery  titleEdgeInsets

@propery  imageEdgeInsets

@propery  contentEdgeInsets

@propery  contentHorizontalAlignment

初始化Button

1    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
2     NSString *title = @"音乐";
3     UIImage  *image = [UIImage imageNamed:@"sp_music_list_sc"];
4     [button setTitle:title forState:UIControlStateNormal];
5     [button setImage:image forState:UIControlStateNormal];
6     button.frame = CGRectMake(20, 100, 100, 40);
7     button.backgroundColor = [UIColor grayColor];

设置文字偏移

1   CGSize titleSize = [title sizeWithFont:[UIFont systemFontOfSize:10.f]];
2     CGSize imageSize = image.size;   
3     button.titleEdgeInsets = UIEdgeInsetsMake(15, -imageSize.width, 0, 0);

设置图片偏移

1 button.imageEdgeInsets = UIEdgeInsetsMake(-15,titleSize.width+10,0,0);

设置整体偏移 (文字和图片都会偏移)

1 button.contentEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 0);

设置对齐方式 (UIButton)

1 //对齐方式  设置content是title和image一起变化
2     button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

 

UIButton文字和图片偏移

标签:

原文地址:http://www.cnblogs.com/guojunzi/p/4512748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!