Size with UITextView in iOS Development

In the iOS development, the UITextView is frequently used for display a NSString with multiple lines.
(在iOS开发中,经常用UITextView来显示一段多行的NSString)

In some of the applications, we need place a UITextView in UITableViewCell.
(在有些时候,会在UITableCell中使用它)

In the UITableViewCell, the height of cell is determined according to the height of UITextView, and the height of UITextView is dependent to the size of NSString which is displaying.
(然后如果要根据字符串绘制之后所占的高度来动态调整UITableViewCell的高度的话,就需要去计算字符串所占大小,再用这个大小去计算UITextView所占大小,再进一步计算Cell的高度)

When we calculated the size of NSString with the methods -sizeWithFont…blablabla
(用NSString自带的-sizeWithFont….系列函数可以计算出字符串的绘制所占用的区域大小)

It’s just the size of text.
(但是这只是文本本身的大小)

Outside the text, there is some space edge within the UITextView, and the edge is 8px in the top and bottom direction, 11px in the left and right direction.
(UITextView还有一个Padding边框,这个边框的大小是top和bottom是8px,left和right是11px)

So, we can get the height of text first, and then add 16px to get the height of UITextView, and then calculate the height of the UITableViewCell.
(所以,我们可以先计算出文本的高度,然后加上16px得到UITextView的高度,再去计算Cell的高度)

发表评论

电子邮件地址不会被公开。 必填项已用*标注