Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTextView limit



On Thursday, July 25, 2002, at 11:39 AM, Nico wrote:

What I'd like is to limit the number of lines in an NSTextView that's to say, if line #501 is attempting to be added, the line #1 must be deleted so that there is never more than 500 lines in total.

You can use textview delegate methods to control changes to text. In this case it sounds like you don't need to approve or disapprove changes, you just need to be notified of them so you can trim accordingly. In that case you probably want textDidChange:.

The next question is just what you mean by "line". To the Cocoa text system, a line of text is the text laid out on a single visual line, and it can depend on the width of the view. On the other hand, some people think of a "line" as the text between two hard line breaks (such as \n). The Cocoa text system calls that a "paragraph". Depending on the settings of your text view, the two concepts may actually coincide, but usually they are distinct.

Here is a rough example of how to count the lines (in the first sense above) in a text view. Untested, but should give the idea:

NSLayoutManager *layoutManager = [textView layoutManager];
NSRange containerGlyphRange = [layoutManager glyphRangeForTextContainer:[textView textContainer]]; // this forces all text in the container to be laid out, if it was not already laid out
NSRange lineGlyphRange = NSMakeRange(containerGlyphRange.location, 0);
unsigned count = 0;
while (NSMaxRange(lineGlyphRange) < NSMaxRange(containerGlyphRange)) {
(void)[layoutManager lineFragmentRectForGlyphAtIndex:NSMaxRange(lineGlyphRange) effectiveRange:&lineGlyphRange];
count++;
}

Douglas Davidson
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >NSTextView limit (From: Nico <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.