Re: Control characters in an NSAttributed string?
Re: Control characters in an NSAttributed string?
- Subject: Re: Control characters in an NSAttributed string?
- From: Douglas Davidson <email@hidden>
- Date: Thu, 22 May 2003 12:14:51 -0700
On Thursday, May 22, 2003, at 11:54 AM, Mark Ogilvie wrote:
I may be mis-understanding this, but does an NSAttributed string have
similiar controls to a regular c style string? In a c style string \n
will be a new line, but I haven't found anything in the NSAttributed
string documentation that mentions that, though there is a note in the
NXText documentation that gives a hexidecimal code for a new line.
If I create a NSAtrributed string from an NSString, are the control
characters(tab, newline) preserved or converted and is there a way to
access them? I was trying to write a routine to count the number of
lines in an RFT document and I'm a bit confused.
The short answer is that yes, an NSAttributedString has control
characters similar to a C string. The longer answer is that an
attributed string is formed by adding attributes to an NSString, and an
NSString is conceptually a Unicode string. In Unicode the ASCII
control characters are mapped with their usual values, but there are
some additional control characters as well. Even in C strings there
are several possible line breaks (\n, \r, \r\n), but in Unicode strings
there are some additional line breaks as well.
Fortunately, there are methods on NSString that will locate line breaks
for you, and will handle the complexity of the multiple kinds of
breaks. Take a look at lineRangeForRange: and
getLineStart:end:contentsEnd:forRange:. The first is simpler, and
would be used if you care only about the range of the entire line. The
second is a bit more complicated, and would be used if you need to
separate the contents of the line from the break at the end. This is
because the break at the end may be 0, 1, or 2 characters--0 if we are
at the end of the string and there is no trailing break, 1 under most
circumstances, and 2 if there is a \r\n.
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.