Left indentation of the NSTextView

大兔子大兔子 提交于 2019-12-12 02:42:59

问题


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.

来源:https://stackoverflow.com/questions/37117771/left-indentation-of-the-nstextview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!