动态判断文字大小,动态排版

来源:互联网 发布:php 自定义模版引擎 编辑:程序博客网 时间:2024/06/06 00:54
- (void)recommendViewUpdate:(NSArray *)arr{        UIButton *btn = (UIButton *)[self.recommendView viewWithTag:2000];    btn.hidden = YES;        for (id obj in self.recommendView.subviews) {        if ([obj isKindOfClass:[UIButton class]]) {            UIButton * btn = obj;            if (btn.tag < 2000) {                [obj removeFromSuperview];            }        }    }        for (int i = 0; i<arr.count; i++) {                NSDictionary *dict = [arr objectAtIndex:i];        NSString *comicName = [dict objectForKey:@"name"];                if (iOSValue >= 7.0) {            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];            btn.frame = CGRectMake(10, 20, 100, 40);            btn.tag = 1000+i;            btn.layer.borderWidth = 1;            btn.layer.borderColor = [UIColor blackColor].CGColor;            [btn setTitle:comicName forState:UIControlStateNormal];            NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:20] forKey:NSFontAttributeName];                        // 计算文本的大小self.view.bounds.size            CGSize textSize = [comicName boundingRectWithSize:self.view.bounds.size // 用于计算文本绘制时占据的矩形块                                                       options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading  // 文本绘制时的附加选项                                                    attributes:attributes        // 文字的属性                                                       context:nil].size; // context上下文。包括一些信息,例如如何调整字间距以及缩放。该对象包含的信息将用于文本绘制。该参数可为nil                                    CGRect rect = btn.frame;            rect.size.width = textSize.width;            rect.size.height = textSize.height;            UIButton *btn2 = (UIButton *)[self.recommendView viewWithTag:1000+i-1];                        if (btn2) {                                if (btn2.frame.origin.x+btn2.frame.size.width+10+rect.size.width+10<=self.recommendView.frame.size.width) {                    rect.origin.x = btn2.frame.origin.x+btn2.frame.size.width+10;                    rect.origin.y = btn2.frame.origin.y;                }else{                    rect.origin.y = btn2.frame.origin.y+btn2.frame.size.height+10;                    rect.origin.x = 10;                    if (rect.origin.y+rect.size.height+10>self.recommendView.frame.size.height) {                        self.recommendView.contentSize = CGSizeMake(self.recommendView.frame.size.width, rect.origin.y+rect.size.height+10);                    }                }                                btn.frame = rect;                            }else{                                btn.frame = rect;            }                        [self.recommendView addSubview:btn];        }else{                        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];            btn.frame = CGRectMake(10, 20, 100, 100);            btn.tag = 1000+i;                        btn.layer.borderWidth = 1;            btn.layer.borderColor = [UIColor blackColor].CGColor;            [btn setTitle:comicName forState:UIControlStateNormal];            CGSize sz = [comicName sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(MAXFLOAT, btn.frame.size.height)];            CGSize linesSz = [comicName sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(btn.frame.size.width, MAXFLOAT)];                        CGRect rect1 = btn.frame;            rect1.size.width = sz.width+40;            rect1.size.height = 18;//            if (linesSz.height > 36) {//                rect1.size.height = linesSz.height-18;//            }else{//                rect1.size.height = 18;//            }                                    UIButton *btn2 = (UIButton *)[self.recommendView viewWithTag:1000+i-1];                        if (btn2) {                                if (btn2.frame.origin.x+btn2.frame.size.width+10+rect1.size.width+10<=self.recommendView.frame.size.width) {                    rect1.origin.x = btn2.frame.origin.x+btn2.frame.size.width+10;                    rect1.origin.y = btn2.frame.origin.y;                }else{                    rect1.origin.y = btn2.frame.origin.y+btn2.frame.size.height+10;                    rect1.origin.x = 10;                                        if (rect1.origin.y+rect1.size.height+10>self.recommendView.frame.size.height) {                        self.recommendView.contentSize = CGSizeMake(self.recommendView.frame.size.width, rect1.origin.y+rect1.size.height+10);                    }                }                                btn.frame = rect1;                            }else{                                btn.frame = rect1;            }                        [self.recommendView addSubview:btn];        }    }}


其实是有一个sizeToFit的方法,可以自适文字大小,但是我们的效果是两边还要有留出空白文字居中,所以才用的这个方法.

在编程的过程中,发现,如果font的字体太过小得话,就无法自动判断出文字的宽度,各位小伙伴如果看见了,可以把字体大小调小一下,告诉我怎么修改呢?


-----------------7.31------------------

已解决,在addSubView的时候 强制

btn.titleLabel.font = [UIFontsystemFontOfSize:13];

0 0
原创粉丝点击