nstextview

How to auto-expand height of NSTextView in SwiftUI?

落爺英雄遲暮 提交于 2020-08-10 19:11:25
问题 How do I properly implement NSView constraints on the NSTextView below so it interacts with SwiftUI .frame()? Goal An NSTextView that, upon new lines, expands its frame vertically to force a SwiftUI parent view to render again (i.e., expand a background panel that's under the text + push down other content in VStack). The parent view is already wrapped in a ScrollView. Since the SwiftUI TextEditor is ugly and under-featured, I'm guessing several others new to MacOS will wonder how to do the

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

How to pull NSImage from NSTextAttachment in NSTextView?

和自甴很熟 提交于 2020-03-06 09:12:55
问题 Goal is to allow user to add NSImage (s) to an NSAttributedString in an NSTextView , and then reverse the process and extract the image(s). With code from here, can add image(s) and have them displayed inline. let attributedText = NSMutableAttributedString(string: string, attributes: attributes as? [String : AnyObject]) let attachment = NSTextAttachment() let imageTest = NSImage(named:"sampleImage") as NSImage? let attachmentCell: NSTextAttachmentCell = NSTextAttachmentCell.init(imageCell:

How to pull NSImage from NSTextAttachment in NSTextView?

烈酒焚心 提交于 2020-03-06 09:09:25
问题 Goal is to allow user to add NSImage (s) to an NSAttributedString in an NSTextView , and then reverse the process and extract the image(s). With code from here, can add image(s) and have them displayed inline. let attributedText = NSMutableAttributedString(string: string, attributes: attributes as? [String : AnyObject]) let attachment = NSTextAttachment() let imageTest = NSImage(named:"sampleImage") as NSImage? let attachmentCell: NSTextAttachmentCell = NSTextAttachmentCell.init(imageCell:

CoreText 实现图文混排

自作多情 提交于 2020-02-28 16:42:28
NSTextView和Attribued String 第一次接触苹果系的富文本编程是在写Mac平台上的一个输入框的时候,输入框中的文字可以设置各种样式,并可以在文字中间插入图片,好在Mac的AppKit中提供了NSTextView这个支持富文本编辑器控件。此控件背后是通过什么方式来描述富文本的呢?答案是NSAttributedString,很多编程语言都提供了AttributedString的概念。NSAttributedString比NSString多了一个Attribute的概念,一个NSAttributedString的对象包含很多的属性,每一个属性都有其对应的字符区域,在这里是使用NSRange来进行描述的。下面是一个NSTextView显示富文本的例子 NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc] initWithString:@"测试富文本显示"] autorelease]; //为所有文本设置字体 [attributedString addAttribute:NSFontAttributeName value:[NSFont systemFontOfSize:24] range:NSMakeRange(0, [attributedString

Premature line wrapping in NSTextView when tabs are used

萝らか妹 提交于 2020-01-20 08:44:05
问题 I have a strange problem with NSTextView linewrapping after the 51st column if I enter a line of tabs. This only happens with tabs, not with any other character, which wrap correctly at the edge of the text view, not after the 51st character. This is easy to repeat. Create a blank project in XCode with a single window and just one NSTextView. The only non-default settings are that I have removed constraints, and used the old style autosize to autosize the textview so that it fills the window.