網(wǎng)上試了下各種方法 感覺都不對贿条,這個是最靠譜的
總結(jié)模式:?
- 圖片在左处窥,title在右?
- 圖片在上嘱吗,title在下 :MKButtonEdgeInsetsStyleTop
- 圖片在右,title在左?
- 圖片在下滔驾,title在上
- (void)layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyle)style
? ? ? ? ? ? ? ? ? ? ? ? imageTitleSpace:(CGFloat)space {
? ? // 1. 得到imageView和titleLabel的寬谒麦、高
? ? //? ? CGFloat imageWith = self.imageView.frame.size.width;
? ? //? ? CGFloat imageHeight = self.imageView.frame.size.height;
? ? CGFloat imageWith = self.currentImage.size.width;
? ? CGFloat imageHeight = self.currentImage.size.height;
? ? CGFloat labelWidth = 0.0;
? ? CGFloat labelHeight = 0.0;
? ? if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
? ? ? ? // 由于iOS8中titleLabel的size為0,用下面的這種設(shè)置
? ? ? ? labelWidth = self.titleLabel.intrinsicContentSize.width;
? ? ? ? labelHeight = self.titleLabel.intrinsicContentSize.height;
? ? } else {
? ? ? ? labelWidth = self.titleLabel.frame.size.width;
? ? ? ? labelHeight = self.titleLabel.frame.size.height;
? ? }
? ? // 2. 聲明全局的imageEdgeInsets和labelEdgeInsets
? ? UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
? ? UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
? ? // 3. 根據(jù)style和space得到imageEdgeInsets和labelEdgeInsets的值
? ? switch (style) {
? ? ? ? case MKButtonEdgeInsetsStyleTop: {
? ? ? ? ? ? imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space, 0, 0, -labelWidth);
? ? ? ? ? ? labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space, 0);
? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? case MKButtonEdgeInsetsStyleLeft: {
? ? ? ? ? ? imageEdgeInsets = UIEdgeInsetsMake(0, -space, 0, space);
? ? ? ? ? ? labelEdgeInsets = UIEdgeInsetsMake(0, space, 0, -space);
? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? case MKButtonEdgeInsetsStyleBottom: {
? ? ? ? ? ? imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space, -labelWidth);
? ? ? ? ? ? labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space, -imageWith, 0, 0);
? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? case MKButtonEdgeInsetsStyleRight: {
? ? ? ? ? ? imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space, 0, -labelWidth-space);
? ? ? ? ? ? labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space, 0, imageWith+space);
? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? break;
? ? }
? ? // 4. 賦值
? ? self.titleEdgeInsets = labelEdgeInsets;
? ? self.imageEdgeInsets = imageEdgeInsets;
}