简体   繁体   English

UILabel中的自动收缩仅适用于宽度吗?

[英]Autoshrink in UILabel only for width?

I have some ASCII syntax diagrams which must not have line breaks in the middle. 我有一些ASCII语法图,中间不能有换行符。

These don't have to be editable so I thought the best way is to use an UILabel with auto shrink option. 这些不必是可编辑的,所以我认为最好的方法是使用带有自动收缩选项的UILabel。 But this option shrinks the text also if the content doesn't fit the height of the labels frame rectangle. 但是,如果内容不适合标签框矩形的高度,则此选项也会缩小文本。

I just want to shrink only if the content doesn't fit the width. 我只想仅在内容不适合宽度时才缩小。 It would be absolutely fine to scroll vertically through the text. 垂直滚动文本是完全可以的。

What is the best way to do this with UILabel or any other UI element? 使用UILabel或任何其他UI元素的最佳方法是什么?

使用UITextView,将“ editable”属性设置为false。

So let me rephrase your question. 因此,让我重新表述您的问题。 I guess what you want is a UILabel which can show multiple lines, but the longest line need to fit into the width of UILabel. 我猜您想要的是可以显示多行的UILabel,但是最长的行需要适合UILabel的宽度。 If this is what you want, well the imagination is weird to me... 如果这是您想要的,那么想象力对我来说很奇怪。

But anyway, I feel there's a conflict in your settings. 但是无论如何,我觉得您的设置存在冲突。 First, allowing multiple lines implies you set "Lines" attribute (number of lines) as 0, which allows unlimited lines. 首先,允许多行意味着您将“行”属性(行数)设置为0,这允许无限行。 But then Autoshrink will play no effect. 但是,然后自动收缩将不起作用。 I'm afraid it is not possible to be done by just setting the storyboard and instead, you need to write some code. 恐怕仅通过设置故事板是不可能完成的,相反,您需要编写一些代码。

I guess people have raised related questions earlier, by which they want to dynamically change the font size when the text become too long. 我想人们早先提出了相关问题,他们希望在文本过长时动态更改字体大小。 I guess you want to take a look about this: 我想您想看一下:

Autoshrink on a UILabel with multiple lines 在多行UILabel上自动收缩

The last issue is you also want the scrolling effect (this is why I feel the outlooking will be weird.) But in short, to achieve this you need 1) dynamically change the UILabel height, most likely using the same technique as explained in the reference thread, and 2) wrap the UILabel in a scroll view. 最后一个问题是您还希望滚动效果(这就是为什么我觉得外观会很奇怪的原因。)但是总之,要实现此目的,您需要1)动态更改UILabel高度,最有可能使用与参考线程,以及2)将UILabel包装在滚动视图中。 Maybe this can achieve what you want. 也许这可以实现您想要的。

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

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