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 6079917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:00:06+00:00 2026-05-23T11:00:06+00:00

In an app I’m working on, the user inputs plain text, and the app

  • 0

In an app I’m working on, the user inputs plain text, and the app reformats the text by transforming it to an NSAttributedString, and displays it. This all happens live.

Currently, I’m doing the following on my NSTextView’s textDidChange delegate method:

- (void)textDidChange:(NSNotification *)notification {

    // saving the cursor position
    NSInteger insertionPoint = [[[self.mainTextView selectedRanges] objectAtIndex:0] rangeValue].location;

    // this grabs the text view's contact as plain text
    [self updateContentFromTextView];

    // this creates an attributed strings and displays it
    [self updateTextViewFromContent];

    // resetting the cursor position
    self.mainTextView.selectedRange = NSMakeRange(insertionPoint, 0);
}

While this mostly works, it’s not ideal. The text seems to blink for a split second (you especially notice it on the red dots under spelling errors), and when the cursor was previously near one of the edges of the visible rect, it the scroll position gets reset. In my case, this is a very much undesirable side-effect.

So my question is: Is there a better way of doing what I’m trying to do?

  • 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-23T11:00:06+00:00Added an answer on May 23, 2026 at 11:00 am

    I think you have a slight misconception of how an NSTextView works. The user never enters a “plain string”, the data store of an NSTextView is always an NSTextStorage object, which is a subclass of NSMutableAttributedString.

    What you need to do is add/remove attributes to the existing attributed string that the user is editing, rather than replacing the entire string.

    You should also not make changes to the string in the ‑textDidChange: delegate method, as changing the string from that method can cause another change notification.

    Instead, you should implement the delegate method ‑textStorageDidProcessEditing:. This is called whenever the text changes. You can then make modifications to the string like so:

    - (void)textStorageDidProcessEditing:(NSNotification*)notification
    {
        //get the text storage object from the notification
        NSTextStorage* textStorage = [notification object];
    
        //get the range of the entire run of text
        NSRange aRange = NSMakeRange(0, [textStorage length]);
    
        //for example purposes, change all the text to yellow
    
        //remove existing coloring
        [textStorage removeAttribute:NSForegroundColorAttributeName range:aRange];
    
        //add new coloring
        [textStorage addAttribute:NSForegroundColorAttributeName 
                            value:[NSColor yellowColor] 
                            range:aRange];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app will have no user interface at all (exept for notifications) except for
My app uses a WebRequest at certain points to get pages from itself. This
My app needs to build a buffer from all the selected cells on a
App A has this BroadcastReceiver in its manifest (within <application>): And this receiver: public
The app I'm working on is relatively simple but it is throwing memory warnings.
The app Im working on uses gps and sometimes in the onResume when the
My app compiles to about 80mb. I was under the impression this was quite
My app fails at this line of code: Dim objConnection As New SqlConnection(Application(ConnString)) My
My app has the following models: user and watch_list. User has attributes id, name
My app must NOT auto -rotate at all. But it includes a screen which

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.