首页 > 代码库 > UIButton上使用UIEdgeInsetsMake让title跟图片对齐

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

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

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

 

使用以下设置后:

  [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)];

若要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)];

效果如下:

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

over!

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


 

实例源码:

 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];

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