简体   繁体   English

有什么办法可以改变UIStackView中的UITextView的行距/边距/填充?

[英]Is there any way to alter line spacing/margins/padding for a UITextView inside a UIStackView?

So I have a prototype cell. 所以我有一个原型单元。

It's a horizontal stack view with several buttons, and a text view as the rightmost subview. 它是带有多个按钮的水平堆栈视图,而文本视图是最右边的子视图。

My problem is that the text view's text is too high up, which allows two lines to fit into the cell's height instead of just one. 我的问题是文本视图的文本太高了,这使得两行适合单元格的高度而不仅仅是一行。 But I need to keep the font size at this value, so I can't increase it and achieve one-line-per-cell that way. 但是我需要将字体大小保持在此值,所以我不能增加它并以这种方式实现每单元一行。

So I need to somehow make the line spacing/margins/etc. 所以我需要以某种方式使行间距/边距/等。 greater for the text view so that when there's only one line of text in it, the text will be centered in the cell. 对于文本视图更大,因此当其中只有一行文本时,文本将在单元格中居中。 Or some approximation of that, such as perhaps setting margins around the text view itself. 或近似值,例如在文本视图本身周围设置边距。

Yet I can't seem to find a way to do any of this. 但是,我似乎找不到任何方法可以做到这一点。

I tried setting constraints for the text view, but that just somehow makes the whole stack view vanish. 我尝试为文本视图设置约束,但这以某种方式使整个堆栈视图消失了。 I tried going into the Size Inspector for the text view and setting explicit layout margins, but Xcode altogether crashes from that completely, so I figure that's probably not allowed. 我尝试进入Size Inspector进行文本视图并设置显式的布局边距,但是Xcode完全导致崩溃,所以我认为这是不允许的。

Can anyone knowledgeable about UITextViews please share some insight? 可以对UITextViews有所了解的任何人都可以分享一些见解吗?

使用UITextField并将高度约束设置为大于等于优先级250的值。

After further research and testing, this appears to be the best way to set insets for the text in a text view: 经过进一步的研究和测试,这似乎是在文本视图中设置文本插图的最佳方法:

cellField.textContainerInset = UIEdgeInsetsMake(10.0, 0.0, 10.0, 0.0)

The same thing can be set via Storyboard in the UITextView's Identity Inspector tab by adding a "textContainerInset" runtime attribute with type "Rect" and setting desired values. 通过添加类型为“ Rect”的“ textContainerInset”运行时属性并设置所需的值,可以通过UITextView的Identity Inspector选项卡中的Storyboard设置相同的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM