Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put different headIndent (alignment) for every paragraph in UITextView

In my application i need to align all the paragraph differently.

like, first paragraph's headIndent is 0.0f then second's 10.0f and third's 3.0f.

i am giving all the paragraph style to textview.attributedText. and it took only one style.

Here whole text will come dynamically by Typing. means when User will type in text view at that time. so, there are no static string to do this.

I am placing all the characters in UITextView by this...

 UIFont *fontBold = [UIFont fontWithName:@"Helvetica-Bold" size:15];
 attributesHelveticaBold = @{NSFontAttributeName :fontBold};

 UIFont *fontNormal = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
 attributesNormal = @{NSFontAttributeName :fontNormal};

 if (varBold== 1) {
     [textView setTypingAttributes:attributesHelveticaBold];
 }
 else {
     [textView setTypingAttributes:attributesNormal];
 }

And i want to get this kind of result in text view

enter image description here

When i am typing the typing become slow too.

but i think i'll come over that issue but for now i stuck on this alignment problem.

how to do it when bullet point come and when different text come.

any kind of link, code, tutorial will be great help...

---------- Edit : ----------

Please have a look in Evernote's application.
I need to do the exactly same thing in my app. for alignment of second,third,etc line when bullet come.

-------- Edit after searching :-------

I searched too much for this but ain't find anything by googling.
So, now i am asking if anyone now about How to give any paragraph style or any attribute style to a paragraph and just leave it as it is on text view and then perform other paragraph style on second paragraph. at this time the first paragraph will not pass throw "shouldChangeTextInRange" method.

yes, it's quite confusing whatever i am saying.

so i explaining it in general...

if user set the text view's first paragraph's headIndent=7.0f then when user will type next paragraph and set the headIndent = 13.0f then first paragraph will stay as it is in textview and just running paragraph will come in a chapter (means in a method).

right now i am doing these thing in shouldChangeTextInRange method to do style for each paragraph.

varStaringPointOfString = 0;
varEndingPointOfString = 0;

NSArray *sampleArrToGetattrStr = [txtViewOfNotes.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (int i=0; i<[sampleArrToGetattrStr count]; i++)
{
    NSString *strtostoreLength = [NSString stringWithFormat:@"%@",[sampleArrToGetattrStr objectAtIndex:i]];
    varStaringPointOfString = (int)strtostoreLength.length + varEndingPointOfString;

    if ([strtostoreLength hasPrefix:@"\t•\t"])
    {

        [[textView textStorage]  addAttribute:NSParagraphStyleAttributeName value:paragraphStyleForBullet range:NSMakeRange(varEndingPointOfString, strtostoreLength.length)];
    }
    else
    {


        [[textView textStorage]  addAttribute:NSParagraphStyleAttributeName value:paragraphStyleNormal range:NSMakeRange(varEndingPointOfString, strtostoreLength.length)];

    }

    varEndingPointOfString = varStaringPointOfString;
    strtostoreLength =@"";
}

but from this the speed of typing is become very slow.

like image 679
Sam Avatar asked Sep 30 '22 23:09

Sam


1 Answers

Try this:

NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init];
[paragraphStyle setFirstHeadLineHeadIndent:firstLineIndend]; //Only the first line
[paragraphStyle setHeadIndent:headIndent]; //The rest of the lines, except the first one
[yourAttributedString addAttribute:NSParagraphStyleAttributeName 
                             value:paragraphStyle
                             range:paragraphRange];

For the bullet point, that's something different. You need to find where are the bullet point, and set another indent accordingly.

like image 109
Larme Avatar answered Oct 13 '22 10:10

Larme