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

  • SEARCH
  • Home
  • 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 8092993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:24:21+00:00 2026-06-05T20:24:21+00:00

I am trying to create an NSTextView that behaves somewhat like a series of

  • 0

I am trying to create an NSTextView that behaves somewhat like a series of typewriter pages — each page has a fixed number of rows and columns available.

The underlying representation ideally match the display, which in turn matches the file format. Pages end with a form-feed character '\f'. For example, two 4×4 pages would look like this when saved (spaces between characters for readability):

T  H  I  S  \n
P  A  G  E  \n
I  S  \n
F  U  L  L  \n
\f
N  O  T  \n
T  H  I  S  \n
O  N  E  \n
\n
\f

A few things to notice:

  • Newlines and form-feeds don’t count in the row/column total
  • Lines shorter than the column count are not padded with trailing whitespace (though they could be if the editor worked in overwrite mode)
  • Pages shorter than the row count are padded with newlines

I tried a few approaches, like converting the text to a NSArray of lines and splitting long lines on every textDidChange notification, then setting the text to the concatenated lines, but it is really inefficient, and loses cursor position if typing happens anywhere except the end of the document.

In the end, I think I’d like this to behave as a large page populated with whitespace, and typing only happens in overwrite mode, but wraps at the end of lines. I’m not sure where to begin with that approach. Any suggestions?

  • 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-06-05T20:24:21+00:00Added an answer on June 5, 2026 at 8:24 pm

    I have found an acceptable, if not perfect, solution for wrapping by subclassing NSTextStorage.

    It behaves entirely as expected when adding text to the end of the document, whether pasting or typing. It also behaves when deleting or replacing text. The only place it is a little strange is when inserting text into the middle of already-full lines. That will cause a wrap just before the inserted text. It works fine, but it means that continuing to type at that position will create a number of short lines.

    For example, the text below wraps at 20 columns. Original text is x, and inserted text is o. The inserted text begins on the second line, at the 8th character.

    xxxxxxxxxxxxxxxxxxxx
    xxxxxxx
    ooooooo
    ooooooo
    oooooooxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxx
    

    Code below:

    - (void)breakLinesInArray:(NSMutableArray *)lines atLength:(NSUInteger)length
    charsPreceding:(NSUInteger)preceding charsFollowing:(NSUInteger)following
    {
        NSUInteger line = 0;
        while (line < [lines count]) {
            NSUInteger maxLineLength = length;
            if (line == 0) {
                maxLineLength = length - preceding;
            } else if (line == [lines count] - 1) {
                maxLineLength = length - following;
            }
            NSString *original = [lines objectAtIndex:line];
            if (maxLineLength > 0 && [original length] > maxLineLength) {
                NSString *first = [original substringToIndex:maxLineLength];
                NSString *second = [original substringFromIndex:maxLineLength];
                [lines replaceObjectAtIndex:line withObject:first];
                [lines insertObject:second atIndex:line + 1];
            }
            ++line;
        }
    }
    
    - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
    {
        static const NSUInteger kMaxLineLength = 20;
        NSString *text = [text_ string];
        NSUInteger firstLineStart = 0;
        NSUInteger lastLineEnd = 0;
        NSMutableArray *lines = [NSMutableArray arrayWithArray:[str componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];
        [text getLineStart:&firstLineStart end:nil contentsEnd:&lastLineEnd forRange:range];
        NSUInteger firstLineStartFragmentLength = range.location - firstLineStart;
        NSUInteger lastLineEndFragmentLength = lastLineEnd - (range.location + range.length);
        [self breakLinesInArray:lines atLength:kMaxLineLength charsPreceding:firstLineStartFragmentLength charsFollowing:lastLineEndFragmentLength];
        NSString *newString = [lines componentsJoinedByString:@"\n"];
        NSInteger changeInLength = [newString length] - range.length;
        NSInteger newLastLineLength = firstLineStartFragmentLength + lastLineEndFragmentLength + changeInLength;
        if ((firstLineStartFragmentLength || lastLineEndFragmentLength) && newLastLineLength > (NSInteger)kMaxLineLength) {
            newString = [@"\n" stringByAppendingString:newString];
            ++changeInLength;
        }
        [text_ replaceCharactersInRange:range withString:newString];
        [self edited:NSTextStorageEditedCharacters range:range changeInLength:changeInLength];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying create a query that will output a total number, as well as
Hi I am trying create a system call that will count the number of
I am trying create a layout. There are several inner layouts that should like
I'm trying create pagination on my page. The user can select the number of
Trying to create a grails ant task that has other environments besides prod for
I'm trying to create a Temp Table that has a key field that auto
I'm trying create a box in my Django app that displays text (and possibly
I'm trying create a ASMX webservice that can perform a HTTP GET request. I
I trying create a class derivated from System.Web.UI.Page and in override Render i set
I am trying create a small web application that allows a user to login

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.