美文网首页iOS 技巧
iOS图片和字符串合成Label展示笔记

iOS图片和字符串合成Label展示笔记

作者: 数字d | 来源:发表于2021-07-08 14:48 被阅读0次

    实现效果

    IMG_0069.PNG

    缺点在于小屏幕适配当中,当9999.99数据较长的时候,文字会直接变成...

    pod文件中

        pod 'YYKit'
    
    

    实现方式

    - (YYLabel *)douLab {
        if (_douLab == nil) {
            _douLab = [[YYLabel alloc]init];
            _douLab.displaysAsynchronously = YES;
            _douLab.textVerticalAlignment = YYTextVerticalAlignmentCenter;
            NSMutableAttributedString *finalStr = [[NSMutableAttributedString alloc]init];
            UIFont *font = [UIFont systemFontOfSize:11];
    //        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    //        paragraphStyle.lineSpacing = 3;
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 14, 14)];
            image.image = [UIImage imageNamed:@"yz_charge_icon_zhongdou"];
            NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
            [finalStr appendAttributedString:attachText];
            
            NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",_dou] attributes:@{NSForegroundColorAttributeName: UIColorHex(#303046),NSFontAttributeName:font}];
            [finalStr appendAttributedString:str];
            _douLab.frame = CGRectMake(0, 80, self.frame.size.width, 15);
            _douLab.attributedText = finalStr;
            _douLab.textAlignment = NSTextAlignmentCenter;
            _douLab.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
            
        }
        return _douLab;
    }
    

    附加礼物scrollview的实现

    YZItemGiftView.m

    
    
    #import "YZItemGiftView.h"
    
    
    @interface YZItemGiftView()<UIGestureRecognizerDelegate>
    @property(nonatomic,strong)UIView * colorLayerView;
    @property(nonatomic,strong)UIImageView * heartImageView;
    @property(nonatomic,strong)UILabel * titleInfoLab;
    @property(nonatomic,strong)YYLabel * douLab;
    @property(nonatomic,strong)NSString * dou;
    @property(nonatomic,strong)NSString * url;
    @property(nonatomic,strong)NSString * title;
    @property(nonatomic,copy)YZGiftTapBlock block;
    @end
    
    @implementation YZItemGiftView
    
    - (instancetype)initWithFrame:(CGRect)frame price:(NSString *)price imageUrl:(NSString *)url title:(NSString *)title
    {
        self = [super initWithFrame:frame];
        if (self) {
            _dou = @"9999.99";
    //        _dou = price;
    //        _url = url;
            _title = title;
            [self addSubview:self.colorLayerView];
            self.colorLayerView.hidden = YES;
            [self addSubview:self.heartImageView];
            [self addSubview:self.titleInfoLab];
            [self addSubview:self.douLab];
            [self.colorLayerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.equalTo(self);
            }];
            
            [_heartImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(4);
                make.centerX.equalTo(self);
                make.width.height.equalTo(@50);
            }];
            
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTap)];
            tap.delegate = self;
            [self addGestureRecognizer:tap];
        }
        return self;
        
    }
    
    -(void)clickTap {
       
    }
    
    -(UIImageView *)heartImageView {
        if (_heartImageView == nil) {
            _heartImageView = [[UIImageView alloc] init];
            _heartImageView.image = [UIImage imageNamed:@"yz_info_icon_gift"];
    //        [_heartImageView sd_setImageWithURL:[NSURL URLWithString:_url]];
        }
        return _heartImageView;
    }
    
    -(UIView *)colorLayerView {
        if (_colorLayerView == nil) {
            _colorLayerView = [[UIView alloc] init];
            _colorLayerView.layer.cornerRadius = 5;
            _colorLayerView.layer.masksToBounds = YES;
            _colorLayerView.layer.borderWidth = 0.5;
            _colorLayerView.layer.borderColor = UIColorHex(#0E76F1).CGColor;
            _colorLayerView.backgroundColor = [UIColor whiteColor];
        }
        return _colorLayerView;
    }
    -(UILabel *)titleInfoLab {
        if (_titleInfoLab == nil) {
            _titleInfoLab = [[UILabel alloc] initWithFrame:CGRectMake(2, 55, self.frame.size.width - 4, 25)];
            _titleInfoLab.text = @"给你棒棒糖";
    //        _titleInfoLab.text = _title;
            _titleInfoLab.textAlignment = NSTextAlignmentCenter;
            _titleInfoLab.font = [UIFont systemFontOfSize:12];
            _titleInfoLab.textColor = UIColorHex(#303046);
            _titleInfoLab.adjustsFontSizeToFitWidth = YES;
        }
        return _titleInfoLab;
    }
    
    - (YYLabel *)douLab {
        if (_douLab == nil) {
            _douLab = [[YYLabel alloc]init];
            _douLab.displaysAsynchronously = YES;
            _douLab.textVerticalAlignment = YYTextVerticalAlignmentCenter;
            NSMutableAttributedString *finalStr = [[NSMutableAttributedString alloc]init];
            UIFont *font = [UIFont systemFontOfSize:11];
    //        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    //        paragraphStyle.lineSpacing = 3;
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 14, 14)];
            image.image = [UIImage imageNamed:@"yz_charge_icon_zhongdou"];
            NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
            [finalStr appendAttributedString:attachText];
            
            NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",_dou] attributes:@{NSForegroundColorAttributeName: UIColorHex(#303046),NSFontAttributeName:font}];
            [finalStr appendAttributedString:str];
            _douLab.frame = CGRectMake(0, 80, self.frame.size.width, 15);
            _douLab.attributedText = finalStr;
            _douLab.textAlignment = NSTextAlignmentCenter;
            _douLab.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
            
        }
        return _douLab;
    }
    
    - (void)setSelectStatus:(BOOL)selectStatus {
        _selectStatus = selectStatus;
        if (selectStatus) {
            self.colorLayerView.hidden = NO;
        }else {
            self.colorLayerView.hidden = YES;
        }
    }
    
    @end
    
    
    

    YZItemGiftView.h

    
    typedef void(^YZGiftTapBlock)(NSInteger);
    @interface YZItemGiftView : UIView
    @property(nonatomic,assign)BOOL selectStatus;
    
    

    YZBannerGiftView.m

    
    
    #import "YZBannerGiftView.h"
    #import "YZItemGiftView.h"
    
    @interface YZBannerGiftView()<UIScrollViewDelegate>
    @property(nonatomic,strong)NSMutableArray * allArray;
    @property(nonatomic,assign)NSInteger needCount;
    @property(nonatomic,assign)CGFloat rateX;
    @end
    
    @implementation YZBannerGiftView
    - (id)initWithFrame:(CGRect)frame :(NSArray*)array;
    {
        self = [super initWithFrame:frame];
        if (self) {
            
            _rateX = Screen_Width / 375;
            
            _allArray = [NSMutableArray arrayWithArray:array];
            if (_allArray.count % 8 == 0) {
                _needCount = _allArray.count / 8;
            }else {
                _needCount = _allArray.count / 8 + 1;
            }
            self.pageCount = _needCount;
            [self creatScrollView];
        }
        return self;
    }
    
    -(void)creatScrollView{
        self.bannerScroll = [[UIScrollView alloc]init];
        self.bannerScroll.frame = CGRectMake(0, 0,Screen_Width,224);
        [self addSubview:self.bannerScroll];
        
        self.bannerScroll.contentSize = CGSizeMake((_needCount)*Screen_Width, 0);
        self.bannerScroll.pagingEnabled = YES;
        self.bannerScroll.showsHorizontalScrollIndicator = NO ;
        self.bannerScroll.delegate = self ;
        self.bannerScroll.bounces = NO;
        
        for (int i = 0; i < _needCount+1; i++) {
            UIView * view = [[UIImageView alloc] initWithFrame:CGRectMake(i*Screen_Width, 0, Screen_Width, Screen_Height)];
            view.backgroundColor = [UIColor clearColor];
            [self.bannerScroll addSubview:view];
        }
        
        if ((Screen_Width - 4 * 90 ) > 0) {
            CGFloat innerMargin = (Screen_Width - 4 * 80) / 5;
          
            for (int i = 0; i < _allArray.count; i ++) {
                
                NSInteger locX = i / 8;
                NSInteger indexY = i % 8;
                CGFloat allY;
                if (indexY > 3) {
                    allY = 10 + 100 + 4;
                }else {
                    allY = 10;
                }
                
                NSInteger infoX = indexY % 4;
                
                CGFloat Xmini = innerMargin + infoX * (80 + innerMargin);
                CGFloat allX = Xmini + locX * Screen_Width;
    
            
                CGRect rect = CGRectMake(allX, allY, 80, 100);
                
                YZItemGiftView * item = [[YZItemGiftView alloc] initWithFrame:rect];
                [self.bannerScroll addSubview:item];
                
            }
            
        }else {
            CGFloat innerMargin = (Screen_Width - 4 * 80 * _rateX) / 5;
          
            for (int i = 0; i < _allArray.count; i ++) {
                
                NSInteger locX = i / 8;
                NSInteger indexY = i % 8;
                CGFloat allY;
                if (indexY > 3) {
                    allY = 10 + 100 + 4;
                }else {
                    allY = 10;
                }
                
                NSInteger infoX = indexY % 4;
                
                CGFloat Xmini = innerMargin + infoX * (80 * _rateX + innerMargin);
                CGFloat allX = Xmini + locX * Screen_Width;
    
            
                CGRect rect = CGRectMake(allX, allY, 80 * _rateX, 100);
                
                YZItemGiftView * item = [[YZItemGiftView alloc] initWithFrame:rect];
                [self.bannerScroll addSubview:item];
                
            }
        }
        
        
        
        
        
    
        
        self.bannerPageControl = [[UIPageControl alloc] init];
        self.bannerPageControl.frame = CGRectMake(0, 224 - 25, Screen_Width, 25);
        self.bannerPageControl.numberOfPages = self.pageCount;
        self.bannerPageControl.currentPage = 0;
        self.bannerPageControl.pageIndicatorTintColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];;
        self.bannerPageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        [self addSubview:self.bannerPageControl];
    
    }
    
    
    
    
    - (void)changeScrollOffset {
        [self.bannerScroll setContentOffset:CGPointMake((self.bannerPageControl.currentPage+1) * Screen_Width, 0) animated:YES];
    }
    
    
    
    #pragma  mark **************UIScrollViewDelegate*******************
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        CGPoint point = scrollView.contentOffset;
        BOOL isRight = self.oldScrollOffset < point.x;
        self.oldScrollOffset = point.x;
        // 调整pageControl的当前 位置
        if (point.x > Screen_Width*(self.pageCount-1)+Screen_Width*0.5) {
            self.bannerPageControl.currentPage = 0;
        }else if (point.x > Screen_Width*(self.pageCount-1)  && isRight){
            self.bannerPageControl.currentPage = 0;
        }else{
            self.bannerPageControl.currentPage = (point.x+Screen_Width*0.5)/ Screen_Width;
        }
        
        //处理两种情况,1、当偏移量超出scrollView contentSize最大值时 2、当偏移量小于零时
        if (point.x >= Screen_Width*self.pageCount) {
            
    //        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
            
        }else if (point.x < 0) {
            
    //        [scrollView setContentOffset:CGPointMake(point.x+Screen_Width*(self.pageCount), 0) animated:NO];
        }
    }
    /**
     手指开始拖动的时候, 就让计时器停止
     */
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
        
     
    }
    /**
     手指离开屏幕的时候, 就让计时器开始工作
     */
    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
        
    }
    
    @end
    
    

    YZBannerGiftView.h

    
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface YZBannerGiftView : UIView
    @property(strong ,nonatomic) UIScrollView  *  bannerScroll; //滚动视图
    @property(strong ,nonatomic) UIPageControl *  bannerPageControl; //分页指示控件
    //@property(strong ,nonatomic) NSArray       *  bannerArray;   //轮播图name数组
    @property(assign ,nonatomic) NSUInteger       pageCount ;   //记录总页面数
    @property(assign ,nonatomic) NSUInteger       CurrentPageCount ; //记录当前的页面
    @property(assign ,nonatomic) float            oldScrollOffset;   //记录之前Scroll偏移量
    - (id)initWithFrame:(CGRect)frame :(NSArray*)array;
    @end
    
    NS_ASSUME_NONNULL_END
    
    

    YZLiveGiftView.m

    #import "YZLiveGiftView.h"
    #import "YZBannerGiftView.h"
    
    @interface YZLiveGiftView()<UIGestureRecognizerDelegate>
    
    @property(nonatomic,assign)CGFloat rateX;
    @property(nonatomic,strong)UIView * blackView;
    @property(nonatomic,strong)UIView * bottomContentView;
    
    @end
    
    @implementation YZLiveGiftView
    
    +(YZLiveGiftView *)show{
        YZLiveGiftView * view = [[YZLiveGiftView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
        UIWindow * window = [[UIApplication sharedApplication] keyWindow];
        [window addSubview:view];
        return view;
    }
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor clearColor];
            _rateX = Screen_Width / 375;
            [self addSubview:self.blackView];
            [self addSubview:self.bottomContentView];
        }
        return self;
    }
    
    -(UIView *)blackView {
        if (_blackView == nil) {
            _blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
            _blackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(remove)];
            tap.delegate = self;
            [_blackView addGestureRecognizer:tap];
        }
        return _blackView;
    }
    
    -(void)remove {
        [self removeFromSuperview];
    }
    
    -(UIView *)bottomContentView {
        if (_bottomContentView == nil) {
            _bottomContentView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_Height - 224 - 50 - SafeAreaBottomHeight, Screen_Width, 224)];
            _bottomContentView.backgroundColor = [UIColor whiteColor];
            NSMutableArray * array = [NSMutableArray arrayWithCapacity:0];
            for (int i = 0 ; i < 10 ; i ++) {
                [array addObject:@(i)];
            }
            YZBannerGiftView * view = [[YZBannerGiftView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 224) :array];
            [_bottomContentView addSubview:view];
        }
        return _bottomContentView;
    }
    
    @end
    
    

    YZLiveGiftView.h

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface YZLiveGiftView : UIView
    
    +(YZLiveGiftView *)show;
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    另外一个图片和文字组合成为一张图片的组合方式

    YZHeadImg.h

    #import <Foundation/Foundation.h>
    #import "YZHeadImageView.h"
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface YZHeadImg : NSObject
    + (UIImage*)imageWithFrame:(CGRect)frame BackGroundColor:(UIColor*)backGroundColor Text:(NSString*)text TextColor:(UIColor*)textColor TextFontOfSize:(CGFloat)size leavel:(NSString *)leavel;
    
    + (UIImage*)zbjimageWithFrame:(CGRect)frame BackGroundColor:(UIColor*)backGroundColor Text:(NSString*)text TextColor:(UIColor*)textColor TextFontOfSize:(CGFloat)size leavel:(NSString *)leavel;
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    YZHeadImg.m

    #import "YZHeadImg.h"
    
    @implementation YZHeadImg
    + (UIImage *)imageWithFrame:(CGRect)frame BackGroundColor:(UIColor *)backGroundColor Text:(NSString *)text TextColor:(UIColor *)textColor TextFontOfSize:(CGFloat)size leavel:( NSString *)leavel{
    //初始化并绘制UI
        YZHeadImageView *view = [[YZHeadImageView alloc] initWithFrame:frame BackGroundColor:backGroundColor Text:text TextColor:textColor TextFontOfSize:size lvleavel:leavel];
    
    //转化成image
    //    UIGraphicsBeginImageContext(view.bounds.size);
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [view.layer renderInContext:ctx];
        UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return tImage;
    }
    
    + (UIImage *)zbjimageWithFrame:(CGRect)frame BackGroundColor:(UIColor *)backGroundColor Text:(NSString *)text TextColor:(UIColor *)textColor TextFontOfSize:(CGFloat)size leavel:( NSString *)leavel{
    //初始化并绘制UI
        YZHeadImageView *view = [[YZHeadImageView alloc] initWithzbjFrame:frame BackGroundColor:backGroundColor Text:text TextColor:textColor TextFontOfSize:size lvleavel:leavel];
    
    //转化成image
    //    UIGraphicsBeginImageContext(view.bounds.size);
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [view.layer renderInContext:ctx];
        UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return tImage;
    }
    
    @end
    

    YZHeadImageView.m

    #import "YZHeadImageView.h"
    
    @implementation YZHeadImageView
    - (id)initWithFrame:(CGRect)frame BackGroundColor:(UIColor*)backGroundColor Text:(NSString*)text TextColor:(UIColor*)textColor TextFontOfSize:(CGFloat)size lvleavel:(NSString *)leavl{
        self = [super initWithFrame:frame];
        if (self) {
            UIView *backView = [[UIView alloc] initWithFrame:frame];
            backView.backgroundColor = backGroundColor;
            [self addSubview:backView];
            
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
    
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"yz_fans_icon_fshz_%@",leavl]];
    
            [backView addSubview:imageView];
            
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 0.5, 31, 13)];
            label.text = text;
            label.textColor = textColor;
            label.backgroundColor = backGroundColor;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:size];
            [backView addSubview:label];
        }
        return self;
    }
    
    - (id)initWithzbjFrame:(CGRect)frame BackGroundColor:(UIColor*)backGroundColor Text:(NSString*)text TextColor:(UIColor*)textColor TextFontOfSize:(CGFloat)size lvleavel:(NSString *)leavl {
        self = [super initWithFrame:frame];
        if (self) {
            UIView *backView = [[UIView alloc] initWithFrame:frame];
            backView.backgroundColor = backGroundColor;
            [self addSubview:backView];
            
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
    
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"yz_fans_icon_fshz_%@",leavl]];
    
            [backView addSubview:imageView];
            
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(7, 0, 27, 12)];
            label.text = text;
            label.textColor = textColor;
            label.backgroundColor = backGroundColor;
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:size];
            [backView addSubview:label];
        }
        return self;
    }
    @end
    
    

    YZHeadImageView.h

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface YZHeadImageView : UIView
    - (id)initWithFrame:(CGRect)frame BackGroundColor:(UIColor*)backGroundColor Text:(NSString*)text TextColor:(UIColor*)textColor TextFontOfSize:(CGFloat)size lvleavel:(NSString *)leavl;
    - (id)initWithzbjFrame:(CGRect)frame BackGroundColor:(UIColor*)backGroundColor Text:(NSString*)text TextColor:(UIColor*)textColor TextFontOfSize:(CGFloat)size lvleavel:(NSString *)leavl;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    

    用法:

    
            UIImage *imageTest = [YZHeadImg imageWithFrame:CGRectMake(0, 0, 45, 13) BackGroundColor:[UIColor clearColor] Text:model.badgetitle TextColor:[UIColor whiteColor] TextFontOfSize:7 leavel:model.badgelevel];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 45 ,13)];
            imageView.image = imageTest;
    
    

    效果:

    IMG_0070.jpg

    另外一个cell的展示是直播中的弹幕消息的实现

    #import "YZRoomDeleListCell.h"
    @interface YZRoomDeleListCell()<UIGestureRecognizerDelegate>
    @property(nonatomic,strong)UIImageView * headImg;
    @property(nonatomic,strong)UILabel * nameLab;
    @property(nonatomic,strong)UILabel * douLab;
    @property(nonatomic,strong)UIView * lineView;
    @property(nonatomic, strong) YYLabel *specialLab;
    @property(nonatomic,strong)NSString * yzdwImgStr;
    @property(nonatomic,assign)NSInteger index;
    @end
    @implementation YZRoomDeleListCell
    
    
    
    
    + (instancetype)cellWithTableView:(UITableView *)tableView index:(NSInteger)index
    {
        static NSString *identifier = @"YZFansContributeCell";
        // 1.缓存中取
        YZRoomDeleListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        // 2.创建
        if (cell == nil) {
            cell = [[YZRoomDeleListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier index:index];
        }
        return cell;
        
    }
    
    
    /**
     *  构造方法(在初始化对象的时候会调用)
     *  一般在这个方法中添加需要显示的子控件
     */
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier index:(NSInteger)index
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            self.layer.cornerRadius = 3;
            self.layer.masksToBounds = YES;
            _index = index;
            [self.contentView addSubview:self.headImg];
            [self.contentView addSubview:self.nameLab];
            [self.contentView addSubview:self.specialLab];
            [self.contentView addSubview:self.douLab];
            [self.contentView addSubview:self.lineView];
            [self configFrameLayout];
        }
        return self;
    }
    
    
    
    
    
    
    -(UIImageView *)headImg {
        if (_headImg == nil) {
            _headImg = [[UIImageView alloc] init];
                    [_headImg sd_setImageWithURL:[NSURL URLWithString:@"http://zhongyou-source.oss-cn-hangzhou.aliyuncs.com/file/2021/04/02/161733533962.png"]];
            _headImg.layer.cornerRadius = 15;
            _headImg.layer.masksToBounds = YES;
            
        }
        return _headImg;
    }
    
    -(UILabel *)nameLab {
        if (_nameLab == nil) {
            _nameLab = [[UILabel alloc] init];
            _nameLab.text = @"木兰花海";
            _nameLab.textColor = UIColorHex(#303046);
            _nameLab.font = [UIFont systemFontOfSize:14];
            _nameLab.textAlignment = NSTextAlignmentLeft;
        }
        return _nameLab;
    }
    
    
    -(UILabel *)douLab {
        if (_douLab == nil) {
            _douLab = [[UILabel alloc] init];
            if (_index == 0) {
                _douLab.text = @"移除房管";
                _douLab.textColor = UIColorHex(#0E76F1);
    
                _douLab.layer.borderColor = UIColorHex(#0E76F1).CGColor;
            }else if (_index == 1){
                _douLab.text = @"解除禁言";
                _douLab.textColor = UIColorHex(#FF3F57);
    
                _douLab.layer.borderColor = UIColorHex(#FF3F57).CGColor;
            }else if (_index == 2){
                _douLab.text = @"解除踢出";
                _douLab.textColor = UIColorHex(#0E76F1);
                _douLab.layer.borderColor = UIColorHex(#0E76F1).CGColor;
            }else if (_index == 3){
                _douLab.text = @"卸任房管";
                _douLab.textColor = UIColorHex(#0E76F1);
                _douLab.layer.borderColor = UIColorHex(#0E76F1).CGColor;
                _douLab.backgroundColor = UIColorHex(#F5FAFF);
            }
            _douLab.layer.cornerRadius = 12.5;
            _douLab.layer.masksToBounds = YES;
            _douLab.layer.borderWidth = 0.5;
            _douLab.font = [UIFont systemFontOfSize:12];
            _douLab.textAlignment = NSTextAlignmentCenter;
            _douLab.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(douAction)];
            tap.delegate = self;
            [_douLab addGestureRecognizer:tap];
        }
        return _douLab;
    }
    
    -(void)douAction {
        [ToastUtils showMessage:@"remove" duration:1 position:Toast_Point_Center];
    }
    
    - (void)setModel:(YZFansTrueLoveModel *)model{
       
        _model = model;
        
        _nameLab.text = model.nickname;
    //    _douLab.text = [NSString stringWithFormat:@"解除房管"];
        [_headImg sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    
        NSMutableAttributedString *finalStr = [[NSMutableAttributedString alloc]init];
        UIFont *font = [UIFont systemFontOfSize:12];
        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
        paragraphStyle.lineSpacing = 3;
    
    
        if(model.gradeimg.isNotBlank){
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 15)];
            [image sd_setImageWithURL:[NSURL URLWithString:model.gradeimg]];
            // 测试数据
            NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
            [finalStr appendAttributedString:attachText];
        }
    
        if ([model.grouplevel integerValue] > 0) {
            UIImage *imageTest = [YZHeadImg imageWithFrame:CGRectMake(0, 0, 45, 13) BackGroundColor:[UIColor clearColor] Text:model.badgetitle TextColor:[UIColor whiteColor] TextFontOfSize:7 leavel:model.badgelevel];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 45 ,13)];
            imageView.image = imageTest;
            NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
            [finalStr appendAttributedString:attachText];
        }
    
        self.specialLab.attributedText = finalStr;
    
        YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(Screen_Width - 100, MAXFLOAT)];
        YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text:self.specialLab.attributedText];
        self.specialLab.textLayout = textLayout;
    
        self.specialLab.frame = CGRectMake(50,29, textLayout.textBoundingSize.width, textLayout.textBoundingSize.height);
    
    }
    
    - (void)setInfoModel:(YZRoomDeleteModel *)infoModel {
        _infoModel = infoModel;
        
        _nameLab.text = _infoModel.nickname;
    //    _douLab.text = [NSString stringWithFormat:@"解除房管"];
        [_headImg sd_setImageWithURL:[NSURL URLWithString:_infoModel.avatar]];
    
        NSMutableAttributedString *finalStr = [[NSMutableAttributedString alloc]init];
        UIFont *font = [UIFont systemFontOfSize:12];
        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
        paragraphStyle.lineSpacing = 3;
    
    
        if(_infoModel.smallimage.isNotBlank){
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 15)];
            [image sd_setImageWithURL:[NSURL URLWithString:_infoModel.smallimage]];
            // 测试数据
            NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
            [finalStr appendAttributedString:attachText];
        }
    
        if (_infoModel.level > 0) {
            UIImage *imageTest = [YZHeadImg imageWithFrame:CGRectMake(0, 0, 45, 13) BackGroundColor:[UIColor clearColor] Text:_infoModel.groupname TextColor:[UIColor whiteColor] TextFontOfSize:7 leavel:[NSString stringWithFormat:@"%d",_infoModel.level]];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 45 ,13)];
            imageView.image = imageTest;
            NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
            [finalStr appendAttributedString:attachText];
        }
    
        self.specialLab.attributedText = finalStr;
    
        YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(Screen_Width - 100, MAXFLOAT)];
        YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text:self.specialLab.attributedText];
        self.specialLab.textLayout = textLayout;
    
        self.specialLab.frame = CGRectMake(50,29, textLayout.textBoundingSize.width, textLayout.textBoundingSize.height);
        
    }
    
    
    -(void)configFrameLayout {
    
            [_headImg mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(15);
                make.centerY.equalTo(self);
                make.width.height.equalTo(@30);
            }];
        
        [_nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(6);
            make.left.equalTo(_headImg.mas_right).offset(5);
            make.width.greaterThanOrEqualTo(@10);
            make.height.equalTo(@23);
        }];
        
        [_douLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.equalTo(@25);
            make.width.equalTo(@65);
            make.centerY.equalTo(self);
        }];
    }
    
    -(UIView *)lineView {
        if (_lineView == nil) {
            _lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 49.5, Screen_Width - 30, 0.5)];
            _lineView.backgroundColor = UIColorHex(#EEEEEE);
        }
        return _lineView;
    }
    
    
    - (YYLabel *)specialLab {
        
        if (_specialLab == nil) {
            _specialLab = [[YYLabel alloc]init];
            _specialLab.numberOfLines = 0;
            _specialLab.textAlignment = NSTextAlignmentLeft;
            _specialLab.lineBreakMode = NSLineBreakByCharWrapping;
            _specialLab.displaysAsynchronously = YES;
            _specialLab.textVerticalAlignment = YYTextVerticalAlignmentCenter;
        }
        return _specialLab;
    }
    
    
    -(void)getServiceData {
        NSString * url = @"";
        if (_index == 0) {
            url = URL_YZ_anchor_room_delete_0;
        }else if (_index == 1){
            url = URL_YZ_anchor_room_delete_1;
        }else if (_index == 2){
            url = URL_YZ_anchor_room_delete_2;
        }else if (_index == 3){
            url = URL_YZ_anchor_quitMyRoom;
        }
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        if (_index == 3) {
            [dic setValue:_infoModel.room forKey:@"room"];
        }else{
            [dic setValue:_model.ID forKey:@"userid"];
            if (_index == 2 || _index == 1) {
                [dic setValue:[XTGlobalManager shareInstance].identification forKey:@"room"];
            }
        }
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",httpsUrl,url];
        [CustomRequest cus_request:urlStr withRequesType:HttpsRequestTypePost parameters:dic success:^(NSDictionary * _Nullable responseObjc) {
            NSLog(@"获取列表信息index = 1:%@",responseObjc);
            if (YZ_success_back == 1) {
                self.infoBlock();
            }else {
                YZ_success_show;
            }
        } failure:^(NSError * _Nullable error) {
    
        }];
    }
    
    
    
    @end
    
    

    效果图大概是这样子的

    IMG_0071.jpg

    相关文章

      网友评论

        本文标题:iOS图片和字符串合成Label展示笔记

        本文链接:https://www.haomeiwen.com/subject/uwqtpltx.html