iOS开发 标签的实现

作者: MangoJ | 来源:发表于2016-04-16 09:42 被阅读1289次

    本文主要运用button实现标签创建及选择功能,简单易懂可以直接将代码复制到xcode的.m文件中使用.

    效果图
    
    屏幕快照 2016-04-16 上午9.49.52.png

    //
    // ViewController.m
    // Sirius_选择标签的创建
    //
    // Created by Sirius on 16/4/15.
    // Copyright © 2016年 Sirius. All rights reserved.
    //
    #define SCREEN_WIDTH self.view.bounds.size.width
    #define SCREEN_HEIGHT self.view.bounds.size.height
    #import "ViewController.h"

    @interface ViewController ()
    {
    NSString *STR;
    NSMutableArray *btnAll;
    NSArray *array ;
    }
    @property(nonatomic,strong)UITextView *text;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor grayColor];
    [self creatUI];
    
    }
    
    
    -(void)creatUI{
    //设置初始值(不设置点击标签时会在TextView中显示一个NULL)
    STR = @"";
    //要显示的标签数组
    
    array =@[@"尊严这个",@"东西,其实",@"是和欲望",@"成反比的,",@"你想得到",@"一个东西,",@"就会变得",@"低三下四,",@"死皮赖脸,",@"而当你对",@"眼前",@"这个人",@"这个事",@"无动于衷",@"的时候,",@"尊严就会",@"在你心中",@"拔地而起."];
    
    // 保存前一个button的宽以及前一个button距离屏幕边缘的距
    CGFloat edge =0;
    //设置button 距离父视图的高
    CGFloat h =100;
    
    
    for (int i =0; i< array.count; i++) {
        UIButton *button =[UIButton buttonWithType:UIButtonTypeSystem];
        button.tag =100+i;
        button.backgroundColor =[UIColor grayColor];
        [button addTarget:self action:@selector(selectClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        
        //确定文字的字号
        NSDictionary *attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:12]};
        //计算文字的长度值
        CGFloat length = [array[i] boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        
        //为button赋值
        [button setTitle:array[i] forState:(UIControlStateNormal)];
        
        //设置button的frame
        button.frame =CGRectMake(edge+10, h, length+20, 30);
        
        //当button的位置超出屏幕边缘时换行 320 只是button所在父视图的宽度
        if (edge+10+length +15 >SCREEN_WIDTH) {
            //换行时将左边距设置为10
            edge =0;
            //距离父视图的高度变化
            h=h+button.frame.size.height +10;
            //标签换行后的frame
            button.frame =CGRectMake(edge+10, h, length+15, 30);
            
        }
        
        //获取前一个button的尾部位置位置
        edge = button.frame.size.width +button.frame.origin.x;
        
        [self.view addSubview:button];
        [btnAll addObject:button];
        
        //******设置 数组的第一个button为选中状态
        ((UIButton*)[btnAll objectAtIndex:0]).selected =YES;
    }
    _text = [[UITextView alloc]initWithFrame:CGRectMake(0, 300, SCREEN_WIDTH, 60)];
    [self.view addSubview:_text];
    
    
    }
    
    //点击事件
    - (void)selectClick:(UIButton *)btn{
    
    
    STR = [NSString stringWithFormat:@"%@%@",STR,array[btn.tag - 100]];
    _text.text = STR;
    
    }
    
    @end

    相关文章

      网友评论

        本文标题:iOS开发 标签的实现

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