Getting formatted string from NSTextView - swift

I am creating an OSX app with NSTextView inside NSScrollView. Using the FormatMenuItem I can modify the text in that NSTextView to Bold, italic etc. But when I get the values using (NSTextViewOutlet.String) I cannot get the modified string, that is the string contain bold and italic characters.

By using below code solve my problem.and i use NSTextField instead of NSTextView.
let str = txt_outlet.attributedStringValue

Related

SwiftUI Text ignoring newline and tab characters

I am attempting to render a multiline text block in SwiftUI using the Text component. I'm passing it a string that contains newlines and tabs, however whenever it is rendered, all of these special characters appear to be removed from text and the content is all rendered in a straight line. Does anyone have any ideas how to convince SwiftUI to actually let this be multiple lines?
let s = "{\n\t\"hey\":\"there\"\n\n\n}"
Text(.init("`\(s)`"))
The above code renders as:
From the docs modify your code accordingly.
Ex.
Text(“In SwiftUI Multiline text is great. \n Right?”)
.lineLimit(nil)
this works for me:
let s = "{\n\t\"hey\":\"there\"\n\n\n}"
Text("`\(s)`")
or
Text("\(s)")
or
Text(s)

Assign attributed text to tooltip in Swift

I am trying to make a tooltip with bold text. I figured NSAttributedString can be used make a string bold. However tooltip only accepts string type. Is there a way to make tooltip with bold font.
Here a sample of what I am trying to achieve(from Xcode):

Updating Attributed String Without Losing Previous Formatting

For the first time, I am working with attributed strings so please bear with me!
I have spent a lot of time on SO looking at various attributed string answers but I'm still not convinced I am doing the right thing and would appreciate any advice.
I have created an NSMutableAttributed String (var attributedText) and a simple UIView with a TextView and 2 buttons which should in theory turn any part of highlighted text in the UITextView to bold or italic.
In ViewDidLoad, I have simply added some dummy text and added a couple of attributes to the text such as line spacing and font color. When the view appears all looks as expected.
If I highlight a portion of text and tap the bold button, it returns an attribute and adds it to attributedText and then to the textView (textView.attributedText = attributedText).
The following is my return function:
func boldText() -> Dictionary<NSAttributedString.Key, Any> {
let attributes: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 15, weight: .bold),
]
return attributes
}
Now, here's where things get confusing. If I manually type some text in the UTextView and highlight a portion of that text and tap the Bold button, the app will crash due to the text being out of range, this makes sense as my attributedText variable is only holding the previous dummy text I added in ViewDidLoad and not the new text I have typed.
So my question is, how do I add the new text I typed to the attributedText variable without messing up all the other formatting that was already part of that text?
If I just add the textView text to the variable:
attributedText = NSMutableAttributedString(string: "\(textView.text!)" )
Then obviously it simply overwrites anything that was held in the attributedText variable previously which in turn removes all formatting.
Am I on the right lines in the fact that every time change happens in the textView I need to somehow update my attributedText variable or should I be using a different approach altogether?

Howto change from proportional to tabulated digits in menu items?

I want to change from proportional to tabulated digits in menu items? It's possible?
Menu without tabulated digits
You need to use the monospaced font like this:
label.font = UIFont.monospacedDigitSystemFont(ofSize: 17, weight: UIFontWeightRegular)
Obviously you can specify whatever font size and weight you want (or get them from the current font descriptor).
You can set the attributedTitle property of a NSMenuItem and provide whatever custom alignment or formatting you want through an NSAttributedString
Good examples of how to do string alignment in an attributed string can be seen here:
Attributed Text Center Alignment
Here is an answer that shows how to add tab stops to an attributed string:
How do I add tab stops to an NSAttributedString
You can even create an attributed string from RTF or HTML:
Creating an NSAttributedString from HTML (or RTF)

how to apply 2 text styles to a label in iphone

i have a tableview. in cell i am displaying some string data from xml.
in that string some italic tags and some normal tags r there.
but when i try to display the entire string it is displayed in normal text and not getting italic text.
i tried by taking 2 labels one with italic text and another with normal text and appending these 2 labels but i am getting entire string as italic.
how can we apply 2 styles in a label
Thanks in advance
Currently, the UILabel control does not support "rich text." You cannot have some text italic and some normal in the same UILabel.
Three20 has a label that supports this, however:
http://api.three20.info/interface_t_t_styled_text_label.php