问题标签 [nslayoutmanager]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1249 浏览

objective-c - 澄清 NSLayoutManager 和 NSTextContainer 关于垂直布局的行为?

代码示例

我在自定义(不是 TextView)中有一个NSLayoutManager, NSTextContainer & NSTextStorageas 属性,初始化如下:NSViewawakeFromNib()

故意将其垂直设置containerSizeNSTextContainer1,以查看它是否具有隐藏正在呈现的文本的预期效果 - 它没有!在视图中呈现此文本没有任何区别 - 这是问题的主题

drawRect我包括下面的行来绘制文本:

发现

  1. 最好在翻转坐标系中使用您的自定义视图,就像NSTextView我觉得我将进入一个痛苦的世界一样!不管怎样, NSLayoutManager 总是开始在一个翻转的坐标系统内部绘制它的文本(就像NSTextView
  2. containerSize.width属性NSTextContainer具有以下效果:它在所有级别(包括第一个级别)的行级别上绑定(我知道这很明显,但坚持我......)
  3. containerSize.height属性NSTextContainer有一个曲线球:即使包含的视图没有空间垂直显示它,它也不会绑定在第一行但将绑定到后续行

*我花了很长时间才得出这个假设,containerSize.height因为我只画了一条线!*

问题

  1. 我的结论是否NSTextContainer正确?
  2. 从垂直角度控制文本绘制的最佳方法是什么?我想将我的单行文本放在视图的底部(而不是像默认情况下那样浮动在顶部)
0 投票
3 回答
9995 浏览

cocoa - 如何使用显式 NSLayoutManager、NSTextStorage、NSTextContainer 以编程方式设置 NSTextView?

按照苹果文档,我试图NSTextView通过它的两个构造函数方法设置一个简单的。

我将下面的代码放在viewDidAppear内容视图的视图控制器的方法中。textView 是 的一个实例NSTextView,frameRect 是内容视图的框架。

以下 Swift 代码有效(给了我一个可编辑的 textView 并在屏幕上显示文本):

以下内容不起作用(文本视图不可编辑且屏幕上未显示文本):

我在第二个例子中做错了什么?我正在尝试遵循 Apple 的“Cocoa Text Architecture Guide”中给出的示例,他们在其中讨论了NSTextView通过显式实例化其帮助对象网络来设置一个。

0 投票
0 回答
270 浏览

cocoa - 如何清除 NSLayoutManager.drawGlyphsForGlyphRange(...) 的效果?

我有一个NSView元素被重用,主要用于绘制不同的文本,因为它们在NSScrollView. 我正在使用三重奏NSTextStorage, NSLayoutManager and NSTextContainer来显示文本,以下是在 drawRect 方法中绘制文本的调用:

我所看到的是,在连续调用drawRect它时,将要在旧文本之上绘制的新文本覆盖在视觉上有效地创建一大堆文本。

清除上次调用 drawRect 时绘制的文本的最佳方法是什么,特别是考虑到滚动性能:

NSRectFillA)在绘制文本之前调用drawRect- 这有效地绘制了先前的文本并且非常简单。在每个卷轴上一遍又一遍地绘制相同的背景似乎很浪费。

B)调查NSView.prepareForReuse()- 这种方法似乎更适合您想要“擦除”现有对象的全部内容以重用并从不同状态开始的场景。在我的场景中,这只是NSLayoutManager我想要删除的之前写的文本。

C)其他一些方法?

0 投票
1 回答
861 浏览

cocoa - 如何使用 NSLayoutManager 在 CALayer 中呈现文本?

我正在使用三重奏NSLayoutManager, NSTextStorage & NSTextContainer在 layer-backed 中呈现文本NSView

我的印象是,如果我可以覆盖wantsUpdateLayer以返回 true 并因此更充分地使用图层,则视图将具有更高的性能。这是真的?

但是,我看到的所有使用示例NSLayoutManager都说您需要将以下调用置于drawRect

您将如何在内部updateLayer或其他更面向层的地方执行等效调用?

0 投票
1 回答
1882 浏览

cocoa - NSStackView places NSViews on the baseline

Having reviewed most of Apple's (paucity) of documentation regarding NSStackView, plus the wise words of the greater Internet, including the collective wisdom of Stackoverflow, a problem remains.

Any NSView I add programmatically to my (IB instantiated) NSStackView, visually locate themselves to the bottom of their respective gravity area. Hence they overlay each other and don't behave like an expected "stack" of views with appropriate spacing.

Each of the gravity areas seem somehow compressed and unable to accommodate any more views. The more I apply NSStackView.addView to a gravity area the slimmer the existing views become.

I would expect NSStackView to naturally expand to accommodate the .size of the embedded views.

Any wise counsel is most appreciated ...

---more details—</p>

I place the StackView into the main window using interface builder. I can then add other views into areas of the stack in interface builder, such as buttons. These stack and layout as expected in IB and when running.

However, when I add or insert any view programmatically to any gravity area their bottom edge is positioned vertically down to the bottom edge of the StackView. This layout behaviour is the same for a vertical or horizontal stack.

Consequently, if I add more than one view programmatically they are sized OK and display but overlap. I am using 'NSBox' objects as views (for now) and they are sized using '.sizeToFit()'. I can see that their '.bounds.height' and 'width' are non zero.

I'm not sure how to set or log the intrinsic content size. That might be the next thing to try.

—no luck—

So, I have now worked rigorously through all permutations of: compression, hugging, (non-zero) intrinsic sizing, constraints, no constraints and various types of NSViews, but ... if I '.addView(...)' or '.insertView(...)' more than once, in any 'NSStackView' gravity area, the subsequent views overlay the previous ones and position themselves on the particular baseline. This is the edge closest to the floor of my office, regardless of the stack orientation.

Any 'NSStackView' and AutoLayout help, either emotional or technical, is most appreciated.

0 投票
1 回答
1672 浏览

ios - UITextView 中的动画 UIImage 与 TextKit

我有一个UIImage包含多个图像帧和一个从动画 GIF 文件中切片在一起的持续时间。如果我将图像加载到 a 中UIWebView,它将按预期进行动画处理,但我想使用 aUITextView代替。显而易见的选择是使用带有自定义NSLayoutManger和许多自定义NSAttributedString属性的 TextKit 来实现各种绘图效果。我有一个单曲UITextView,我想给出一个NSAttributedString包含NSTextAttachments 的所有我需要内联显示的图像。它工作正常,除了当我加载其中一个动画UIImages时,它们不会通过它们的帧进行动画处理。关于如何做到这一点的任何想法?我提出了一个理论上的选择:

  1. 为每个动画图像添加自定义属性
  2. 当布局管理器去绘制背景时,迭代这个属性来定位附件的位置。
  3. 回调文本视图以创建UIImageView带有动画图像的 s,添加为子视图,并调用startAnimating

我还没有尝试过,但它相当hacky。我想知道是否有更好的选择。

总而言之:我想在不需要额外对象的情况下为 a UIImageinside a UITextViewvia NSTextAttachmentinside a设置动画。您可以忽略以前使用. 具有用于设置动画帧的属性 ( , ),我目前已配置。如果我将它分配给它动画,如果我将它设置为 a ,则没有动画。NSAttributedStringUIImageViewUIWebViewUIImageimagesdurationUIImageViewNSTextAttachment

我将不胜感激任何反馈。谢谢!

0 投票
1 回答
1026 浏览

objective-c - 在 AppKit 中测量文本宽度的性能

AppKit 中有没有办法真正快速地测量大量 NSString 对象(比如一百万)的宽度?我尝试了 3 种不同的方法来做到这一点:

  • [NSString sizeWithAttributes:]
  • [NSAttributedString 大小]
  • NSLayoutManager(获取文本宽度而不是高度)

    这是一些性能指标



    NSLayoutManager 显然是要走的路,但问题是

  • 由于创建了重量级的NSTextStorage对象 ,内存占用高(根据分析器超过 1GB) 。
  • 高创建时间。所花费的所有时间都是在创建上述字符串期间,这本身就是一个破坏者。(随后测量具有创建和布局的字形的 NSTextStorage 对象只需要大约 0.0002 秒)。
  • 7 秒对于我想要做的事情来说仍然太慢了。有更快的方法吗?在一秒钟内测量一百万根弦?

    如果你想玩,这里是 github 项目。

  • 0 投票
    0 回答
    179 浏览

    objective-c - 将 NSlayoutManager 装入 UIView

    我已经使用 NSLayoutManager (TextKit) 设置了一个 UIView,并将 UIView 放置在 UIScrollView 中。我无法将 UIView 的大小调整为文本的大小,以便可以滚动它。

    目前,文本填满了屏幕,但 scrollView 并没有真正滚动到它的其余部分。

    UIView 代码:

    }

    使用

    我得到一个数字(我认为是文本的正确高度)。

    如何使用返回的数字来调整 UIView 的大小。

    在我的 ViewController.m 我尝试使用

    附言。UIView 作为 IBOutlet 附加到 UIViewController 并称为 daveningView,但 UIView 的自定义类名称是 CoreDavening。DaveningLabel 是 ScrollView(我知道它对于我计划修复的 scrollView 的措辞不好)

    编辑 1:视图控制器代码:

    没有限制。

    0 投票
    3 回答
    3802 浏览

    ios - 通过点击获取 UILabel 中的字符索引

    我想获取 UILabel 上点击字符的索引。我已经对 UILabel 进行了子类化。在我的 awakeFromNib() 我有这个:

    对于标签的前 5 个字符,它正在按照我想要的方式工作,就像我点击第一个字符并且在我的 touchesEnded 中我得到一个 0 的索引:

    var touchedCharacterIndex = layoutManager.characterIndexForPoint(touch.locationInView(self), inTextContainer: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)

    但是当我点击 UILabel 前四个字符之后的任何位置时,我得到 4 或 0,这是不正确的。

    谢谢,

    更新

    根据评论中的建议,我添加了以下内容:

    我把它称为layoutSubviews(),和,awakFromNib和 的设置器。boundsframeattributedTexttext

    现在它给了我一些奇怪的索引,比如如果文本长度是 21,点击第一个字母给我 6。

    0 投票
    1 回答
    1061 浏览

    ios - 使用 NSLayoutManager 创建动画文本效果?

    在WWDC 2013的第 220 场会议(带有文本工具包的高级文本布局和效果)中,他们明确表示NSLayoutManager可以与高级文本动画结合使用NSTextStorageNSTextContainer创建高级文本动画。他们没有说如何。

    我想用NSLayoutManager//来创建自定义文本动画NSTextStorageNSTextContainer简单地说,我想为单个字形的大小和位置设置动画,并淡化和不淡化特定的字形。

    似乎没有专门的方法或动画文档,NSLayoutManager我发现的唯一教程是here。然而,它展示了如何破解NSLayoutManager动画,而不是如何以应有的方式使用它(它们CATextLayer为每个单独的字形创建!)。

    有人可以指出我正确的方向吗?我知道如何使用NSLayoutManager//来呈现静态文本NSTextStorageNSTextContainer一些演示,展示动画文本的原理NSLayoutManager将是完美的。为了让我开始,我可以自己弄清楚细节。