【发布时间】:2020-02-25 11:48:49
【问题描述】:
我正在尝试创建一个自定义的 NSTextBlock,就像 Apple 在 WWDC 18 (23 mins in) 所做的那样。
好的,所以当我使用带有文本块的段落样式编辑和标记段落时,它非常有用。
但是当我剪切并粘贴它(或从磁盘归档/取消归档)时,它会丢失。编辑:它实际上将我的TweetTextBlock 子类变成了NSTableViewTextBlock,这也解释了边界。
实施
Here's a full Xcode project。使用Format 顶部菜单项触发markTweet 功能。
这是我将属性添加到段落的方法
@IBAction func markTweet(_ sender : Any?){
print("now we are marking")
let location = textView.selectedRange().location
guard let nsRange = textView.string.extractRange(by: .byParagraphs, at: location) else { print("Not in a paragraph"); return }
let substring = (textView.string as NSString).substring(with: nsRange)
let tweetParagraph = NSMutableParagraphStyle()
tweetParagraph.textBlocks = [TweetTextBlock()]
let twitterAttributes : [AttKey : Any] = [
AttKey.paragraphStyle : tweetParagraph,
AttKey.font : NSFont(name: "HelveticaNeue", size: 15)
]
textView.textStorage?.addAttributes(twitterAttributes, range: nsRange)
}
这是我的NSTextBlock 子类
import Cocoa
class TweetTextBlock: NSTextBlock {
override init() {
super.init()
setWidth(33.0, type: .absoluteValueType, for: .padding)
setWidth(70.0, type: .absoluteValueType, for: .padding, edge: .minX)
setValue(100, type: .absoluteValueType, for: .minimumHeight)
setValue(300, type: .absoluteValueType, for: .width)
setValue(590, type: .absoluteValueType, for: .maximumWidth)
backgroundColor = NSColor(white: 0.97, alpha: 1.0)
}
override func drawBackground(withFrame frameRect: NSRect, in controlView: NSView,
characterRange charRange: NSRange, layoutManager: NSLayoutManager) {
let frame = frameRect
let fo = frameRect.origin
super.drawBackground(withFrame: frame, in: controlView, characterRange:
charRange, layoutManager: layoutManager)
// draw string
let context = NSGraphicsContext.current
context?.shouldAntialias = true
let drawPoint: NSPoint = CGPoint(x: fo.x + 70, y: fo.y + 10)
let nameAttributes = [AttKey.font: NSFont(name: "HelveticaNeue-Bold", size: 15), .foregroundColor: NSColor.black]
var handleAttributes = [AttKey.font: NSFont(name: "HelveticaNeue", size: 15), .foregroundColor: NSColor(red: 0.3936756253, green: 0.4656872749, blue: 0.5323709249, alpha: 1)]
let nameAStr = NSMutableAttributedString(string: "Johanna Appleseed", attributes: nameAttributes)
let handleAStr = NSAttributedString(string: " @johappleseed · 3h", attributes: handleAttributes)
nameAStr.append(handleAStr)
nameAStr.draw(at: drawPoint)
let im = NSImage(named: "profile-twitter")!
im.draw(in: NSRect(x: fo.x + 10, y: fo.y + 10, width: 50, height: 50))
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
我尝试了什么
我的想法是,这可能会发生,因为 TextKit 不知道如何归档自定义块中的属性。但我尝试覆盖init:fromCoder 和encode。他们不会被召唤。不适用于复制、粘贴、归档、取消归档。所以我想不是这样。这使我认为所有这些自定义绘图逻辑都不能保存在属性字符串中,而这一切都发生在布局管理器中。这就说得通了。但是我该如何坚持这个块呢?
更新:我尝试读取属性。它有一个段落样式,并且该段落样式在textBlocks 数组属性中有一个项目。但是那个文本块是NSTextBlock 而不是我的子类(我试过if block is TweetTextBlock 返回false)
更新 2:我尝试覆盖 classForArchiver 之类的属性,然后使用例如读取它们print("twb: Class for archiver", block.classForArchiver)。这里有趣的是,文本块已经变成了NSTextTableBlock!我现在非常热衷于破解这个问题,我正在寻找一种将 className somewhere 存储在文本块中的方法。到目前为止,我唯一能想到的是tooltip 属性,但它对用户是可见的,我可能想将它用于其他用途。
更新 3:工具提示也未保留。这很奇怪。我能想到的下一个大技巧是将文本颜色设置为 HSB (n, 0, 0),其中 n 是 NSTextBlock 子类的标识符。希望我不必去那里。
更新 4. 这很可能是由于存档和复制/粘贴将字符串转换为 RTF 造成的。这是我剪贴板中的public.rtf
{\rtf1\ansi\ansicpg1252\cocoartf2509
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fnil\fcharset0 HelveticaNeue;}
{\colortbl;\red255\green255\blue255;\red245\green245\blue245;}
{\*\expandedcolortbl;;\csgray\c97000;}
\pard\intbl\itap1\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0
\f0\fs30 \cf0 THIS text is in a TweetTextBlock}
【问题讨论】:
-
感谢您发布调试步骤。你最终找到解决方案了吗?
标签: swift nsattributedstring nstextview textkit nslayoutmanager