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

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
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.

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.

Related

How could I change the color of a fixed character of placeholder text, as I am bit confused to perform this

I tried all the ways but its not as per my aspectations please help.
I have to change the color of all * to red.
I know I have to work in didload event.
I tried to get the last character of all the strings and do one by one but it makes the code quite lengthy hope there would be any should code/ approach.
Thanks
Image Here Please have a look
Try this one
NSMutableAttributedString *placeHoldertString = [[NSMutableAttributedString alloc] init];
NSAttributedString *str1 = [[NSAttributedString alloc] initWithString:#"First Name" attributes:#{ NSForegroundColorAttributeName : [UIColor lightGrayColor] }];
NSAttributedString *str2 = [[NSAttributedString alloc] initWithString:#"*" attributes:#{ NSForegroundColorAttributeName : [UIColor redColor] }];
[placeHoldertString appendAttributedString:str1];
[placeHoldertString appendAttributedString:str2];
yourTextFeild.attributedPlaceholder = placeHoldertString;
your question is still not clear you could make the textfield red on two events 1). when the user is entering the characters and jumps to the next textField you can make it red if the input is blank or incorrect
2). You can make it red on button click of submit or anything you want
and instead of writing it in View did load write it in UitextField Delegate method i.e Should change character in range method this will be called for each text field and you can use nested if else for your text field inside that method
e.g
if textField == (your textfiled name here)
{
//do your logic here for making the field red
}
remember you have to use this nested if else in should change character in length method UitextField Delegate
Try this
textField.attributedPlaceholder =
[[NSAttributedString alloc] initWithString:#"yourPlaceHolderName" attributes:#{NSForegroundColorAttributeName: yourColor, NSFontAttributeName : yourFont}];
}

Color the specific range of word by NSMutableAttributedString in a UITextView cause UITextView scroll to the top?

Here is a UITextView,on which i put long chapter(i wanna to make it just like a book.),when i press a word more than 2 seconds,(i set a long gesture here),then the word will be highlight(the following code can successfully hightlight the word,but also trigger some side effect which i need to eliminate),then here comes the story:
code is like follows:
NSMutableAttributedString *attrStr = [self.contentTextView.attributedText mutableCopy];
[attrStr addAttribute:NSBackgroundColorAttributeName value:[UIColor grayColor] range: _wordToSearchRange ];
self.contentTextView.attributedText = attrStr;
So far, i debugged the code, and finally targetted on this line:
self.contentTextView.attributedText = attrStr;
which lead my textView scroll to the top(not stay in the current word location),it is reasonable since this operation is similar to refresh the textView and set the totally new content for it. But is there any other method to set the specific word range in a textView which would not cause this ?
Any help would be great! Thx a lot!
Since you are editing existing text, you shouldn't replace all of it.
You need to create attributes dictionary and add it to the attributed text:
NSMutableDictionary* attributes; //set the wanted attributes
NSRange range; //set the right range
[_contentTextView.textStorage beginEditing];
[_contentTextView.textStorage addAttributes:attributes range:range];
[_contentTextView.textStorage endEditing];

Indent second line of UILabel

So I have a UILabel that may or may not go to a second line, depending if it is on iPhone or iPad. What I would like to accomplish is to have it indent on the second line to line up correctly, if needed.
On iPad it will almost never need the second line break, and depending on which iPhone it is running on, it may or may not. So, in essence, I need a way to dynamically indent the second line, only when there is a second line.
Use an NSAttributedString for your label, and set the headIndent of its paragraph style:
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.headIndent = 14;
NSDictionary *attributes = #{
NSParagraphStyleAttributeName: style
};
NSAttributedString *richText = [[NSAttributedString alloc] initWithString:#"So this UILabel walks into a bar…" attributes:attributes];
self.narrowLabel.attributedText = richText;
self.wideLabel.attributedText = richText;
Result:

how to set a UITextView's text to be bolded and not clickable

I have the following HTML in a UITextView and would like to render it into a UITextView
is my body for the note
food item - more item stuff;`
Let me add: it's currently showing as blue and underlined and not clickable. I would like to make it bolded and not clickable. I have read the docs regarding linkTextAttributes but, not having used this, it is a bit beyond me and I don't really see any easy way to manipulate this. How would I just render the above link bolded and black (not blue) and maintain the non-clickable nature?
UPDATE (solution using UITextView's linkTextAttributes)
self.testTextView.editable = NO;
self.testTextView.selectable = YES;
self.testTextView.userInteractionEnabled = NO; // workaround to disable link - CAUTION: it also disables scrolling of UITextView content
self.testTextView.dataDetectorTypes = UIDataDetectorTypeLink;
self.testTextView.linkTextAttributes = #{NSFontAttributeName : [UIFont boldSystemFontOfSize:14.0f], // NOT WORKING !?
NSForegroundColorAttributeName : [UIColor redColor]};
...
self.testTextView.text = #"Lorem ipsum http://www.apple.com Lorem ipsum";
As you can see in comments, I wasn't able to set new font to linkTextAttributes, though the colour attribute was working as expected.
If you can get away with colour attribute or some other text attribute to style your URLs and you don't have to worry about disabled UITextView scrolling, then this may be your solution.
PREVIOUS (alternative solution)
If you're using Storyboard/xib then make sure you've deselected Detection -> Links for your UITextView. You can make your link bold by setting its container font to some bold typeface. If you want to support different text/font styles in one string object then you should really look for NSAttributedString or NSMutableAttributedString.
See: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSAttributedString_Class/Reference/Reference.html.
Example:
UIFont *linkFont = [UIFont fontWithName:#"SomeBoldTypeface" size:12];
NSString *link = #"food item - more item stuff";
NSMutableAttributedString *someString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"is my body for the note %#; let me ad", link]];
[someString addAttribute:NSFontAttributeName value:linkFont range:NSMakeRange(24, link.length)];
UITextView *textView = [[UITextView alloc] init];
textView.attributedText = someString;
...

Why doesn't this properly color the substring in my UILabel?

I want a particular substring to be yellow. I don't want to break it up into multiple UILabels since that would make localizing the layout a nightmare. So I do this:
NSMutableAttributedString* instructions = [[[NSMutableAttributedString alloc] initWithString:self.o_instructionsLabel.text] autorelease];
NSRange range = [instructions.string rangeOfString:#"FOOBARBAZ"];
if (range.length > 0)
{
[instructions addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:range];
self.o_instructionsLabel.attributedText = instructions;
}
However, the whole of the text in the label remains white. This seems to be how all the examples do it, I verified the range is correct, and when I dump the instructions object, I see the attributes inline where I guess they should be.
What am I doing wrong?
The solution was custom code in drawTextInRect: was ignoring the attributed string.

Resources