0

I have an implementation that abstracts from NSView to display a scrolling text view.

I have a long string I'm displaying on a custom class based on NSView. As soon as it starts to scroll the lines overlap one another and become unreadable. At the end of the loop, when it starts over it will show all the lines on the screen without overlap, and as soon as this first section reaches the end of the screen the overlap comes back for the rest of the drawing period.

Are there any ideas on what I could fix here? Thanks so much.

Finally

Here are two screen shots, one has the text partly displayed without overlap, this is at the end of the first loop and it represents the beginning of the text.

enter image description here

This is the second screen shot, in which the overlap is prominent and consistent with how it is displaying most of the string.

enter image description here

Here is the code from the scrolling, as copied /pasted from iTunes Song Title Scrolling in Cocoa

Community
  • 1
  • 1
Finley
  • 1
  • 3
  • Can you show some code for how it handles the scrolling? It looks possibly like it isn't clearing the view properly before rendering the text in the new position. Also, how long is the draw taking? – Fogmeister Apr 17 '13 at 09:39
  • Sure, thanks for your reply. I actually got the source code from here. http://stackoverflow.com/questions/3232801/itunes-song-title-scrolling-in-cocoa And I think it draws this fast. [displayNewSentance2 setSpeed:0.01]; //redraws every 1/100th of a second – Finley Apr 17 '13 at 09:42
  • I commented out the deallocs and releases. – Finley Apr 17 '13 at 09:59
  • @Fogmeister I'm sorry I don't exactly know what I'm doing but I can't seem to fix this. Also I tried highlighting the text and it seems like I could drag one revealing a separate text layer underneath, like it is drawing it twice. Btw these are random substitutions along the grammar of a text, noun, verb, adjective, etc, from a word list composed of that text along with anything else, kind of like word alchemy. It's a meditation on things lost in translation or saturation, or just time and what isn't lost. – Finley Apr 17 '13 at 10:07
  • At the risk of going off topic: Are you _sure_ you meant the button title to be Grammatical _Sentance_? – Monolo Apr 17 '13 at 17:03
  • Hi, lol, not much thought was put into the title. It was an old button for a missing nstextfield that printed the grammer abstractedly of imported text using NSLinguisticTagger. – Finley Apr 17 '13 at 20:16

1 Answers1

1

Since drawRect is overwritten it must also clear the background, otherwise anything drawn underneath it will show through and (as you can see) previous content is not removed. So, add a [backgroundColor set] call and NSRectFill before you start writing the text.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • Hi, thanks for your help @Mike. I added [[NSColor whiteColor] setFill]; NSRectFill(dirtyRect); To the beggining of -(void)drawRect:(NSRect)dirtyRect{} The background is white, but I am still having the issue. I noticed that shorter strings it's not a problem. Once the string is greater than the amount of space in the text view to present the whole thing at once for some reason then it starts happening. Here example lastphotoisoverlap. http://www.use.com/319a1499564027f4cba7#photo=3 http://www.use.com/319a1499564027f4cba7#photo=2 http://www.use.com/319a1499564027f4cba7#photo=1 – Finley Apr 17 '13 at 20:12
  • Well, if you look closer at that code you referenced you will see that this is for horizontal scrolling of a single line. Also the point computation in drawRect looks suspicious, e.g. when the text is scrolled out once the output point is moved by the dirty rect with, which might be smaller than the text width. Hence the text is suddenly written partially in the view again. After that another paint happens if x is still < 0 (which can happen easily if the text is large enough). So this draws the text again. – Mike Lischke Apr 18 '13 at 08:05
  • Thanks. I had forgotten that I changed some of the code to be thing.thing.y instead of thing.thing.x to make it move vertically. I have now changed string.width to be of .height to no avail. I don't understand the purpose of comparing x to 0 and the rect width with the text width. Or, maybe text heigh versus rect height, in my case, if the rectangle is the drawing area? It does seem to do with the rect height because any string that is equal or less than this works. Thanks so much for your help, I think it's along the lines of what you've said, I'll keep working on this. – Finley Apr 18 '13 at 23:47
  • I would add that the text is definitely overlapping. If the space of the drawing rectangle is 5, the length of text 1-5 works, and when the string is greater, instead of the length in string 6 that is 6-7, being held off screen and then shown once 1-2 have passed off screen, instead 6-7 is written on top of 1-5 and this creates the blurring. However, I don't see why this happens. – Finley Apr 18 '13 at 23:52
  • Also I see what you mean that there are two calls to paint. – Finley Apr 19 '13 at 00:08
  • I keep trying different things but to no avail. Do you have any advice on how to more appropriately handle scrolling here? Thanks. – Finley Apr 20 '13 at 08:32
  • Is it possible to get a stripped down version of your app to see what's going on? – Mike Lischke Apr 20 '13 at 09:28
  • Sure, I'd be much obliged. I've attached the project here. In essence, I'm sending a large string to the NSView subclass in the code I referenced via it's method "setText:" I changed .x to .y to make things scroll vertically. This string is determined by a process which uses this code reference to implement multi dimensional arrays. http://stackoverflow.com/questions/7459931/objective-c-multi-dimensional-array. Bassically, curRow is an array which is created each time to be the "rows" in another array, arrayA. – Finley Apr 20 '13 at 10:45
  • Initially each sentence of a text in plain text formatting is read using NSLinguisticTagger. The sentences are transformed and stored as an order of grammer, while the words themselves are added into a "word bank" that groups real words according to their grammar function. Then a string is created according to the original grammer of that text but with random substitutions for any specific noun, any specific verb, etc. After this string is created the setText: method of the code for scrolling I mentioned is called and this string is passed to that code that puts it in view and scrolls it. – Finley Apr 20 '13 at 10:46
  • There's a (bad) memory leak somewhere. If the text is too long it will use up all of the memory in the computer. Short texts work best, and poetic ones lend more easily to random recombinations. Initially one copy is created, clicking "generate new sentances" again will create another copy, and so. "read" reads the txt I've uploaded two zip files here. Test xcode duplicate contains the whole project, Just scrolling text contains just the .h and .m files for the scrolling text class. If there is an easier way to share this, let me know. Thanks, Finley. http://www.mediafire.com/#xc7cc055wo7f7 – Finley Apr 20 '13 at 10:48
  • The approach you have taken is mostly wrong, sorry. With larger texts its saturates a complete core to 100%. That's because you are printing the full text all the time, even the part that is not visible. My last advice here: don't use NSTextView (you are not using anything of its features) instead start over with NSView or NSControl. Then split the string to print into parts so you only print what is visible. You could however go another route: instead drawing the text yourself you could go back to NSTextView and just scroll it programmatically and leave everything else to it. – Mike Lischke Apr 20 '13 at 11:39
  • I'll try to find out how to program it programmatically. Thanks so much for your help. – Finley Apr 20 '13 at 19:32