天天看點

根據内容顯示左右帶固定寬度邊距和背景的标簽

根據内容顯示左右帶固定寬度邊距和背景的标簽

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)];      
#import

@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

@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      
@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;
}