cocoa - 排除使用 NSTextView/UITextView 无法正确移动文本
问题描述
我有一个NSTextView
带有一些排除的矩形,所以我可以垂直移动文本,而不必求助于在字符串中添加换行符。但是,我注意到排除 rect 相当有限,甚至可能有问题,因为我不能将文本垂直移动超过 textview 高度的约 45%。我找到了一种解决方法,我将 textview 的高度增加了 2 倍,但这感觉很恶心,我宁愿“正确”地做
在上图中,我有三个文本视图(从左到右)
- 以编程方式将其封装在
NSScrollView
2x 高度内 - 以编程方式
NSScrollView
封装。2x 高度 - 使用界面生成器,常规高度。
排除矩形是用 a 绘制的CAShapeLayer
,您可以看到“hello world new world”没有正确定位在排除矩形之外。
我尝试了所有三个示例,以确保在封装在NSScrollView
(我是 AppKit 和 TextKit 的新手)中时,我没有遗漏有关 IB 默认设置或文本视图动态的任何内容,但是所有 3 个示例都表现出相同的错误。
更新文本排除矩形的代码
(每次滑块在右下角移动时,它都会更新它们的文本矩形)
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
在第一行之后放置矩形的两行示例文本就可以正常工作。
所以看起来问题是计算容器高度时,它以 excludePaths 开头-[NSLayoutManager usedRectForTextContainer:]
@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
y
这不是从 excludePaths 和线段高度返回位置,而是返回一个从 开始的大矩形0, 0
。只要 NSTextView 只包含一个文本容器,这应该可以工作。
推荐阅读
- apache-nifi - nifi 检查:属性值匹配
- mysql - 如何使用带有散列密码(bcrypt)的 JSON 文件为数据库播种?
- javascript - 防止将同一图像两次添加到数组中
- javascript - 获取 Ajax 发布请求中的进度百分比
- swift - 如何编码“让时间戳:日期”以获取 2018 年 5 月 10 日晚上 10:00
- ios - 如何在 UIMenuController 的指定位置添加自定义 menuItem?
- javascript - 获取对象内部对象的索引
- python - (Python 3) 输出问题
- ruby-on-rails - Rails 比较 2 个不同集合中的值
- php - 显示/隐藏自定义 WordPress 菜单