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

UIButton上使用UIEdgeInsetsMake让title跟图片对齐

时间:2014-12-16 14:51:26      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   color   os   使用   sp   for   

UIButton上使用UIEdgeInsetsMake让title跟图片对齐

默认情况下,不设置的效果,都使居中现实,button为150*150

bubuko.com,布布扣

 

使用以下设置后:

  [self setTitleEdgeInsets:UIEdgeInsetsMake( 0.0,-backGroundImag.size.width, 0.0,0.0)];

  [self setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -self.titleLabel.bounds.size.width)];

bubuko.com,布布扣

若要title在图片的上方,则位置相对于图片来说,向上移动-80

    [self setTitleEdgeInsets:UIEdgeInsetsMake( -80.0,-backGroundImag.size.width, 0.0,0.0)];

    [self setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -self.titleLabel.bounds.size.width)];

效果如下:

bubuko.com,布布扣

综上所述,若单独设置一个title或者image在button中的位置,UIEdgeInsets是相对于button的frame来计算的(上,左,下,右,),如果是刚才所描述的情况,则title是相对于image的frame设置的,而image的位置是相对于titel的位置设置的

over!

以上转载自:http://blog.csdn.net/yanxiaoqing/article/details/7230660


 

实例源码:

bubuko.com,布布扣
 UIImage * image = [[UIImage imageNamed:@"telIcon"] retain];
    NSLog(@"%@>>",NSStringFromCGSize(image.size));
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundColor:[UIColor yellowColor]];
    [button setFrame:CGRectMake(80, 80, 150, 150)];
    [button setTitle:@"测试位置" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:10.0]];
    
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -image.size.width, 0.0, 0.0)];
    [button setImageEdgeInsets:UIEdgeInsetsMake(15.0, 0.0, 0.0, -button.titleLabel.bounds.size.width)];

    NSLog(@"%@",NSStringFromUIEdgeInsets(button.titleEdgeInsets));
    [button setImage:image forState:UIControlStateNormal];
    
    [self.view addSubview:button];
bubuko.com,布布扣

UIButton上使用UIEdgeInsetsMake让title跟图片对齐

标签:style   blog   http   ar   color   os   使用   sp   for   

原文地址:http://www.cnblogs.com/allanliu/p/4166992.html

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