有没有一种很好的方法来调整a的大小UITextView以符合其内容?比方说我有一个UITextView包含一行文字:
"Hello world"
Run Code Online (Sandbox Code Playgroud)
然后我添加另一行文字:
"Goodbye world"
Run Code Online (Sandbox Code Playgroud)
在Cocoa Touch中是否有一个好方法rect可以保存文本视图中的所有行,以便我可以相应地调整父视图?
再举一个例子,查看日历应用程序中事件的注释字段 - 注意单元格(及其UITextView包含)如何扩展以包含注释字符串中的所有文本行.
我有一个完全使用自动布局编程的视图.我在视图中间有一个UITextView,上面和下面有项目.一切正常,但我希望能够在添加文本时扩展UITextView.当它扩展时,它应该将其下面的所有内容都推下来.
我知道如何以"弹簧和支柱"的方式做到这一点,但是有一种自动布局方式吗?我能想到的唯一方法是每次需要增长时删除并重新添加约束.
我正在尝试根据内容以及它的兄弟容器和父容器来调整文本视图的大小.
下面的代码在iOS 6中正常运行
if (/* less than ios 7 */) {
CGRect frame = _textView.frame;
CGSize conSize = _textView.contentSize;
CGFloat difference = conSize.height - frame.size.height;
frame.size.height += difference;
_textView.frame = frame;
UIScrollView *parentView = (UIScrollView *)_textView.superview;
// adjust views residing below this text view.
// sibling view
UIView *belowView = // access it somehow
CGRect frame1 = belowView.frame;
frame1.origin.y += difference;
belowView.frame = frame1;
// adjust parent scroll view, increase height.
CGSize frame3 = parentView.contentSize;
frame3.height += difference;
parentView.contentSize = …Run Code Online (Sandbox Code Playgroud) 我有一个表格视图,todoTableView其中包含由用户创建的单元格.每个单元格都有文本视图.我想通过文本视图的内容更改单元格的高度.这是我试过的:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! TableViewCell
cell.bounds.size.height = cell.textView.bounds.size.height
return cell
}
Run Code Online (Sandbox Code Playgroud) 我试过sizeWithFont:constrainedToSize:lineBreakMode和sizeToFit,这两者不返回正确的结果.
该属性contentSize应该返回正确的高度,但是在文本视图呈现到屏幕之前它不起作用,我需要在文本视图可见之前计算高度(它确定a的高度)UITableViewCell.
有没有人找到正确计算UITextView高度的任何其他方法,自定义文本视图等?
编辑 我应该澄清我想要基于文本视图内容的理想高度.
ios ×5
uitextview ×4
autolayout ×2
cocoa-touch ×1
height ×1
ios7 ×1
iphone ×1
sizetofit ×1
swift ×1
uikit ×1
uitableview ×1
xcode ×1