NSString *str = [NSString stringWithFormat:@“fro%@aft”, getNotNilString(model.item)];
NSUInteger fillCharacterCount = 3;
NSUInteger fillAfterCharacterCount = 3;
NSMutableAttributedString *textAttributedString = [[NSMutableAttributedString alloc] initWithString:str];
[textAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, fillCharacterCount)];
[textAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(str.length- fillAfterCharacterCount, fillAfterCharacterCount)];
CBPItemListUnitEntity.h
#import <Foundation/Foundation.h>
@interface CBPItemListUnitEntity : BGBaseEntity
@property (nonatomic, strong) NSString *item_id;
@property (nonatomic, strong) NSString *item;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) BOOL isSelect;
//@property (nonatomic, assign) NSMutableAttributedString *textAttributedString;
//@property (nonatomic, strong) NSString *entity_title;
//@property (nonatomic, strong) NSString *entity_desc;
//@property (nonatomic, strong) NSString *entity_logo;
//@property (nonatomic, strong) NSString *entity_price;
//@property (nonatomic, strong) NSString *num;
@end
CBPItemListUnitEntity.m
```cpp
#import "CBPItemListUnitEntity.h"
@implementation CBPItemListUnitEntity
- (instancetype)init
{
self = [super init];
if (self) {
[self fillData];
}
return self;
}
-(void)fillData
{
}
-(void)setItem:(NSString *)item
{
_item = item;
_width = [self getRect].width;
}
-(CGSize)getRect
{
NSString *str = [NSString stringWithFormat:@"fro%@aft", getNotNilString(_item)];
CGSize size = [self getSizeOfString:str];
return CGSizeMake(size.width, size.height);
}
//- (void)updateTextAttributedStringWithString:(NSString *)string{
// if(isCommonUnitEmpty(string))
// {
// _textAttributedString = [[NSMutableAttributedString alloc] init];
// return;
// }
// NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:12]}; //字体属性,设置字体的font
// CGSize maxSize = CGSizeMake(FULL_WIDTH); //设置字符串的宽高 MAXFLOAT为最大宽度极限值 30为固定高度
// CGSize size = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
// return size; //此方法结合 预编译字符串 字体font 字符串宽高 三个参数计算文本 返回字符串宽度
//}
- (CGSize )getSizeOfString:(NSString *)string{
NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:12]}; //字体属性,设置字体的font
CGSize maxSize = CGSizeMake(FULL_WIDTH, 32); //设置字符串的宽高 MAXFLOAT为最大宽度极限值 30为固定高度
CGSize size = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
return size; //此方法结合 预编译字符串 字体font 字符串宽高 三个参数计算文本 返回字符串宽度
}
CBPItemListCollectionCell.h
```cpp
#import <UIKit/UIKit.h>
#import "PPCollectionViewCell.h"
//#import "BGCalendarMacro.h"
//#import "BGCalendarEntity.h"
#import "CBPItemListUnitEntity.h"
@interface CBPItemListCollectionCell : PPCollectionViewCell
@property (nonatomic, strong) UILabel *describeTitleLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) CBPItemListUnitEntity *model;
@property (nonatomic, assign) BOOL isHiddenLine;
- (instancetype)initWithFrame:(CGRect)frame;
//- (void)updateWithSelectCateToolUnitEntity:(CBPItemListUnitEntity *)selectCateToolUnitEntity model:(CBPItemListUnitEntity *)model;
@end
CBPItemListCollectionCell.m
@interface CBPItemListCollectionCell ()
@property (nonatomic, strong) UIView *selectBackgroundView;
@property (nonatomic, strong) UIView *unSelectBackgroundView;
@end
@implementation CBPItemListCollectionCell
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self setupViewWithFrame:frame];
}
return self;
}
-(void)setupViewWithFrame:(CGRect)frame
{
self.contentView.backgroundColor = [UIColor whiteColor];
self.selectBackgroundView = [[UIView alloc] init];
self.selectBackgroundView.backgroundColor = BGColorHex(FFF6F3);
self.selectBackgroundView.layer.borderColor = (BGColorHex(FF794C)).CGColor;
[self.selectBackgroundView.layer setBorderWidth:BG_1PX];
[self.selectBackgroundView addCornerWithCornerRadius:5];
[self.contentView addSubview:self.selectBackgroundView];
self.selectBackgroundView.hidden = YES;
self.unSelectBackgroundView = [[UIView alloc] init];
self.unSelectBackgroundView.backgroundColor = BGColorHex(F5F5F5);
[self.unSelectBackgroundView addCornerWithCornerRadius:5];
[self.contentView addSubview:self.unSelectBackgroundView];
[self.contentView addSubview:self.describeTitleLabel];
// [self.selectBackgroundView addSubview:self.lineView];
// [self unitsSdLayout];
}
//- (void)updateWithSelectCateToolUnitEntity:(CBPItemListUnitEntity *)selectCateToolUnitEntity model:(CBPItemListUnitEntity *)model
//{
// if((!selectCateToolUnitEntity || ![selectCateToolUnitEntity isKindOfClass:[CBPItemListUnitEntity class]]) || (!model || ![model isKindOfClass:[CBPItemListUnitEntity class]]) || (selectCateToolUnitEntity != model) || isCommonUnitEmptyString(selectCateToolUnitEntity.cate_id))
// {
// self.isHiddenLine = YES;
// self.model = model;
// }
// else
// {
// self.isHiddenLine = NO;
// self.model = model;
// }
//}
-(void)setIsHiddenLine:(BOOL)isHiddenLine
{
self.lineView.hidden = isHiddenLine;
_isHiddenLine = isHiddenLine;
}
-(void)setModel:(CBPItemListUnitEntity *)model
{
if(!model || ![model isKindOfClass:[CBPItemListUnitEntity class]])
{
return;
}
_model = model;
UIColor *color = BGColorHex(666666);
if(model.isSelect)
{
color = BGColorHex(FF794C);
self.unSelectBackgroundView.hidden = YES;
self.selectBackgroundView.hidden = NO;
}
else
{
self.unSelectBackgroundView.hidden = NO;
self.selectBackgroundView.hidden = YES;
}
NSString *str = [NSString stringWithFormat:@"fro%@aft", getNotNilString(model.item)];
NSUInteger fillCharacterCount = 3;
NSUInteger fillAfterCharacterCount = 3;
NSMutableAttributedString *textAttributedString = [[NSMutableAttributedString alloc] initWithString:str];
[textAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, fillCharacterCount)];
[textAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(str.length- fillAfterCharacterCount, fillAfterCharacterCount)];
[textAttributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(fillCharacterCount, str.length- fillAfterCharacterCount- fillCharacterCount)];
[textAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.f] range:NSMakeRange(0, str.length)];
self.describeTitleLabel.attributedText = textAttributedString;
[self unitsSdLayout];
}
-(void)unitsSdLayout
{
self.selectBackgroundView.sd_layout
.topSpaceToView(self.contentView, 0)
.rightSpaceToView(self.contentView, 0)
.widthIs(self.frame.size.width)
.heightIs(self.frame.size.height);
self.unSelectBackgroundView.sd_layout
.topSpaceToView(self.contentView, 0)
.rightSpaceToView(self.contentView, 0)
.widthIs(self.frame.size.width)
.heightIs(self.frame.size.height);
self.describeTitleLabel.sd_layout
.centerYEqualToView(self.contentView)
.rightSpaceToView(self.contentView, 0)
.widthIs(self.frame.size.width)
.heightIs(12);
[self.selectBackgroundView updateLayout];
[self.unSelectBackgroundView updateLayout];
[self.describeTitleLabel updateLayout];
// self.lineView.sd_layout
// .topSpaceToView(self.describeTitleLabel, 4)
// .centerXEqualToView(self.describeTitleLabel)
// .widthIs(40)
// .heightIs(2);
}
- (UIView *)lineView {
if(!_lineView)
{
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = BGColorHex(FF7648);
_lineView.hidden = YES;
}
return _lineView;
}
- (UILabel *)describeTitleLabel {
if(!_describeTitleLabel)
{
_describeTitleLabel = [[UILabel alloc] init];
_describeTitleLabel.backgroundColor = [UIColor clearColor];
_describeTitleLabel.textAlignment = NSTextAlignmentCenter;
_describeTitleLabel.font = BGFont(14);
_describeTitleLabel.numberOfLines = 1;
_describeTitleLabel.textColor = COMMON_FONT_COLOR;
_describeTitleLabel.text = @"教学用品";
}
return _describeTitleLabel;
}