TextOut in macos?

Hi, does anyone have a project that outputs text? Unfortunately I coudn't find code for Swift 5 in macos, sorry.


I need a command like that:


TextOut(x:50, y:100, text: "Hello World!")

Accepted Reply

There is a func for this :

            text.draw(in: rect)



Instance Method draw(in:)


Draws the attributed string inside the specified bounding rectangle in the current graphics context.


Declaration

iOS, tvOS, watchOS
func draw(in rect: CGRect)
macOS
func draw(in rect: NSRect)

Parameters

rect

The bounding rectangle in which to draw the string. In AppKit, the origin is normally in the lower-left corner of the drawing area, but the origin is in the upper-left corner if the focused view is flipped.

Discussion

This method draws as much of the string as it can inside the specified rectangle, wrapping the string text as needed to make it fit. If the string is too long to fit inside the rectangle, the method renders as much as possible and clips the rest. It is possible for a portion of a glyph to appear outside the area of

rect
if the image bounding box of the particular glyph exceeds its typographic bounding box. Text is drawn according to its line sweep direction; for example, Arabic text begins at the right edge and is potentially clipped on the left.

Layout always occurs from top to bottom. AppKit automatically adjusts the initial drawing point whether or not the view is flipped. For example, if the

rect
argument is
{0.0, 0.0, 100.0, 100.0}
, the text origin is {0.0, 0.0} when the view coordinates are flipped and {0.0, 100.0} when the view is not flipped.

This method draws the line using the attributes specified in the attributed string itself. If newline characters are present in the string, those characters are honored and cause subsequent text to be placed on the next line underneath the starting point.

There must be either a focused view or an active graphics context when you call this method.



See Also

Related Documentation


draw(at:)
,
lockFocus()


Or with attributes

Instance Method draw(in:withAttributes:)


Draws the attributed string inside the specified bounding rectangle.



Language

SDKs
  • iOS 7.0+
  • macOS 10.0+
  • tvOS 9.0+
  • watchOS 2.0+


Declaration

iOS, tvOS, watchOS
func draw(in rect: CGRect, withAttributes attrs: [NSAttributedString.Key : Any]? = nil)
macOS
func draw(in rect: NSRect, withAttributes attrs: [NSAttributedString.Key : Any]? = nil)

Parameters

rect

The bounding rectangle in which to draw the string. In AppKit, the origin of the bounding box is normally in the lower-left corner, but the origin is in the upper-left corner if the focused view is flipped.

attrs

The text attributes with which to draw the string. These are the same attributes that can be applied to an

NSAttributedString
object, but in the case of
NSString
objects, the attributes apply to the entire string, rather than ranges within the string.

Discussion

This method draws as much of the string as it can inside the specified rectangle, wrapping the string text as needed to make it fit. If the string is too long to fit inside the rectangle, the method renders as much as possible and clips the rest.

If newline characters are present in the string, those characters are honored and cause subsequent text to be placed on the next line underneath the starting point.

There must be either a focused view or an active graphics context when you call this method.


Or draw(at:)

Instance Methoddraw(at:)


Draws the attributed string starting at the specified point in the current graphics context.



Language

  • Swift
  • Objective-C
SDKs
  • iOS 6.0+
  • macOS 10.0+
  • tvOS 9.0+
  • watchOS 2.0+


Declaration

iOS, tvOS, watchOS
func draw(at point: CGPoint)
macOS
func draw(at point: NSPoint)

Parameters

point

The point in the current graphics context where you want to start drawing the string. The coordinate system of the graphics context is usually defined by the view in which you are drawing.

Discussion

This method draws the entire string starting at the specified point. This method draws the line using the attributes specified in the attributed string itself. If newline characters are present in the string, those characters are honored and cause subsequent text to be placed on the next line underneath the starting point.

There must be either a focused view or an active graphics context when you call this method.



See Also

Related Documentation



draw(in:)
,
lockFocus()
,
size()


Replies

Output text to what ?


Could you explain more exactly what you want to achieve ?

I want to output text to the application window. I'd like to tell the function where to draw the text, and the string that is drawn.


And later I want to use this to create a tracker, so I want to make about 1000 calls to this function for text like "C-5", and it should not be much too laggy on a new mac.

That is not the way to do a user interface, even on Windows.

But that is what trackers do.

There is a func for this :

            text.draw(in: rect)



Instance Method draw(in:)


Draws the attributed string inside the specified bounding rectangle in the current graphics context.


Declaration

iOS, tvOS, watchOS
func draw(in rect: CGRect)
macOS
func draw(in rect: NSRect)

Parameters

rect

The bounding rectangle in which to draw the string. In AppKit, the origin is normally in the lower-left corner of the drawing area, but the origin is in the upper-left corner if the focused view is flipped.

Discussion

This method draws as much of the string as it can inside the specified rectangle, wrapping the string text as needed to make it fit. If the string is too long to fit inside the rectangle, the method renders as much as possible and clips the rest. It is possible for a portion of a glyph to appear outside the area of

rect
if the image bounding box of the particular glyph exceeds its typographic bounding box. Text is drawn according to its line sweep direction; for example, Arabic text begins at the right edge and is potentially clipped on the left.

Layout always occurs from top to bottom. AppKit automatically adjusts the initial drawing point whether or not the view is flipped. For example, if the

rect
argument is
{0.0, 0.0, 100.0, 100.0}
, the text origin is {0.0, 0.0} when the view coordinates are flipped and {0.0, 100.0} when the view is not flipped.

This method draws the line using the attributes specified in the attributed string itself. If newline characters are present in the string, those characters are honored and cause subsequent text to be placed on the next line underneath the starting point.

There must be either a focused view or an active graphics context when you call this method.



See Also

Related Documentation


draw(at:)
,
lockFocus()


Or with attributes

Instance Method draw(in:withAttributes:)


Draws the attributed string inside the specified bounding rectangle.



Language

SDKs
  • iOS 7.0+
  • macOS 10.0+
  • tvOS 9.0+
  • watchOS 2.0+


Declaration

iOS, tvOS, watchOS
func draw(in rect: CGRect, withAttributes attrs: [NSAttributedString.Key : Any]? = nil)
macOS
func draw(in rect: NSRect, withAttributes attrs: [NSAttributedString.Key : Any]? = nil)

Parameters

rect

The bounding rectangle in which to draw the string. In AppKit, the origin of the bounding box is normally in the lower-left corner, but the origin is in the upper-left corner if the focused view is flipped.

attrs

The text attributes with which to draw the string. These are the same attributes that can be applied to an

NSAttributedString
object, but in the case of
NSString
objects, the attributes apply to the entire string, rather than ranges within the string.

Discussion

This method draws as much of the string as it can inside the specified rectangle, wrapping the string text as needed to make it fit. If the string is too long to fit inside the rectangle, the method renders as much as possible and clips the rest.

If newline characters are present in the string, those characters are honored and cause subsequent text to be placed on the next line underneath the starting point.

There must be either a focused view or an active graphics context when you call this method.


Or draw(at:)

Instance Methoddraw(at:)


Draws the attributed string starting at the specified point in the current graphics context.



Language

  • Swift
  • Objective-C
SDKs
  • iOS 6.0+
  • macOS 10.0+
  • tvOS 9.0+
  • watchOS 2.0+


Declaration

iOS, tvOS, watchOS
func draw(at point: CGPoint)
macOS
func draw(at point: NSPoint)

Parameters

point

The point in the current graphics context where you want to start drawing the string. The coordinate system of the graphics context is usually defined by the view in which you are drawing.

Discussion

This method draws the entire string starting at the specified point. This method draws the line using the attributes specified in the attributed string itself. If newline characters are present in the string, those characters are honored and cause subsequent text to be placed on the next line underneath the starting point.

There must be either a focused view or an active graphics context when you call this method.



See Also

Related Documentation



draw(in:)
,
lockFocus()
,
size()


Works, thanks a lot! I'm first time Xcode user and had unchecked Storyboard for no good reason, just to have no "storyboard thing" 😊

View controller never was loaded, googled tutorials came in 5+ text snippets, so I went to the forum. Anyway, it looks really easy to use, yeah.

I don't know what a "tracker" is. The "draw" method described above, like the textOut method you mentioned, should only be used in very limited circumstances. Essentially, when you have some dynamic object to draw on screen like a diagram and you want to have some text in it too. But even in that case, there are almost certainly much better approaches to doing that kind of low-level drawing. Also, you mentioned making thousands of calls to this method and being concerned about it being "laggy". I'm afraid that betrays a fundamental misunderstanding about how the graphics drawing architecture works on a Mac. You are digging yourself a rabbit hole that you may not escape from.

Starting to get very mild lag at 4000 draw calls per update, I need only a few hundreds at most since I won't make it too colorful anyway, basically just selection blocks with different color. I love Xcode, even if Cocoa was slow, the amount of help from Xcode + you is amazing. Thanks for any help. If you know a library for fastest possible text drawing, let me know...


A tracker is music software that shows a lot of text most of the time. It's special. Anyways I was just too bad not to remove the Storyboard before trying to do something with view controllers.


Thanks guys... Me ❤ Xcode & Forums

Mac = My best investment since my C64...