4

I have 1 attributed string with multiple paragraph.
I had given the FirstLineHeadIndent = 2.12.
Now I want to give the FirstLineHeadIndent=0 to 1st paragraph and FirstLineHeadIndent=2 to 2nd paragraph in the attributed string.

If I set the property like

 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing =Line_space;
        paragraphStyle.firstLineHeadIndent =2;
        paragraphStyle.headIndent =margin_space;
        paragraphStyle.tailIndent =-margin_space;
        paragraphStyle.paragraphSpacing=paragraph_space;

        NSDictionary *ats = @{
                              NSFontAttributeName : [UIFont fontWithName:self.bookView.defaultFontFamily size:self.bookView.defaultFontSize],
                              NSParagraphStyleAttributeName : paragraphStyle,
                              };

It will give the head space to both the paragaraph. Please help me. I am uploading the image for more help:- enter image description here

ios developer
  • 3,363
  • 3
  • 51
  • 111
  • 1
    How did you set the paragraph style to your `NSAttributedString`? – Larme May 14 '14 at 10:35
  • @Larme I had set the paragraph style by NSMutableParagraphStyle.But it gives imapact to all the paragraph.I want to set different style to 1st and 2nd paragraph.For example 1)paragraph have line space =2 and 2nd paragraph have line space =4. – ios developer May 14 '14 at 10:39
  • Could you show all the code? – Larme May 14 '14 at 10:40
  • NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.firstLineHeadIndent =2; paragraphStyle.paragraphSpacing=paragraph_space; NSDictionary *ats = @{ NSFontAttributeName : [UIFont fontWithName:self.bookView.defaultFontFamily size:self.bookView.defaultFontSize], NSParagraphStyleAttributeName : paragraphStyle, }; – ios developer May 14 '14 at 10:43
  • You need to use 2 paragraphStyle, with different range. Is that what you do? – Larme May 14 '14 at 10:45
  • Can you give me example? how do i fetch the range of the 1st paragraph? It is not always fix? Do you any idea to fetch the range of paragaph dyanamically? – ios developer May 14 '14 at 11:03
  • Could you show a screenshot of what you have, and what you want? An illustration could be useful. – Larme May 14 '14 at 11:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52667/discussion-between-shweta-and-larme) – ios developer May 14 '14 at 11:18

1 Answers1

6

So, the solution is to use 2 paragraph styles.
Your NSString is separated by a \n, that indicate the separations between the 2 paragraphs.

NSMutableParagraphStyle *firstParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[firstParagraphStyle setFirstLineHeadIndent:0];
//Do the rest of the settings

NSMutableParagraphStyle *secondParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[secondParagraphStyle setFirstLineHeadIndent:2];
//Do the rest of the settings

//You may use the same paragraphStyle, changing just the firstLineHeadIndent, and set the attributes, but for a clearer explanation, I used 2 paragraph styles
NSRange range = [[yourAttributedString string] rangeOfString:@"\n"];

[yourAttributedString addAttribute:NSParagraphStyleAttributeName value: firstParagraphStyle range:NSMakeRange(0, range.location)]; //The range is from the start to the \n

[yourAttributedString addAttribute:NSParagraphStyleAttributeName value: secondParagraphStyle range:NSMakeRange(range.location, [[yourAttributedString string] length]-range.location)]; //The range is from the start of \n to the end
Larme
  • 24,190
  • 6
  • 51
  • 81