UIImage *buttonImg = [button imageForState:UIControlStateNormal]; CGFloat titleWidth = [button.titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:18],NSFontAttributeName, nil]].width; [button setTitleEdgeInsets:UIEdgeInsetsMake(buttonImg.size.height, -buttonImg.size.width, 0, 0)]; [button setImageEdgeInsets:UIEdgeInsetsMake(-15, 0, 0, -titleWidth)];
效果如下。俩个图不对称是因为图片大小不同。
时间: 2024-10-05 13:11:54