首页 > 其他分享 >iOS开发之字数不一的多标签Demo

iOS开发之字数不一的多标签Demo

时间:2022-11-28 17:03:31浏览次数:61  
标签:10 labelWidthF Demo self iOS width labelL strSize 标签


有朋友让帮他写一个封装的字数不一的多标签视图,所以今天将代码展示一下,供大家学习

代码中封装了两种方法,分别是:1.传递数组,数组中是NSString类型的方法;2.传递数组,数组中是NSDictionary类型的方法

首先介绍:1.传递数组,数组中是NSString类型的方法;

//初始化数组中是NSString类型的标签视图
-(instancetype)initWithFrame:(CGRect)frame textArray:(NSArray *)aArray textFont:(UIFont *)aFont
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //首先获取宽、高,确定显示的位置
        CGFloat widthF = frame.size.width;
        //CGFloat heightF = frame.size.height;
        
        //然后创建标签视图,注意:这个方法传过来的数组中就是NSString类型,可以直接显示
        CGFloat labelWidthF = 0.0f;
        CGFloat labelHeightF = 0.0f;
        for (int i = 0; i < aArray.count; i++)
        {
            NSString *str = aArray[i];
            CGSize strSize = [self sizeWithFont:str font:aFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
            
            //labelWidthF=labelWidthF+strSize.width+10;
            //判断宽、高
            if (widthF < labelWidthF+strSize.width+10)
            {
                labelWidthF=0.0f;
                labelHeightF = labelHeightF+strSize.height+10;
            }
            
            UILabel *labelL = [[UILabel alloc] initWithFrame:CGRectMake(labelWidthF, labelHeightF, strSize.width+10, strSize.height+10)];
            labelL.text = str;
            labelL.textAlignment = NSTextAlignmentCenter;
            labelL.backgroundColor = [UIColor lightGrayColor];
            labelL.font = aFont;
            [self addSubview:labelL];
            
            labelWidthF=labelWidthF+strSize.width+10;
        }
    }
    return self;
}

记住一定要计算控件的宽高,否则容易无法多行显示

第二种方式:2.传递数组,数组中是NSDictionary类型的方法

//初始化数组中是NSDictionary类型的标签视图
-(instancetype)initWithFrame:(CGRect)frame textArray:(NSArray *)aArray keyStr:(NSString *)aKeyStr textFont:(UIFont *)aFont
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //首先获取宽、高,确定显示的位置
        CGFloat widthF = frame.size.width;
        //CGFloat heightF = frame.size.height;
        
        //然后创建标签视图,注意:这个方法传过来的数组中就是NSString类型,可以直接显示
        CGFloat labelWidthF = 0.0f;
        CGFloat labelHeightF = 0.0f;
        for (int i = 0; i < aArray.count; i++)
        {
            NSDictionary *dic = aArray[i];
            NSString *str = dic[aKeyStr];
            CGSize strSize = [self sizeWithFont:str font:aFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
            
            //labelWidthF=labelWidthF+strSize.width+10;
            //判断宽、高
            if (widthF < labelWidthF+strSize.width+10)
            {
                labelWidthF=0.0f;
                labelHeightF = labelHeightF+strSize.height+10;
            }
            
            UILabel *labelL = [[UILabel alloc] initWithFrame:CGRectMake(labelWidthF, labelHeightF, strSize.width+10, strSize.height+10)];
            labelL.text = str;
            labelL.textAlignment = NSTextAlignmentCenter;
            labelL.backgroundColor = [UIColor lightGrayColor];
            labelL.font = aFont;
            [self addSubview:labelL];
            
            labelWidthF=labelWidthF+strSize.width+10;
        }
    }
    return self;
}

方法设定完成,进行调用

NSArray *arr1 = @[@"哈哈哈",@"呵呵",@"嘻嘻嘻嘻",@"嘿",@"么么么么么么",@"面朝大海,春暖花开",@"好好学习,天天向上,加油!"];
    BHMoreLabelView *bhMoreLV1 = [[BHMoreLabelView alloc] initWithFrame:CGRectMake(0, 170, self.view.frame.size.width, 100) textArray:arr1 textFont:[UIFont systemFontOfSize:15]];
    //bhMoreLV1.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:bhMoreLV1];
    
    NSArray *arr2 = @[@{@"name":@"哈哈哈"},@{@"name":@"呵呵"},@{@"name":@"嘻嘻嘻嘻"},@{@"name":@"嘿"},@{@"name":@"么么么么么么"},@{@"name":@"面朝大海,春暖花开"}];
    BHMoreLabelView *bhMoreLV2 = [[BHMoreLabelView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 100) textArray:arr2 keyStr:@"name" textFont:[UIFont systemFontOfSize:15]];
    //bhMoreLV2.backgroundColor = [UIColor redColor];
    [self.view addSubview:bhMoreLV2];

效果展示图:

iOS开发之字数不一的多标签Demo_ios




标签:10,labelWidthF,Demo,self,iOS,width,labelL,strSize,标签
From: https://blog.51cto.com/u_15894905/5892262

相关文章

  • iOS开发之自己封装的提示框(警告框)样式BHAlertView
    最近需要使用到提示框(警告框)进行信息的展示和提醒,所以进行了一个类的封装,想用Swift调用此OC文件,但是发现有些困难,所以暂时先把OC代码进行展示,随后再好好研究一下在Swift中的......
  • vue大文件上传demo
    ​ 以ASP.NETCoreWebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API,包括文件的上传和下载。 准备文件上传的API #region 文件上传......
  • 多标签用户画像分析跑得快的关键在哪里?
    用户画像分析需要使用众多标签来描述用户属性,通常有两类标签。一类用户标签的值可能有多个,比如用户学历是中学、大学、研究生、博士等,年龄段是children、juvenile、youth、m......
  • HTML基础-表单标签,button按钮,select下拉菜单
    HTML基础-表单标签目标和学习路径1.表单标签1.1input系列标签的基本介绍1.2input系列标签-文本框(拓展)value属性和name属性作用介1.3input系列标签-密码框......
  • HTML基础-列表标签和表格标签
    HTML基础-列表标签和表格标签二、列表标签2.列表的应用场景Ø场景:在网页中按照行展示关联性的内容,如:新闻列表、排行榜、账单等Ø特点:按照行的方式,整齐显示内容Ø......
  • axios的封装
    新建network文件夹及request.js  importaxiosfrom'axios'exportfunctionrequest(config){//创建axios实例constinstance=axios.create({......
  • 拓端tecdat|用于NLP的Python代写辅导:使用Keras的多标签文本LSTM神经网络分类
    用于NLP的Python:使用Keras的多标签文本LSTM神经网络分类 介绍 在本文中,我们将看到如何开发具有多个输出的文本分类模型。我们将开发一个文本分类......
  • axios请求
    安装npminstallaxios--save axios基本使用axios({url:'http://123.207.32.32:8000/home/multidata',method:'get'//不写默认get方式}).then(res......
  • 在iOS中使用NSURLProtocol进行网络代理
    在iOS中使用NSURLProtocol进行网络代理一引言网络能力是互联网应用程序必不可少的功能。随着应用程序的复杂,对网络的依赖性也会逐渐增高。如何统一的处理请求头,统一的处......
  • BIOS设置 翻译中文图文教程(一)
    BIOS设置图解教程­BIOS(基本输入/输出系统)是被固化在计算机CMOSRAM芯片中的一组程序,为计算机提供最初的、最直接的硬件控制。BIOS主要有两类∶AWARDBI......