如何让 NSTextField 表现得像 HTML 文本?

发布于 2024-10-18 08:58:22 字数 496 浏览 6 评论 0原文

我有一些静态文本,通常只有 1 行长,显示在我的 xib 的 NSTextField 中。在某些情况下,文本足够长,足以保证 2 行,我只想垂直调整标签大小以适应它,而不给我滚动条或任何其他东西。想想网页上的文本如何表现……这就是我想要的。我只是希望标签随着不同的文本而缩小,并调整其宽度。我怎样才能实现这个目标?



更新
以下是有关其当前行为方式的视频: http://screencast.com/t/4JYTv7jVG3O

注意当 NSTextField 长为两行时,文本下方有一个很大的间隙。这是因为星星和按钮在框架的底部对齐,并且因为我必须使框架更高以容纳 2 行,所以它们保留在那里。如果我能得到这个问题的答案,我会让 1 行文本的框架变短,并使底部文本字段(带有较小的文本)更高以进行补偿。这种浮动布局可以实现吗?

I have some static text that is usually only 1 line long that displays in a NSTextField in my xib. In some instances, the text is long enough to warrant 2 lines, and I just want the label to resize vertically to fit it, without giving me scrollers or any thing else. Think of how text on a webpage behaves… that is what I want. I just want the label to grow shrink with different text, and with adjustments to its width. How can I achieve this?

UPDATE
Here is a video of how it currently behaves: http://screencast.com/t/4JYTv7jVG3O

Notice how when the NSTextField is two lines long, there is a big gap underneath the text. This is because the stars and button are aligned at the bottom of the frame, and because I have to have the frame taller to accommodate 2 lines, they stay there. If I can get an answer to this question, I would make the frame shorter for the 1 line text, and make the bottom textfield (with the smaller text) taller to compensate. Can this type of floating layout be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡大本事 2024-10-25 08:58:22

一种选择是实际使用 WebView 来显示您的内容。然后,您将获得您所期望的行为,但需要花费一些时间来管理与控件的交互。

您需要使用[webView setDrawsBackground:NO]WebView设置为不显示背景。

您还需要使用 HTML/CSS 构建内容(包括星级和按钮),然后使用 Objective-C/JavaScript 桥在按下按钮时回调到您的应用程序。

有关从 JavaScript 调用 Objective-C 的更多信息,请参见 在这里

您也可以使用 NSTextView 并将按钮和星级嵌入为 NSTextAttachment 对象,但这相当复杂,使用 WebView 会容易得多

我能看到的唯一的其他选择是编写一个视图控制器,它根据容器视图的当前大小来管理控件的布局。您需要测量文本才能做到这一点,一种方法是使用优秀的 NS(属性)String+Geometrics 类别,由 Jerry Krinock 编写。

One option is to actually use a WebView to display your content. You will then get exactly the behaviour you are expecting, at the cost of a bit of work to manage interaction with the controls.

You would need to set the WebView to display no background, using [webView setDrawsBackground:NO].

You'd also need to construct the content (including the star rating and the button) using HTML/CSS and then use the Objective-C/JavaScript bridge to call back to your app when the button is pressed.

More information on calling Objective-C from JavaScript is here.

You could probably also use an NSTextView and embed the button and star rating as NSTextAttachment objects but this is quite complex, it would be a lot easier to use a WebView.

The only other alternative that I can see is writing a view controller that manages the layout of the controls based on the current size of their container view. You would need to measure the text to do this and one way to do that is to use the excellent NS(Attributed)String+Geometrics category written by Jerry Krinock.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文