这篇文章主要介绍了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,UITextView,UILabel 根据内容来计算高度
基础教程推荐
猜你喜欢
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- Android多返回栈技术 2023-04-15
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- android studio按钮监听的5种方法实例详解 2023-01-12
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- Flutter手势密码的实现示例(附demo) 2023-04-11
- Android中的webview监听每次URL变化实例 2023-01-23
