ios开发笔记根据传入字符串的长度动态生成label,并按照屏幕宽度排列

在开发工作中遇到了一个对我来说非常难得问题,搞了两天呀,都虚脱了,终于骗到大神给的demo做了出来效果是这样

首先后台的同事传一个字符串。。拒绝传数组,那么我们就要学会分割!

NSString *tagStr = [dictionary valueForKey:@"tags"];

NSArray *statusArray = [tagStr componentsSeparatedByString:@","];

这个就是把字符串按照符号分割数组的方法,接下来我们就可以根据内容动态生成标签了;
这是用到的宏:

#define FONT_SIZE 13.0f

#define HORIZONTAL_PADDING 7.0f

#define VERTICAL_PADDING 3.0f

#define CORNER_RADIUS 10.0f

#define LABEL_MARGIN 5.0f

#define BOTTOM_MARGIN 5.0f

#define BACKGROUND_COLOR UIColorFromRGB(0x04, 0x9f, 0xf1)

//[UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.00]

#define TEXT_COLOR [UIColor whiteColor]

#define TEXT_SHADOW_COLOR [UIColor whiteColor]

#define TEXT_SHADOW_OFFSET CGSizeMake(0.0f, 1.0f)

#define BORDER_COLOR [UIColor lightGrayColor].CGColor

#define BORDER_WIDTH 1.0f

这是代码:

if (tagStr.length > 0)//判断一下有值才生成

        {

            float totalHeight = 0;//

            CGRect previousFrame = CGRectZero;

            BOOL gotPreviousFrame = NO;//检查是否有前一个标签

            for (NSString *text in statusArray)

            {

                CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(self.frame.size.width, 1500) lineBreakMode:UILineBreakModeWordWrap];//动态获取字符串长度

                textSize.width += HORIZONTAL_PADDING*2;

                textSize.height += VERTICAL_PADDING*2;

                UILabel *label = nil;

                if (!gotPreviousFrame)

                {

                    label = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, textSize.width, textSize.height)];

                    totalHeight = textSize.height;

                }

                else

                {

                    CGRect newRect = CGRectZero;

                    if (previousFrame.origin.x + previousFrame.size.width + textSize.width + LABEL_MARGIN > self.frame.size.width) {

                        newRect.origin = CGPointMake(0, previousFrame.origin.y + textSize.height + BOTTOM_MARGIN);

                        totalHeight += textSize.height + BOTTOM_MARGIN;

                    }

                    else

                    {

                        newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);

                    }

                    newRect.size = textSize;

                    label = [[UILabel alloc] initWithFrame:newRect];

                }

                previousFrame = label.frame;

                gotPreviousFrame = YES;

                [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];

                if (!lblBackgroundColor)

                {

                    [label setBackgroundColor:BACKGROUND_COLOR];

                } else

                {

                    [label setBackgroundColor:lblBackgroundColor];

                }

                [label setTextColor:TEXT_COLOR];

                [label setText:text];

                [label setTextAlignment:UITextAlignmentCenter];

                //                [label setShadowColor:TEXT_SHADOW_COLOR];

                //                [label setShadowOffset:TEXT_SHADOW_OFFSET];

                [label.layer setMasksToBounds:YES];

                [label.layer setCornerRadius:CORNER_RADIUS];

                [label.layer setBorderColor:BORDER_COLOR];

                [label.layer setBorderWidth: BORDER_WIDTH];

                [_CorpTagView addSubview:label];

            }

            sizeFit = CGSizeMake(self.frame.size.width, totalHeight + 1.0f);//这个我一直不知道什么作用,抄来的O(∩_∩)O哈哈~

        }

posted @ 2015-04-20 20:16  土豪YY  阅读(480)  评论(0编辑  收藏  举报