Left indentation of the NSTextView

2019-09-09 02:11发布

问题:

I wrote the following code:

 NSMutableParagraphStyle *paragraphStyle;
 double indent = 100 * mm2pt / 10.0;
 if ( !m_textView )
     return;

 if ( start == -1 && end == -1 )
 {
     if( style.HasLeftIndent() )
     {
         paragraphStyle = [[NSMutableParagraphStyle alloc] init];
         [paragraphStyle setHeadIndent: indent];
         [paragraphStyle setFirstLineHeadIndent: indent];
         [m_textView setDefaultParagraphStyle:paragraphStyle];
         [paragraphStyle release];
     }
 }
 else // Set the attributes just for this range.
 {
     NSRange range = NSMakeRange(start, end-start);

     NSTextStorage* storage = [m_textView textStorage];
     if( style.HasLeftIndent() )
     {
         paragraphStyle = [[NSMutableParagraphStyle alloc] init];
         [paragraphStyle setFirstLineHeadIndent: indent];
         [paragraphStyle setHeadIndent: indent];
         [storage addAttribute: NSParagraphStyleAttributeName value: paragraphStyle range: range];
         [paragraphStyle release];
     }
 }

The idea is that if the start and end both are equal to -1, left indent should be applied to the whole text view whether there is a text already or not. And the else condition should be applied if I have a selection.

Unfortunately the code inside the if does not work, but the else condition works and I can see the text being indented for the selection.

What am I doing wrong? Because even if the control is empty (no text) and the left indent is applied the text I put in (programmatically or by typing) should have left indentation. But trying to call this code with start = -1 and end = -1 with already existing text does not change the appearance.

Thank you.