1 IDSGenderLeviNamedView 的实现效果
2 类的封装方法:
IDSGenderLeviNamedView.h
@interface IDSGenderLeviNamedView : UIView
@property (nonatomic, strong) UILabel *ageLabel;
@property (nonatomic, strong) UIImageView *genderImageView;
- (instancetype)initWithGender:(NSInteger)gender age:(NSInteger)age;
- (void)gender:(NSInteger)gender age:(NSInteger)age;
@end
IDSGenderLeviNamedView.m
#import "IDSGenderLeviNamedView.h"
@implementation IDSGenderLeviNamedView
#pragma mark - 初始化需求函数
- (instancetype)initWithGender:(NSInteger)gender age:(NSInteger)age
{
if (self = [superinit]) {
self.genderImageView = [[UIImageViewalloc] initWithFrame:CGRectMake(3, 2, 10, 10)];
if (gender == 0) {
self.genderImageView.image = IDSImageNamed(@"img_yiqiwan_man");
self.backgroundColor = NF_Color_C32;
}
else {
self.genderImageView.image = IDSImageNamed(@"img_yiqiwan_woman");
self.backgroundColor = NF_Color_C30;
}
self.genderImageView.contentMode = UIViewContentModeScaleAspectFill;
[selfaddSubview:self.genderImageView];
self.ageLabel = [[UILabelalloc] init];
self.ageLabel.font = [UIFontsystemFontOfSize:Near_Final_Font_T1];
self.ageLabel.textColor = NF_Color_C1;
if (age) {
self.ageLabel.text = [NSStringstringWithFormat:@"%ld",age];
self.ageLabel.frame = CGRectMake(CGRectGetMaxX(self.genderImageView.frame)+1, 0, 0, 0);
self.ageLabel.font = [UIFontsystemFontOfSize:Near_Final_Font_T1];
self.ageLabel.textColor = NF_Color_C1;
[self.ageLabelsizeToFit];
self.ageLabel.centerY = self.genderImageView.centerY;//测试一下可行不?
[selfaddSubview:self.ageLabel];
self.frame = CGRectMake(0, 0, CGRectGetMaxX(self.ageLabel.frame)+3, 14);
}
else {
self.frame = CGRectMake(0, 0, CGRectGetMaxX(self.genderImageView.frame)+3, 14);
}
self.layer.cornerRadius = 3.0f;
self.layer.masksToBounds = YES;
self.clipsToBounds = YES;
}
returnself;
}
- (void)gender:(NSInteger)gender age:(NSInteger)age
{
self.genderImageView.frame = CGRectMake(3, 2, 10, 10);
if (gender == 0) {
self.genderImageView.image = IDSImageNamed(@"img_yiqiwan_man");
self.backgroundColor = NF_Color_C32;
}
else {
self.genderImageView.image = IDSImageNamed(@"img_yiqiwan_woman");
self.backgroundColor = NF_Color_C30;
}
self.genderImageView.contentMode = UIViewContentModeScaleAspectFill;
self.ageLabel.font = [UIFontsystemFontOfSize:Near_Final_Font_T1];
self.ageLabel.textColor = NF_Color_C1;
if (age) {
self.ageLabel.hidden = NO;
self.ageLabel.text = [NSStringstringWithFormat:@"%ld",age];
self.ageLabel.frame = CGRectMake(CGRectGetMaxX(self.genderImageView.frame)+1, 0, 0, 0);
self.ageLabel.font = [UIFontsystemFontOfSize:Near_Final_Font_T1];
self.ageLabel.textColor = NF_Color_C1;
[self.ageLabelsizeToFit];
self.ageLabel.centerY = self.genderImageView.centerY;//测试一下可行不?
self.frame = CGRectMake(0, 0, CGRectGetMaxX(self.ageLabel.frame)+3, 14);
}
else {
self.ageLabel.hidden = YES;
self.frame = CGRectMake(0, 0, CGRectGetMaxX(self.genderImageView.frame)+3, 14);
}
self.layer.cornerRadius = 3.0f;
self.layer.masksToBounds = YES;
self.clipsToBounds = YES;
}
@end
- OVER