ALL,
Apple documentation here talks about 3 function on setting indentation.
I did use them and I even called:
[attrs setValue: paragraphStyle forKey: NSParagraphStyleAttributeName];
[m_textView setTypingAttributes: attrs];
but unfortunately pressing Enter at the end of the text does not indent the cursor.
Below is a complete code:
paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setFirstLineHeadIndent: indent];
[paragraphStyle setHeadIndent: indent];
[storage addAttribute: NSParagraphStyleAttributeName value: paragraphStyle range: range];
if( range.length == 0 )
{
[attrs setValue: paragraphStyle forKey: NSParagraphStyleAttributeName];
[m_textView setTypingAttributes: attrs];
}
[paragraphStyle release];
It works when I have a selection in the text (range.length > 0), but when I try to set it, go to the end of the text and press Enter, cursor still goes to the column 1 (not indenting).
What am I missing?
I think it is called auto-indenting so I will add that tag as well. Please remove if I'm wrong.
TIA!
[EDIT]
Trying to add:
[m_textView setDefaultParagraphStyle:paragraphStyle];
does not do anything.
When there is no selection and I hit Enter at the end of the text, cursor still jump to the position at the beginning of the line.
[/EDIT]
[EDIT2]
I modified my code as follows:
if( style.HasLeftIndent() )
{
if( start == end && start == 0 )
range = NSMakeRange( 0, 1 );
else if( start == end && start == storage.string.length )
range = NSMakeRange( start - 1, start );
else if( start == end )
range = NSMakeRange( start, start + 1 );
paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setFirstLineHeadIndent: indent];
[paragraphStyle setHeadIndent: indent];
[storage addAttribute: NSParagraphStyleAttributeName value: paragraphStyle range: range];
if( /*start == -1 && end == -1*/range.length == 0 )
{
[attrs setValue: paragraphStyle forKey: NSParagraphStyleAttributeName];
[m_textView setDefaultParagraphStyle:paragraphStyle];
[m_textView setTypingAttributes: attrs];
}
[paragraphStyle release];
}
Now let's say my text contains following:
Big brown fox jumps over the lazy dog.\n
If I have my cursor sitting right after the dot and there is no selection the program crashes. It looks like the range variable is incorrect.
[/EDIT2]
[EDIT3]
My current code is:
if( start == end && start == 0 )
range = NSMakeRange( 0, 1 );
else if( start == end && start == storage.string.length )
range = NSMakeRange( start - 1, 1 );
else if( start == end )
range = NSMakeRange( start, 1 );
paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setFirstLineHeadIndent: indent];
[paragraphStyle setHeadIndent: indent];
[storage addAttribute: NSParagraphStyleAttributeName value: paragraphStyle range: range];
if( /*start == -1 && end == -1*/range.length > 1 )
{
[attrs setValue: paragraphStyle forKey: NSParagraphStyleAttributeName];
[m_textView setDefaultParagraphStyle:paragraphStyle];
[m_textView setTypingAttributes: attrs];
}
[paragraphStyle release];
[/EDIT3]
[EDIT4]
This is my latest code:
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithCapacity:5];
if( style.HasLeftIndent() )
{
if( start == end && start == 0 )
range = NSMakeRange( 0, 1 );
else if( start == end )
range = NSMakeRange( start - 1, 1 );
paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setFirstLineHeadIndent: indent];
[paragraphStyle setHeadIndent: indent];
[storage addAttribute: NSParagraphStyleAttributeName value: paragraphStyle range: range];
{
[attrs setValue: paragraphStyle forKey: NSParagraphStyleAttributeName];
[m_textView setDefaultParagraphStyle:paragraphStyle];
[m_textView setTypingAttributes: attrs];
}
[paragraphStyle release];
}
this code works if my cursor sits at the beginning of the paragraph.
However if I'm in the middle of itor in the end of there is no indentation.
The paragraph being the text after new-line symbol or Enter press.
What am I doing wrong?
[/EDIT4]
[EDIT5]
This is my last code:
if( style.HasLeftIndent() )
{
if( start == end && start == 0 )
range = NSMakeRange( start, start + 1 );
else
range = [[m_textView textStorage].string paragraphRangeForRange: NSMakeRange( end - 1, 1 )];
paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setFirstLineHeadIndent: indent];
[paragraphStyle setHeadIndent: indent];
[storage addAttribute: NSParagraphStyleAttributeName value: paragraphStyle range: range];
if( /*start == -1 && end == -1*/range.length == 0 )
{
[attrs setValue: paragraphStyle forKey: NSParagraphStyleAttributeName];
[m_textView setDefaultParagraphStyle:paragraphStyle];
[m_textView setTypingAttributes: attrs];
}
[paragraphStyle release];
}
It is almost perfect. The only thing missing is following:
Again my text is:
The brown fox jumps over the lazy dog.\n\n
There are 2 empty lines after the text. When my cursor is set on the top new new line this code indent the text on the first line. What I'd like is the text to stay non-indented, but the cursor jumps to the right, so that newly entered text becomes indented.
And the same should happen when my cursor at the second empty line.
[/EDIT5]
What am I missing?