IOS 中UITextField,UITextView,UILabel 根据内容来计算高度
更新时间:2017年03月22日 08:39:51 投稿:lqh
这篇文章主要介绍了IOS 中UITextField,UITextView,UILabel 根据内容来计算高度的相关资料,需要的朋友可以参考下
IOS 中UITextField,UITextView,UILabel 根据内容来计算高度
在开发的过程中,常常遇到根据内容来决定控件的高度的情况,常见的就是UITextField,UITextView,UILabel这三个控件,下面一UITextView 为例来说明一下:
首先新新建一个textView. 设施text,font
UITextView *textView = [[UITextView alloc] init]; textView.text = @"2015-01-19 14:07:47.290 MicroPort[3047:103721] -[PPRevealSideViewController gestureRecognizerDidTap:] [Line 1463] Yes, the tap gesture is animated, this is normal, not a bug! Is there anybody here with a non animate interface? :P"; textView.font = [UIFont systemFontOfSize:14]; float width =200; float height =[self heightForString:textView.text fontSize:14 andWidth:width]; textView.frame = CGRectmake(0,0,width,height); [self.view addSubview:textView];
计算textview高度的方法
- (float)heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width//根据字符串的的长度来计算UITextView的高度 { float height = [[NSStringstringWithFormat:@"%@\n ",value] boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeadingattributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:fontSize],NSFontAttributeName, nil] context:nil].size.height; return height; }
一般情况下常见的需求这个方法都能够满足
您可能感兴趣的文章:
- iOS UITextField最大字符数和字节数的限制详解
- iOS中修改UITextField占位符字体颜色的方法总结
- IOS UI学习教程之设置UITextField各种属性
- iOS应用开发中使UITextField实现placeholder属性的方法
- 解决iOS UITextField 编辑时文本偏移问题
- iOS开发之自定义UITextField的方法
- iOS UITextField、UITextView只限输入中文、英文、数字及实时限制字符个数的封装实现代码
- 解决iOS7上UITextField限制字数输入导致崩溃问题的方法
- iOS输入框(UITextField)密码明暗文切换方法
- iOS中监听UITextField值改变事件的方法实例
相关文章
Flutter Widgets MediaQuery控件屏幕信息适配
这篇文章主要为大家介绍了Flutter Widgets 之 MediaQuery控件获取屏幕信息和屏幕适配示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-11-11iOS 11 使用两种方法替换(Method Swizzling)去掉导航栏返回按钮的文字
这篇文章主要介绍了iOS 11 使用方法替换(Method Swizzling)去掉导航栏返回按钮的文字,需要的朋友可以参考下2018-05-05
最新评论