[英]Exclusion rects not shifting text properly with NSTextView/UITextView
我有一个带有一些排除矩形的NSTextView
,因此我可以垂直移动文本,而不必求助于在字符串中添加换行符。 但是,我注意到排除 rect 相当有限,甚至可能有问题,因为我不能将文本垂直移动超过 textview 高度的约 45%。 我找到了一种解决方法,我将 textview 的高度增加了 2 倍,但这感觉很恶心,我宁愿“正确”地做
在上图中,我有三个文本视图(从左到右)
NSScrollView
中,2x 高度NSScrollView
封装。 2x 高度 排除矩形是用CAShapeLayer
绘制的,您可以看到“hello world new world”没有正确定位在排除矩形之外。
我尝试了所有三个示例,以确保在封装在NSScrollView
(我是 AppKit 和 TextKit 的新手)中时,我没有遗漏有关 IB 默认设置或文本视图动态的任何内容,但是所有 3 个示例都表现出相同的错误。
(每次 slider 在右下角移动时,它都会更新它们的文本)
label.stringValue = "excl: \(sender.integerValue): height: \(view.frame.height)"
print(sender.integerValue)
let exclHeight: CGFloat = CGFloat(slider.integerValue)
[aTextView, bTextView, cTextView]
.compactMap { $0 }
.forEach {
let rect = CGRect(
x: 5,
y: 5,
width: aTextView.frame.width - 10,
height: exclHeight)
$0.wantsLayer = true
$0.textContainer?.exclusionPaths.removeAll()
$0.textContainer?.exclusionPaths.append(.init(rect: rect))
$0.layer?.sublayers?.forEach {
$0.removeFromSuperlayer()
}
let layer = CAShapeLayer()
layer.frame = rect
layer.backgroundColor = NSColor.blue.withAlphaComponent(0.2).cgColor
layer.borderWidth = 1
layer.borderColor = NSColor.white.cgColor
$0.layer?.addSublayer(layer)
}
}
问题似乎是 excludePath 在第一行之前。
只是玩弄参数,一个两行示例文本,矩形y
位于第一行之后,没有任何问题。
所以看起来问题是计算容器高度时,它从-[NSLayoutManager usedRectForTextContainer:]
中的 excludePaths 开始
@interface LOLayoutManager : NSLayoutManager
@end
@implementation LOLayoutManager
- (NSRect)usedRectForTextContainer:(NSTextContainer *)container
{
NSRect rect = [super usedRectForTextContainer:container];
NSRect newRect = NSMakeRect(0, 0, NSMaxX(rect), NSMaxY(rect));
return newRect;
}
@end
这不是从 excludePaths 和行片段高度返回y
position ,而是返回一个从0, 0
开始的大矩形。 只要 NSTextView 只包含一个文本容器,这应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.