Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:31:42+00:00 2026-05-23T14:31:42+00:00

I am currently writing many chunks of text on an NSView using the NSString

  • 0

I am currently writing many chunks of text on an NSView using the NSString helper method … however it is very slow to write a large amount of in many cases repeated text. I am trying to re-write the code so that the text is converted to NSBezierPath’s that are generated once, then drawn many times. The following will draw text at the bottom of the screen.

I am still trying to read through the apple documentation to understand how this works, but in the mean time I wonder if there is an easy way to alter this code to re-draw the path in multiple locations?

// Write a path to the view
NSBezierPath* path = [self bezierPathFromText: @"Hello world!" maxWidth: width];
[[NSColor grayColor] setFill];
[path fill];

Here is the method that writes some text into a path:

-(NSBezierPath*) bezierPathFromText: (NSString*) text maxWidth: (float) maxWidth {

// Create a container describing the shape of the text area,
// for testing done use the whole width of the NSView.
NSTextContainer* container = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(maxWidth - maxWidth/4, 60)];

// Create a storage object to hold an attributed version of the string to display
NSFont* font = [NSFont fontWithName:@"Helvetica" size: 26];
NSDictionary* attr = [NSDictionary dictionaryWithObjectsAndKeys: font, NSFontAttributeName, nil];
NSTextStorage* storage = [[NSTextStorage alloc] initWithString: text attributes: attr];

// Create a layout manager responsible for writing the text to the NSView
NSLayoutManager* layoutManger = [[NSLayoutManager alloc] init];
[layoutManger addTextContainer: container];
[layoutManger setTextStorage: storage];

NSRange glyphRange = [layoutManger glyphRangeForTextContainer: container];
NSGlyph glyphArray[glyphRange.length];
NSUInteger glyphCount = [layoutManger getGlyphs:glyphArray range:glyphRange];

NSBezierPath* path = [[NSBezierPath alloc] init];
//NSBezierPath *path = [NSBezierPath bezierPathWithRect:NSMakeRect(0, 0, 30, 30)];
[path moveToPoint: NSMakePoint(0, 7)]; 
[path appendBezierPathWithGlyphs:glyphArray count: glyphCount inFont:font];

// Deallocate unused objects
[layoutManger release];
[storage release];
[container release];

return [path autorelease];
}

Edit: I am attempting to optimise an application that outputs to screen, a sequence of large amounts text such as a sequence of 10,000 numbers. Each number has markings around it and/or differing amounts of space between them, and some numbers have dots and/or lines above, below or between them. Its like the example at the top of page two of this document but with much much more output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:31:43+00:00Added an answer on May 23, 2026 at 2:31 pm

    It appears this might do the trick, however I am not sure if it is the best way to do it?

    // Create the path
    NSBezierPath* path = [self bezierPathFromText: @"Fish are fun to watch in a fish tank, but not fun to eat, or something like that." maxWidth: width];
    
    // Draw a copy of it at a transformed (moved) location
    NSAffineTransform* transform = [[NSAffineTransform alloc] init];
    [transform translateXBy: 10 yBy: 10];
    NSBezierPath* path2 = [path copy];
    [path2 transformUsingAffineTransform: transform];
    [[NSColor greenColor] setFill];
    [path2 fill];
    [path2 release];
    [transform release];
    [path2 release];
    
    // Draw another copy of it at a transformed (moved) location
    transform = [[NSAffineTransform alloc] init];
    [transform translateXBy: 10 yBy: 40];
    path2 = [path copy];
    [path2 transformUsingAffineTransform: transform];
    [[NSColor greenColor] setFill];
    [path2 fill];
    [path2 release];
    [transform release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently writing my bachelor thesis with latex and using TexnicCenter. I want to
I am currently writing a multi-threaded C++ server using Poco and am now at
I'm currently writing my master thesis and I'm using TortoiseSVN to keep a backup
I'm currently writing a fairly complex paint method for a user control, involving a
I'm writing a game for Mac using Cocoa. I'm currently implementing hit testing and
I am currently writing a calculator application. I am trying to write a derivative
I'm currently analyzing the pros and cons of writing a server using a threaded
Our company is currently writing a GUI automation testing tool for compact framework applications.
I am currently writing a simple, timer-based mini app in C# that performs an
I am currently writing a small calendar in ASP.Net C#. Currently to produce the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.