UIButton文字和图片偏移
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;