动态获取UITextView 高度
2016-01-27 14:56
543 查看
- (CGSize)getStringRectInTextView:(NSString *)string InTextView:(UITextView *)textView;
{
//实际textView显示时我们设定的宽
UITextView* localTextView=textView;
CGFloat contentWidth = CGRectGetWidth(localTextView.frame);
//但事实上内容需要除去显示的边框值
CGFloat broadWith = (localTextView.contentInset.left + localTextView.contentInset.right
+ localTextView.textContainerInset.left
+ localTextView.textContainerInset.right
+ localTextView.textContainer.lineFragmentPadding/*左边距*/
+ localTextView.textContainer.lineFragmentPadding/*右边距*/);
CGFloat broadHeight = (localTextView.contentInset.top
+ localTextView.contentInset.bottom
+ localTextView.textContainerInset.top
+ localTextView.textContainerInset.bottom);
//由于求的是普通字符串产生的Rect来适应textView的宽
contentWidth -= broadWith;
CGSize InSize = CGSizeMake(contentWidth, MAXFLOAT);
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = localTextView.textContainer.lineBreakMode;
NSDictionary *dic = @{NSFontAttributeName:localTextView.font, NSParagraphStyleAttributeName:[paragraphStyle copy]};
CGSize calculatedSize = [string boundingRectWithSize:InSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
CGSize adjustedSize = CGSizeMake(ceilf(calculatedSize.width),calculatedSize.height + broadHeight);
return adjustedSize;
}
{
//实际textView显示时我们设定的宽
UITextView* localTextView=textView;
CGFloat contentWidth = CGRectGetWidth(localTextView.frame);
//但事实上内容需要除去显示的边框值
CGFloat broadWith = (localTextView.contentInset.left + localTextView.contentInset.right
+ localTextView.textContainerInset.left
+ localTextView.textContainerInset.right
+ localTextView.textContainer.lineFragmentPadding/*左边距*/
+ localTextView.textContainer.lineFragmentPadding/*右边距*/);
CGFloat broadHeight = (localTextView.contentInset.top
+ localTextView.contentInset.bottom
+ localTextView.textContainerInset.top
+ localTextView.textContainerInset.bottom);
//由于求的是普通字符串产生的Rect来适应textView的宽
contentWidth -= broadWith;
CGSize InSize = CGSizeMake(contentWidth, MAXFLOAT);
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = localTextView.textContainer.lineBreakMode;
NSDictionary *dic = @{NSFontAttributeName:localTextView.font, NSParagraphStyleAttributeName:[paragraphStyle copy]};
CGSize calculatedSize = [string boundingRectWithSize:InSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
CGSize adjustedSize = CGSizeMake(ceilf(calculatedSize.width),calculatedSize.height + broadHeight);
return adjustedSize;
}
相关文章推荐
- Java - String, StringBuffer and StringBuilder
- 两个UIViewController之间的翻转可以用动画效果翻转(上下,左右)--跨界面之间的视图切换
- HDU-4908-BestCoder Sequence【思维题】
- RESTEasy数据自动装配之@QueryParam
- ClassNotFound: edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue
- UIScrollView 实践经验
- 解决VirtualBox分辨率太小及VBoxGuestAdditions_4.3.12.iso下载地址
- druid数据源例子
- Value Dispose() cannot be called while doing CreateHandle().
- 实现ueditor + 图片空间 实现图片上传
- UIButton详解
- mac配置Android 环境,完整过程,其中包括遇到的设置$JAVA_HOME问题,新建项目报 Errors running builder 'Android Resource Manager问题
- (转)UEFI系统的启动过程
- NGUI图片闪光
- Xcode 7测试化工具XCTestCase
- (转)String和StringBuilder、StringBuffer的区别?
- iOS 蓝牙使用小结 bluetooth
- iOS开发-UI控件:自定义UITableViewCell 上的多个按钮点击事件处理
- MCU51单片机uIP协议栈+ENC28J60网卡 移植、应用
- iOS开发中UIDatePicker控件的使用方法简介