动态判断文字大小,动态排版
来源:互联网 发布: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
- 动态判断文字大小,动态排版
- 动态修改ListView文字大小
- 动态修改ListView文字大小
- GridView自动动态排版
- android中动态文字排版
- uitableviewcell动态内容的排版
- 线性动态规划-文件排版
- vb.net动态生成控件并排版
- Android--GridView实现动态文字排版
- Android--GridView实现动态文字排版
- 动态文字排版(GridView与ArrayAdapter)
- Android 如何根据设置文字大小动态计算TextView长度、动态设置Relativelayout布局
- Android 如何根据设置文字大小动态计算TextView长度、动态设置Relativelayout布局
- Android 如何根据设置文字大小动态计算TextView长度、动态设置Relativelayout布局
- Android 版本动态判断
- ajax页面动态判断
- Silverlight 在Grid中动态增加控件并排版
- 多个label排版,动态长度及自动换行
- SGU 548
- IT忍者神龟之XHTML教程
- SGU 551
- uva 1485 - Permutation Counting(递推)
- HDU2545:树上战争
- 动态判断文字大小,动态排版
- 线段树--Color the ball(多次染色问题)
- python 视频处理,提取视频相关帧,读取Excel
- C#利用vbs控制3D Stereoscopic Player播放器
- Segments by Row Lock Waits
- 重新认识HTML,CSS,Javascript 之node-webkit 初探
- poj2411 Mondriaan's Dream
- JavaScript数值转换总结
- touch