How to add Text to an NSView on MacOS in Swift - swift

Wondering what the correct way is to add non-editable text to an NSView for MacOS using Swift. I am not sure if I am supposed to draw it like this, or if there is another way. I am used to HTML where you just set the textContent property on an element/view and that's it. Not sure if it should be at the level of the canvas/cgrect or UITextView or what.
Basically I would like to add both button labels (small amounts of text) and paragraphs of text (large amounts) using the same approach if possible, using something that has good performance.

Related

How can I dynamically detect links/phone numbers/addresses etc in a SwiftUI Text

I am using SwiftUI and would like to have something similar to .dataDetectorTypes in a UITextView to dynamically display content and allow links or phone numbers or addresses to be clickable in a SwiftUI Text or TextEditor.
As this will be for a chat app the content is dynamic and so just using .onTap won't work. Ideally I would like to use something like textView.dataDetectorTypes = .all in either a Text or TextEditor in SwiftUI but it seems that is not available.
Currently I'm using a UITextView Representable but having all sorts of issues there with dynamic height adjustment and scroll performance so hoping to find a SwiftUI way of doing this without UIViewRepresentable.
This is not possible (as of the current version of swiftUI) you will need to continue to use UITextView for having a dynamic height have you looked at this post it might help with your height issues (maybe)...

What is the best way to decorate fragments of text in SwiftUI?

I need to insert custom views into Text in SwiftUI. In other words to decorate some fragments of text, like formula field in Numbers app.
I still novice in swiftUI and Xcode, so just can't figure out how to begin and google it. 🤷🏻‍♂️
I would be grateful for any help in pointing out areas for further deepening. I suppose, it's about UIKit…
UPDATE
What kind of UI element I have to use to achieve result like on picture: textfield, which may contain another intractable UI elements?
I want some words become buttons, other have image on background.
I won't give you a full answer here as it seems that you're the kind of developer who'd like to figure this out for yourself. (Please, correct me if I am mistaken in assuming this.)
The gist is that the SwiftUI way of doing it is probably still not nearly as simple to do as the UIKit way.
In UIKit, you're likely looking at creating a wrapper view or subclass of a UITextView which will manage its subviews, handle touch and text input events, and update the cursor of the UITextView.
LinkedIn's Hakawai sounds like something you could consider using to power this...
You can then expose this to SwiftUI via the UIViewRepresentable wrapper type.

How to insert and delete image and text in one view just like MMS apps on iPhone

I would like to implement something like MMS app on iPhone, where user will have an option to include an image and text.
The way I did is i created a UIUiew and included a UITextView and UIImageView inside it, and when user selects an image it goes in to the UIImageView and the user can type in the UITextView.
But the problem that I am facing is once you insert a UIImage how do you put a cursor after the image so that user can delete(backspace) the image and that space free's up and it becomes available for the user to type in more text.
Simple answer: You cannot mix text and images in a UITextView. If you want to achieve a behavior like this, you should look into using Core Text to render the text yourself (it supports other elements inside the text via certain callbacks through which you tell the text system how much space it is supposed to reserve for the image.
To make the text editable, you would have to implement the text editing protocols UITextInput and UIKeyInput. Which means you have to basically write your own text editor. It's possible but it's a lot of work.

Cocoa Touch text view with aligned, justified text

I am trying to create a textview with predefined height that will contain justified text.
When entering the text into the view, I need to be able to check when the view is full.
How would I do this? Core Text?
I am using sdk-3.2.
You cannot do it using UITextView (that would be the most "logical" way to do it), or any others SDK current classes.
The only simple way to do it if to put the content to be justified in an html file, justify it using CSS, and show it in a UIWebView.

Can't display selected text with different colors in UITextView, what should I do?

I want to display texts in UITextView with different colors and underline some of them (to show they are erroneous - commonly done in spellchecker) but that's not possible with the current version of UIKit. What other options should I pursue?
I am thinking of subclassing UIScrollView (as I am dealing with a lot of texts) and blit the text into a UIView (as the content) using CoreGraphics, and as new text is entered, I will blit new text onto it. Is that a good approach?
The recommended approach is to generate HTML and use a UIWebView. That is a scroll view so you should have all the functionality you need.
If you want more control over the text then your approach is good although you can probably use the UIKit NSString extensions to draw your text. Specifically, you want drawAtPoint:withFont: