- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我试图在我的 NSTextView 子类中显示不可见字符,例如换行符。覆盖 NSLayoutManager 的 drawGlyph 方法这样的常用方法是一个坏主意,因为它太慢并且不能正确处理多页布局。
我想做的是重写 NSLayoutManager 的 setGlyph 方法,这样它将用“¶”字形替换不可见的“\n”字形,用“∙”替换“”。
它适用于 ""空格字形,但对换行符没有影响。
public override func setGlyphs(_ glyphs: UnsafePointer<CGGlyph>, properties props: UnsafePointer<NSGlyphProperty>, characterIndexes charIndexes: UnsafePointer<Int>, font aFont: Font, forGlyphRange glyphRange: NSRange) {
var substring = (self.currentTextStorage.string as NSString).substring(with: glyphRange)
// replace invisible characters with visible
if PreferencesManager.shared.shouldShowInvisibles == true {
substring = substring.replacingOccurrences(of: " ", with: "\u{00B7}")
substring = substring.replacingOccurrences(of: "\n", with: "u{00B6}")
}
// create a CFString
let stringRef = substring as CFString
let count = CFStringGetLength(stringRef)
// convert processed string to the C-pointer
let cfRange = CFRangeMake(0, count)
let fontRef = CTFontCreateWithName(aFont.fontName as CFString?, aFont.pointSize, nil)
let characters = UnsafeMutablePointer<UniChar>.allocate(capacity: MemoryLayout<UniChar>.size * count)
CFStringGetCharacters(stringRef, cfRange, characters)
// get glyphs for the pointer of characters
let glyphsRef = UnsafeMutablePointer<CGGlyph>.allocate(capacity: MemoryLayout<CGGlyph>.size * count)
CTFontGetGlyphsForCharacters(fontRef, characters, glyphsRef, count)
// set those glyphs
super.setGlyphs(glyphsRef, properties:props, characterIndexes: charIndexes, font: aFont, forGlyphRange: glyphRange)
}
然后我想出了一个主意:看起来 NSTypesetter 标记了新的行字符范围,就像它根本不应该处理的那样。所以我继承了 NSTypesetter 并覆盖了一个方法:
override func setNotShownAttribute(_ flag: Bool, forGlyphRange glyphRange: NSRange) {
let theFlag = PreferencesManager.shared.shouldShowInvisibles == true ? false : true
super.setNotShownAttribute(theFlag, forGlyphRange: glyphRange)
}
但它不起作用。无论我创建什么字形,NSLayoutManager 仍然不会为换行符生成字形。
我做错了什么?
最佳答案
正如我发现的那样,NSTypesetter 的 setNotShownAttribute: 类的默认实现不会更改其字形存储中已经生成的字形。因此,调用 super 不会产生任何效果。我只需要在调用 super 之前手动替换字形。
因此,显示不可见字符的最有效实现(缩放 View 时您会看到差异)是这样的:
这种方法的局限性:如果您的应用必须在 TextView 中使用多种字体,那么这种方法可能不是一个好主意,因为那些显示的不可见字符的字体会有所不同出色地。这不是您想要实现的目标。
继承 NSLayoutManager 并覆盖 setGlyphs 以显示空格字符:
public override func setGlyphs(_ glyphs: UnsafePointer<CGGlyph>, properties props: UnsafePointer<NSGlyphProperty>, characterIndexes charIndexes: UnsafePointer<Int>, font aFont: Font, forGlyphRange glyphRange: NSRange) {
var substring = (self.currentTextStorage.string as NSString).substring(with: glyphRange)
// replace invisible characters with visible
if PreferencesManager.shared.shouldShowInvisibles == true {
substring = substring.replacingOccurrences(of: " ", with: "\u{00B7}")
}
// create a CFString
let stringRef = substring as CFString
let count = CFStringGetLength(stringRef)
// convert processed string to the C-pointer
let cfRange = CFRangeMake(0, count)
let fontRef = CTFontCreateWithName(aFont.fontName as CFString?, aFont.pointSize, nil)
let characters = UnsafeMutablePointer<UniChar>.allocate(capacity: MemoryLayout<UniChar>.size * count)
CFStringGetCharacters(stringRef, cfRange, characters)
// get glyphs for the pointer of characters
let glyphsRef = UnsafeMutablePointer<CGGlyph>.allocate(capacity: MemoryLayout<CGGlyph>.size * count)
CTFontGetGlyphsForCharacters(fontRef, characters, glyphsRef, count)
// set those glyphs
super.setGlyphs(glyphsRef, properties:props, characterIndexes: charIndexes, font: aFont, forGlyphRange: glyphRange)
}
子类 NSATSTypesetter 并将其分配给您的 NSLayoutManager 子类。子类将显示新行字符并确保每个不可见字符都将以不同的颜色绘制:
class CustomTypesetter: NSATSTypesetter {
override func setNotShownAttribute(_ flag: Bool, forGlyphRange glyphRange: NSRange) {
var theFlag = flag
if PreferencesManager.shared.shouldShowInvisibles == true {
theFlag = false
// add new line glyphs into the glyph storage
var newLineGlyph = yourFont.glyph(withName: "paragraph")
self.substituteGlyphs(in: glyphRange, withGlyphs: &newLineGlyph)
// draw new line char with different color
self.layoutManager?.addTemporaryAttribute(NSForegroundColorAttributeName, value: NSColor.invisibleTextColor, forCharacterRange: glyphRange)
}
super.setNotShownAttribute(theFlag, forGlyphRange: glyphRange)
}
/// Currently hadn't found any faster way to draw space glyphs with different color
override func setParagraphGlyphRange(_ paragraphRange: NSRange, separatorGlyphRange paragraphSeparatorRange: NSRange) {
super.setParagraphGlyphRange(paragraphRange, separatorGlyphRange: paragraphSeparatorRange)
guard PreferencesManager.shared.shouldShowInvisibles == true else { return }
if let substring = (self.layoutManager?.textStorage?.string as NSString?)?.substring(with: paragraphRange) {
let expression = try? NSRegularExpression.init(pattern: "\\s", options: NSRegularExpression.Options.useUnicodeWordBoundaries)
let sunstringRange = NSRange(location: 0, length: substring.characters.count)
if let matches = expression?.matches(in: substring, options: NSRegularExpression.MatchingOptions.withoutAnchoringBounds, range: sunstringRange) {
for match in matches {
let globalSubRange = NSRange(location: paragraphRange.location + match.range.location, length: 1)
self.layoutManager?.addTemporaryAttribute(NSForegroundColorAttributeName, value: Color.invisibleText, forCharacterRange: globalSubRange)
}
}
}
}
}
要显示/隐藏不可见字符,只需调用:
let storageRange = NSRange(location: 0, length: currentTextStorage.length)
layoutManager.invalidateGlyphs(forCharacterRange: storageRange, changeInLength: 0, actualCharacterRange: nil)
layoutManager.ensureGlyphs(forGlyphRange: storageRange)
关于swift - 无论我做什么,NSLayoutManager 都会隐藏换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39545718/
默认布局管理器在没有文本(最后一行除外)的地方填充背景颜色(通过 NSAttributedString .backgroundColor 属性指定)。 我已经通过子类化 NSLayoutManager
即使 iOS 文档说: NLayoutManager, NSTextStorage, and NSTextContainer can be accessed from subthreads as lo
在此线程中,Core Text calculate letter frame in iOS ,他们能够使用 Core Text 非常精确地计算每个字形的框架。最终的矩形完美地拥抱了实际绘制的字形。 使
我正在尝试使用以下方法在给定恒定宽度的情况下测量 NSAttributedString 的高度: -(CGFloat)calculateHeightForAttributedString:(NSAtt
在我的 cocoa 应用程序(用 Swift 编写)中,我试图创建一个分页系统,其中每个页面都是 CASTextView(NSTextView 的子类)的一个实例。此外,每个 CASTextView
将 setAllowsNonContigouslyLayout 设置为 YES 时,如果我执行以下步骤,我会得到奇怪的行为: 选择 NSTextView 中的所有文本 按退格键删除所有文本 输入几行文
我有一个自定义 NSLayoutManager我用来绘制药丸形标记的子类。我为具有自定义属性( TokenAttribute )的子字符串绘制这些标记。我可以画没有问题。 但是,我需要使用我的 Tok
我正在尝试获取 NSTextContainer 中所选字符的实际点: func handleTouch(gestureRecognizer: UIGestureRecognizer) { var
如何获取 NSLayoutManager 中的字形总数? 我正在制作一个光标层以覆盖在自定义 TextView 上,如下所示: func moveCursorToGlyphIndex(glyphInd
我有一个 NSLayoutManager,它使用以下代码绘制文本: [[self textLayoutManager] drawGlyphsForGlyphRange: NSMakeRange(0,
我正在开发一个 View ,它使用 TextKit 框架在列中排版文本,如下所示: 我使用带有边缘插入(黑色矩形)的 UIView 边界来计算 10 个 CGRects,然后我将其转换为 NSText
我如何制作一个 NSLayoutManager 将其文本中的每个段落分配给不同的 NSTextContainer? 这类似于 TextKit 中常见的多页/多列用法,但每个页面对应一个可变长度的不同段
我试图在我的 NSTextView 子类中显示不可见字符,例如换行符。覆盖 NSLayoutManager 的 drawGlyph 方法这样的常用方法是一个坏主意,因为它太慢并且不能正确处理多页布局。
我正在使用 NSAttributedString 的 drawInRect(rect: CGRect) 方法显示可能包含表情符号的文本。因为我想检测文本上的点击,所以我使用以下方法查看点击了哪个字符:
我想在 UITextView 中“突出显示”特定的单词,而不是使用纯色(可以通过 NSAttributedString),而是使用渐变和其他一些奇特的效果。 因此,我决定有必要手动创建一个 View
在 WWDC 2013 的第 220 节课(高级文本布局和文本工具包效果)中他们特别说NSLayoutManager可以与 NSTextStorage 和 NSTextContainer 结合使用来创
给定任何任意的单行字符串,我的目标是将其渲染为位图表示形式。但是,我无法事先找出它的尺寸,因此我只能获取字形范围的边界矩形,并在 Canvas 不够大时调整 Canvas 的大小。不幸的是,如果 Ca
我正在使用 NSTextView 并为其 -textStorage 属性设置了委托(delegate)。收到 -textStorageDidProcessEditing: 后,我需要将属性应用于文本的
m_LayoutManager = [[NSLayoutManager alloc] init]; m_TextContainer = [[NSTextContainer alloc] init];
在我的应用程序中,我在UITextView中的文本行下绘制自定义背景。为此,我使用boundingRect(forGlyphRange:in:)的NSLayoutManager方法。它适用于LTR语言
我是一名优秀的程序员,十分优秀!