nslayoutmanager

NSLayoutManager returns incorrect character index when tapping on LTR text in RTL attributed text

僤鯓⒐⒋嵵緔 提交于 2021-01-28 08:51:58
问题 I wrote code to detect if the user taps within a specific range in UILabel . It seems to be working in most cases, but I have noticed that tapping the last line of an attributed text creates inconsistent results. public static bool DidTapRange(this UITapGestureRecognizer recognizer, UILabel label, NSRange range) { using (var ts = new NSTextStorage()) { var lm = new NSLayoutManager(); var tc = new NSTextContainer(new CGSize(label.Frame.Width, double.MaxValue)); lm.AddTextContainer(tc); ts

NSLayoutManager returns incorrect glyphIndex for mousePosition in NSTextView

半世苍凉 提交于 2020-08-09 08:08:37
问题 I have an NSTextView subclass as the right item in an NSSplitViewController , the left panel is an NSOutlineView . To process mouse clicks while the command key is pressed in the text view, I added the following to find the glyph that is under the mouse: override func mouseDown(with event: NSEvent) { guard let lm = self.layoutManager, let tc = self.textContainer else { return } let localMousePosition = convert(event.locationInWindow, to: nil) var partial = CGFloat(1.0) let glyphIndex = lm

Hide Markdown Characters with NSLayoutManager in Swift

橙三吉。 提交于 2020-08-01 09:05:10
问题 I am working on a rich text editor in a Mac app that uses Markdown syntax. I use NSTextStorage to watch for matches in Markdown syntax, then apply styles to the NSAttributedString in real time like this: At this point, I'm already in way over my head on this stuff, but I'm excited to be making progress. :) This tutorial was very helpful. As a next step, I want to hide the Markdown characters when the NSTextView 's string is rendered. So in the example above, once the last asterisk is typed, I

Control spacing around custom text attributes in NSLayoutManager

痴心易碎 提交于 2020-06-09 12:50:47
问题 I’ve got a custom NSLayoutManager subclass I’m using to draw pill-shaped tokens. I draw these tokens for substrings with a custom attribute ( TokenAttribute ). I can draw no problem. However, I need to add a little bit of “padding” around the ranges with my TokenAttribute (so that the round rectangle background of the token won’t intersect with the text). In the above image, I’m drawing my token’s background with an orange colour, but I want extra padding around 469 so the background isn’t

NSLayoutManager with multiple NSTextContainers causes UITextViews to not be selectable/editable

五迷三道 提交于 2020-05-10 20:56:11
问题 I am trying to achieve a multi page text editing layout, as in Pages, MS Word, ... . On OS X I can achieve this by creating one NSLayoutManager with one NSTextStorage for multiple NSTextViews. Each NSTextView has its own NSTextContainer. See below code for OS X. A simple example with text spreading between two NSTextViews: import Cocoa class ViewController: NSViewController { let layoutManager = NSLayoutManager() let textStorage = NSTextStorage(attributedString: NSAttributedString(string: