如何在文本视图中实现水平尺? [英] How to implement a horizontal rule in a text view?

查看:87
本文介绍了如何在文本视图中实现水平尺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个 NSTextView (或iOS上的 UITextView ),我希望用户能够插入水平分隔线规则,例如HTML < hr> 标记或以下内容:

Say I have an NSTextView (or a UITextView on iOS) and I want the user to be able to insert horizontal divider rules, like the HTML <hr> tag or this thing:

Apple的文档非常重要。到目前为止,我有两个想法:

Apple's documentation is very thing on this. So far, I have two ideas:


  1. 插入 NSTextAttachment 每个规则并让布局管理器以某种方式绘制它。

  1. Insert an NSTextAttachment for each rule and make the layout manager draw it somehow.

而不是单个文本视图,而应使用禁用滚动的多个文本视图,并将它们放在堆栈视图中,在它们之间添加分隔符视图,然后将堆栈视图放入滚动视图。

Instead of a single text view, use multiple text views with scrolling disabled, put them in a stack view, add separator views between them and then put the stack view in a scroll view.

这两种方法似乎都有些错误或对我不利,因为:

Both approaches seem a little wrong or inelegant to me because:


  1. 在文档中,听起来文本附件最初是用于附加文件的。水平线不是文件。

  1. From the documentation, it sounds like text attachments are intended for attaching files in the first place. A horizontal rule is not a file.

如果我使用多个文本视图,可能会丢失 NSTextView固有的一些性能调整。 ,因为无论用户在何处滚动,所有文本视图都需要加载并准备显示。另外,多文本视图方法将阻止用户选择整个文本,这是我的应用程序所必需的。

If I use multiple text views, I'll probably lose some performance tweaks inherent to NSTextView as all the text views need to be loaded and ready for display, no matter where the user has scrolled. In addition, the multiple text view approach would prevent the user from selecting the entire text, which is a requirement in my app.

还有更好的主意吗?

推荐答案

我记得几年前曾尝试这样做。使用NSTextAttachment是对我有用的方法。我将与Mike Ferris的此对话添加为书签以帮助我记住怎么做。我没有代码了,但是很简单。我将 NSTextAttachmentCell 子类化,它符合 NSTextAttachmentCellProtocol 。您可以覆盖 cellFrame(对于textContainer:NSTextContainer,professionalLineFragment lineFrag:NSRect,glyphPosition位置:NSPoint,characterIndex charIndex:Int),您可以访问其容器以提供其宽度它具有行片段,它使文本在分隔符上方的y位置(加上所需的任何填充)。然后只需重写 draw(withFrame cellFrame:NSRect,在controlView:NSView?)方法并绘制分隔线即可。

I remember trying to do this years ago. Using NSTextAttachment was the method that worked for me. I bookmarked this conversation with Mike Ferris to help me remember how to do it. I don't have the code anymore, but it was pretty straightforward. I subclassed NSTextAttachmentCell which conforms to NSTextAttachmentCellProtocol. You override cellFrame(for textContainer: NSTextContainer, proposedLineFragment lineFrag: NSRect, glyphPosition position: NSPoint, characterIndex charIndex: Int) which gives you access to its container that can provide its width, and it has the line fragment which gives you the y position of the text above your divider (plus any padding you want). Then just override the draw(withFrame cellFrame: NSRect, in controlView: NSView?) method and draw your divider.

< hr> 标记一样嵌入文本本身。

As for the payload NSData, you could do anything with that. Maybe you include the width of the line, it's padding, color, etc? The good thing about using NSTextAttachment is that it stays embedded in the text itself like the <hr> tag.

这篇关于如何在文本视图中实现水平尺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆