根据内容显示左右带固定宽度边距和背景的标签

简介: 根据内容显示左右带固定宽度边距和背景的标签

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;
}
目录
相关文章
|
4月前
|
前端开发
css 图标和文字对齐 —— 垂直居中对齐,任意位置对齐
css 图标和文字对齐 —— 垂直居中对齐,任意位置对齐
98 2
|
4月前
|
前端开发 容器
css 动态文本对齐自适应 — 文本宽度小于容器宽度时居中对齐,文本宽度大于容器宽度时居左对齐
css 动态文本对齐自适应 — 文本宽度小于容器宽度时居中对齐,文本宽度大于容器宽度时居左对齐
39 2
|
5月前
|
前端开发 UED 容器
CSS基础-文本样式:颜色、字体、大小、对齐
【6月更文挑战第8天】本文介绍了CSS中设置文本颜色、字体、大小和对齐的基础知识。通过color属性改变文本颜色,使用font-family设置字体,用font-size调整大小,text-align控制对齐方式。注意浏览器兼容性、文本溢出和响应式设计的问题。提供的代码示例展示了这些属性的用法,帮助创建更具吸引力的网页。
66 6
|
4月前
|
前端开发 容器
CSS【详解】对齐 (含文本垂直对齐,文本水平对齐、单行文本垂直居中、多行文本垂直居中、6 种方案块级元素水平垂直居中 、7 种方案图片水平垂直居中、文本自适应对齐、图标和文本对齐,图片和文本对齐等)
CSS【详解】对齐 (含文本垂直对齐,文本水平对齐、单行文本垂直居中、多行文本垂直居中、6 种方案块级元素水平垂直居中 、7 种方案图片水平垂直居中、文本自适应对齐、图标和文本对齐,图片和文本对齐等)
71 0
|
4月前
|
JavaScript
ELEMENT-PLUES如何设计二级高亮样式,文字如何呈现高亮效果,如果字体出现不对齐的情况,侧边栏icon如何调位置,目录如何折叠,如何延迟动画播放,如何添加阴影,如何添加logo,在行内样式写宽
ELEMENT-PLUES如何设计二级高亮样式,文字如何呈现高亮效果,如果字体出现不对齐的情况,侧边栏icon如何调位置,目录如何折叠,如何延迟动画播放,如何添加阴影,如何添加logo,在行内样式写宽
|
6月前
使用SDAutoLayout实现控件根据内容进行宽度自适应和高度自适应
使用SDAutoLayout实现控件根据内容进行宽度自适应和高度自适应
110 2
|
6月前
|
前端开发
css样式字体、文本、背景属性,盒子模型详解,轻松调教出优美的字体和网页背景色
css样式字体、文本、背景属性,盒子模型详解,轻松调教出优美的字体和网页背景色
|
11月前
CSS3新增属性之圆角、盒阴影、字阴影
CSS3新增属性之圆角、盒阴影、字阴影
|
前端开发
HTML table 表格内容垂直对齐、边框颜色、标题
HTML table 表格内容垂直对齐、边框颜色、标题
56 0
HTML中图片和文字按照上下排列并且水平居中显示
HTML中图片和文字按照上下排列并且水平居中显示
297 0