这种【既带数字,又带图片的按钮】的效果的实现思路是怎样的?应该随着数字的变大,按钮也变长, 如下图 ---
3楼 @yangjie6020 UIButton就自带了UIImageVIew和UILabel
之前有个项目我是这样做的 如果需要动态调整宽度的话 略为修改一下代码就可以实现了
#import <UIKit/UIKit.h>
@interface TSSIconButton : UIButton
+ (id)buttonWithTagName:(NSString*)tagName;
@property (nonatomic, strong) UIImage *icon;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) UIFont *textFont;
@end
#import "TSSIconButton.h"
@implementation TSSIconButton
+ (id)buttonWithTagName:(NSString *)tagName
{
TSSIconButton *btn = [TSSIconButton buttonWithType:UIButtonTypeCustom];
btn.userInteractionEnabled = NO;
[btn setTagName:tagName];
btn.titleLabel.font = [APP getFont:18];
btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btn.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(2, 5, 0, 0);
return btn;
}
- (void)setIcon:(UIImage *)icon
{
[self setImage:icon forState:UIControlStateNormal];
}
- (void)setText:(NSString *)text
{
[self setTitle:text forState:UIControlStateNormal];
}
- (void)setTextColor:(UIColor *)textColor
{
[self setTitleColor:textColor forState:UIControlStateNormal];
}
- (void)setTextFont:(UIFont *)textFont
{
self.titleLabel.font = textFont;
}
@end