这篇文章主要介绍了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 根据内容来计算高度
猜你喜欢
- Flutter实现底部和顶部导航栏 2022-08-31
- Android实现轮询的三种方式 2023-02-17
- Android实现监听音量的变化 2023-03-30
- Android studio实现动态背景页面 2023-05-23
- iOS 对当前webView进行截屏的方法 2023-03-01
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- 详解flutter engine 那些没被释放的东西 2022-12-04
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
