append a colored text to another text on macOS - macos

I am developing an application in Cocoa.
I need to append a colored text to another text in table view, Is there a any way to do this?

Take a look at NSAttributedString, it will let you do exactly this.

Related

Support "styled text" in a scriptable Mac application (Cocoa Scripting)

My app supports being scripted with Applescript.
I am trying to make styled text content, stored in NSAttributedString objects, available to an Applescript user.
I thought I could simply deliver styled text with the NSAttributedString class, just like I deliver plain text with the NSString class, but that does not work - Cocoa Scripting then reports that it cannot convert or coerce the data.
I wonder if I'm missing something or if this is just plain impossible with the standard classes supported by Cocoa Scripting?
AppleScript does know the "styled text" type, as seen in this example:
set stxt to "foo" as styled text
So, if AppleScript knows this type by default, shouldn't the Cocoa Scripting engine support it as well somehow?
As always there are many choices for solving an AS problem.
In my scriptable text editor (Ted), I implemented the Text Suite, which is based on rich text (NSTextStorage, a subclass of NSMutableAttributedString). I wanted to be able to script tabs in my paragraphs, so I added a style record, which contains all the paragraph style information. This lets me write scripts like this:
tell application "Ted"
set doc1 to make new document at beginning with properties {name:"Document One"}
tell doc1
set p1 to make new paragraph at end with data "Paragraph One" with properties {size:24, color:maraschino}
set p2 to make new paragraph at end with data "Paragraph Two" with properties {style:style of paragraph 1}
set color of paragraph 1 to blue
end tell
set doc2 to make new document at beginning with properties {name:"Document Two"}
copy p1 to beginning of doc2
properties of paragraph 1 of doc2
end tell
Since p1 is rich text, the second document ends up with both the text and formatting of the first paragraph of the first document.
You can also ask for the properties of a piece of text, where I have implemented the usual Text Suite properties, as well as a "style" property for paragraph style (backed by NSParagraphStyle, since I wanted to be able to script the tab stops):
properties of paragraph 1 of doc2
Result:
{height:60.0, italic:false, size:24, style:{paragraph spacing after:0.0, head indent:0.0, line break mode:0, alignment:4, line spacing:0.0, minimum line height:0.0, first line head indent:0.0, paragraph spacing before:0.0, tabs:{"28L", "56L", "84L", "112L", "140L", "168L", "196L", "224L", "252L", "280L", "308L", "336L"}, tail indent:0.0, maximum line height:0.0, line height multiple:0.0, default tab interval:0.0}, color:blue, width:164.109375, font:"Helvetica", bold:false, class:attribute run}
This works well for passing rich text within my application, but may not be as useful for passing styled text to other applications, which may be what you wanted to do. I think adding a "style" property (of type record) is probably the best way to convey style info for use in other scriptable apps. Then in the second app, the scripter can make use of any properties in the style record that the second app understands.
It looks like there is no implicit support for styled text in AppleScript. And there is also no common interchange record type for passing styled text.
AppleScript was developed in the pre-OSX days when styled text was often represented by a combination of a plain text (in System or MacRoman encoding) and a styl resource. With Unicode came an alternative format of a ustl style format. These are still used with the Carbon Pasteboard API (PasteboardCreate etc.) today. Yet, none of these seem to have made it into the use with AppleScript.
The fact that AppleScript knows of a styled text type has no special meaning. Even its class is just text.
Update
I just found that Matt Neuburg's book "AppleScript The Definitive Guide" mentions styled text and gives an example where it's indeed showing a record containing both the plain text (class ktxt) and style data (class ksty) with data of type styl, just as I had expected above. He also points out that most applications don't use that format, though.
So, it appears using a record with style resource data is indeed the intended way, only that hardly anyone knows about it. Go figure.

how to align the text in the text box in Xcode?

I don't know how to align the text in the text box. I need to change the font size, font type in the text box. Is anybody know that means send me the answer?
Its rather simple.
Follow these steps and you will be done with what you need.
In your storyboard, select your text box and then open up the Interface Builder.
In the Interface Builder,you will see properties for "Alignment","Text Size" and Color.
Make appropriate changes there and your done.
Hope this helps.

Is it possible to set color of texts in dialog boxes of Applescript?

Is there any way that the text's color inside a dialog box of Apple script can be changed???
Please reply
Thanks in advance.
No, there is not a way to change the text color in a AppleScript dialog box. However, you can change other aspects such as displaying an icon, making the text field display the letters as bullets (for things like passwords), etc. See AppleScript Wiki for more info.

Display Emoji icons in Mac app

I want the user to have easy access to Emoji icons in a small chat I am making for the company I'm working at.
Therefore, I want to make a panel which shows the available Emoji icons. To do this, I am using an NSCollectionView. Now, I need to pass a string which is shown in an NSTextField. This string must contain one Emoji icon.
I can't figure out how to write this Emoji icon in unicode in a string. Everything I have tried just shows a strange symbol instead of the icon.
Can anyone tell me, how I can put an Emoji icon into an NSString?
📱 You want emoji 😄?
🐷🐻🐨🐸 They're in Unicode now 🐸🐨🐻🐷
If you open up the 💻Character Viewer💻 you can actually paste them directly into your source code. If you'd rather, you can encode them in hexadecimal (\U0001F431 for cat face) by looking them up in the Unicode code charts (especially the chart for the U+1F300 block).
If you want to make sure they appear in color, you'll need to select the "Apple Color Emoji" font for the Emoji. You can use NSAttributedString to set the font on a per-character basis.

line spacing NSTextView

If you install Microsoft word on Mac and open a document, you can increase line spacing to 2 by hitting Command + 2. Now my cocoa app uses NSTextView. And my users want a similar functionality in the app. I looked at the NSTextView apis and could not find any which will help me here. Is this something that can be accomplished using the defaults write command may be ?.
Text Views are views to display their model, NSTextStorage. NSTextStorage is a subclass of NSAttributedString. You need to modify the attributes of the text for custom line spacing.

Resources