0

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?

Igor
  • 5,620
  • 11
  • 51
  • 103
  • What is the value of `range`? Is the character at the end of the text (and the new line after pressing enter) in this range? – Willeke Jul 21 '19 at 09:46
  • @Willeke, Lets say I have text "abcdefghij" which is 10 characters. Lets also say that my cursor is blinking at the very end of this text (after the letter j) and there is no new string at the end. Then the range is (10, 10) - it comes from the current selection is the view. And so the character at the end is part of the range (I think) and the new line after pressing Enter is not. – Igor Jul 21 '19 at 18:01
  • Is `attrs` `nil`? – Willeke Jul 21 '19 at 21:14
  • @Willeke, no, it is not. Code is executing but nothing happened. – Igor Jul 21 '19 at 21:17
  • Do you set `attrs` to something before setting `NSParagraphStyleAttributeName`? – Willeke Jul 21 '19 at 21:29
  • @Willeke, yes, of course. – Igor Jul 21 '19 at 21:51
  • How and what is the value? – Willeke Jul 22 '19 at 07:49
  • @Willeke, what value? – Igor Jul 22 '19 at 13:41
  • The value of `attrs` before `[attrs setValue: paragraphStyle forKey: NSParagraphStyleAttributeName]`. – Willeke Jul 22 '19 at 14:00
  • @Willeke, the value of attrs is nil. Maybe I need to call `tailIndent`? – Igor Jul 23 '19 at 23:28
  • So `attrs` is `nil`. See [Sending a message to nil in Objective-C](https://stackoverflow.com/questions/156395/sending-a-message-to-nil-in-objective-c). – Willeke Jul 24 '19 at 08:34
  • @Willeke, isn't this call should set the value of `attrs`? Something like `attrs->setValue( NSParagraphStyleAttributeName, paragraphStyle );` in C/C++. Because when my cursor is not at the last character, the code works and the text becomes indented. – Igor Jul 24 '19 at 14:47
  • @Willeke, I will also try to debug it tonight to see what the difference is. – Igor Jul 24 '19 at 14:47
  • @Willeke, please see the edit. Why am I getting the crash? I'm not even on the last character.... – Igor Jul 25 '19 at 13:01
  • The arguments of `NSMakeRange` are `loc` and `len`, not `start` and `end`. – Willeke Jul 25 '19 at 13:57
  • @Willeke, `start` and `end` are just variables that are passed in as parameters. Are you saying I'm using it wrong? – Igor Jul 25 '19 at 14:54
  • 1
    The arguments of `NSMakeRange` are the start location and length of the range, not the start location and the end location. – Willeke Jul 25 '19 at 15:11
  • @Willeke, I fixed it and now I see the indentation when I am on the new line (with the sample text I used). However when I start typing, cursor jumps back to the column 1, undoing the indentation. Any idea? I'm going to put my latest code in the OP. – Igor Jul 26 '19 at 01:23
  • I tried to reproduce the issue and I'm seeing weird behaviour of `MSTextView`. Do you change the indents when the user clicks a button? Take a look at how the indent settings of the ruler work (in your text view or in TextEdit). – Willeke Jul 26 '19 at 09:21
  • @Willeke, please see another edit. The code works when I am on the position 0 or if I have a selection which starts from position 0. But if the cursor is somewhere in the middle of the string or at the end - no indentation happens. Any idea? Maybe I need to identify the beginning of the paragraph to apply the indentation? – Igor Jul 29 '19 at 00:44
  • When you apply the indentation to the middle of the string then the indentation of the first character of the paragraph doesn't change. – Willeke Jul 29 '19 at 08:03
  • @Willeke, is it possible to make it work like this? Find the beginning and ending of the current paragraph and apply the indentatio? Because it doesn't work even if I have a wrap mode on - meaning the control width is too narrow to display the whole string. And it doesn't work if I'm at the end of the paragraph or end of the text. – Igor Jul 29 '19 at 13:00
  • 1
    Use `paragraphRangeForRange:` of `NSString` to get the range of the paragraph. – Willeke Jul 29 '19 at 14:47
  • @Willeke, you've been very helpful. Now for the last time - see the last edit and the question. My guess is I need to set the range appropriately. Thank you in advance. – Igor Jul 31 '19 at 01:35

0 Answers0