How to increase spacing between lines in UITextView?
I wish to use default system font,i.e., Helvetica with size 15.
Declare you implement the protocol by adding
<NSLayoutManagerDelegate>
to your interface. Then, set:
yourTextView.layoutManager.delegate = self;
Then override this delegate method:
- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
return 5; // Line spacing of 19 is roughly equivalent to 5 here.
}
UPDATE:
I recently discovered that this can also be done using the NSMutableParagraphStyle setLineSpacing: API.
In an effort to always provide useful copy-paste code-snippet awesomeness, here you go!
NSMutableParagraphStyle *myStyle = [[NSMutableParagraphStyle alloc] init];
[myStyle setLineSpacing:myLineSpacingInt];
[myString addAttribute:myDesiredAttribute value:myStyle range:myDesiredRange];
[myViewElement setAttributedText:myString];
^myViewElement can be a UITextField, UILabel, or UITextView.
In IOS6+ you can set the typing attributes for a UITextView
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[textView setTypingAttributes:attrsDictionary];
Related
I am facing one issue that is - I have one label like "1. The Seibl is a software made by the some company and can be purchased at $500"
When it comes to iPhone 4s, the label is printing second line and second line is starting exactly under "1.". I would like to give space/margin/space so that label looks like numbering format.
Try this solution. It might helps you.
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:
Take a look at using a TextView instead, and modifying its textStorage property to define an exclusion area so that a new line is inset. Here's a link to the documentation.
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:
I have a FAQ section in my app, but I have to present it in a certain way using a non-editable UITextView. Just like below,
How can I cancel a recording?
A. You can cancel a recording while it is in progress by pressing the cancel (red ‘X’) button
in the center of the speaker on the recording screen. The audio recording
will not be saved.
But the problem is as you can see the has to be shown with some padding and the next line should start just below the answer line "not below the A". And there's a huge document of questions so I cannot format it manually. And it for both iPhone and iPad so I the UITextView width differs. Is there any solution to this kind of problem ?
I'd suggest to use NSAttributedString and the NSParagraphStyle combined with NSParagraphAttributeName.
Here is a example:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:yourString];
int indent1 = 0;
int indent2 = 20;
int indent3 = 2*indent2;
NSMutableParagraphStyle *styleTitleByNumber = [[NSMutableParagraphStyle alloc] init];
[styleTitleByNumber setFirstLineHeadIndent:indent1];
[styleTitleByNumber setHeadIndent:indent1];
NSMutableParagraphStyle *styleTitleByLetter = [[NSMutableParagraphStyle alloc] init];
[styleTitleByLetter setFirstLineHeadIndent:indent2];
[styleTitleByLetter setHeadIndent:indent2];
NSMutableParagraphStyle *styleSimpleText = [[NSMutableParagraphStyle alloc] init];
[styleSimpleText setFirstLineHeadIndent:indent3];
[styleSimpleText setHeadIndent:indent3];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:styleTitleByNumber
range:rangeOfTitleByNumber];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:styleTitleByLetter
range:rangeOfTitleByLetter];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:styleSimpleText
range:rangeOfSimpleText];
[yourTextView setAttributedText:attributedString];
Now, depending how is formatted your initial text, I leave you the way to know where to apply which style (for the NSRange parameter), or you can also, if the different parts are separated apply a direct effect on the NSAttributedString, and then combine them all.
In NSMutableParagraphStyle the paragraphSpacing Works perfectly fine.
but i am using bullet points also.
When user Press Return(Enter) then new Paragraph will come.
so when User type bullet points in Uitextview then in between two Points the paragaph spacing should be small and in rest of all the paragraph spacing is something high.
My code is...
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.minimumLineHeight = 0.f;
paragraphStyle.maximumLineHeight = 16.f;
paragraphStyle.firstLineHeadIndent = 16.0;
paragraphStyle.paragraphSpacing = 7.0;
paragraphStyle.lineSpacing = 5.0;
paragraphStyle.headIndent = 16.0;
paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
paragraphStyle.tailIndent=305.0;
mutattstr1 = [[NSMutableAttributedString alloc] initWithAttributedString:txtViewOfNotes.attributedText];
[mutattstr1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, mutattstr1.length)];
[txtViewOfNotes setAttributedText:mutattstr1];
Like in this text view i need to give small space in between two bullet points line.
So, how to get multiple line spacing or paragraph spacing or any other thing related to this in app.
Any kind of link, idea, code, dock is welcomed...
You can have multiple NSMutableParagraphStyle in the one NSAttributedStrings by using the range attributes - just like you've already done.
You'll just have to detect where you want each NSMutableParagraphStyle and apply with various NSRange. That's the "fun" part.
I am trying to set attribute text to a label. The attributes seems to be a working the font as well as the color.
Only issue I am facing is the wrapping of lines. The Size of the UILabel is (200,300) with numberofLines=0. So with this it should wrap the lines, but it is not happening so.
NSMutableString *title=[[NSMutableString alloc] init];
NSRange range1;
NSRange range2;
NSRange range3;
NSString *str1=#"ABCD EFGHI klm";
[title appendString:str1];
range1=NSMakeRange(0, str1.length);
NSString *str2=#"PQRSSSS ";
[title appendString:str2];
range2=NSMakeRange(range1.length, str2.length);
NSString *str3=#"1235 2347 989034 023490234 90";
[title appendString:str3];
range3=NSMakeRange(range2.location+range2.length, str3.length);
NSMutableAttributedString *attributeText=[[NSMutableAttributedString alloc] initWithString:title];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color1,NSForegroundColorAttributeName,[self getStlylishItalicFont:13.0] ,NSFontAttributeName,nil] range:range1];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color2,NSForegroundColorAttributeName,[self getStylishFont:13.0] ,NSFontAttributeName,nil] range:range2];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color3,NSForegroundColorAttributeName,[self getStylishBoldFont:13.0] ,NSFontAttributeName,nil] range:range3];
self.myTextLabel.attributedText=attributeText;
UILabel is displayed like this, even though the height is 300.
ABCD EFGHI klm PQRSSSS 1235 234 ...
What you need is the NSParagraphStyle attribute :
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
paragraphStyle.lineSpacing = 1;
//Now add this to your attributes dictionary for the key NSParagraphStyleAttributeName eg,
#{NSParagraphStyleAttributeName:paragraphStyle,...}
On an unrelated note you know its better to create dictionaries in the modern Objective-c format. Whenever I don't, my mentor get's angry. That would look something like this :
[attributeText setAttributes:#{NSForegroundColorAttributeName:color1, NSFontAttributeName:[self getStlylishItalicFont:13.0], NSParagraphStyleAttributeName:paragraphStyle, }];
//The trailing comma in the dictionary definition is not at typo it is important.
Make sure you set your UILabel's line break mode attribute to the one you desired like so:
UILabel.lineBreakMode = NSLineBreakByWordWrapping;
Or if you are using Interface Builder, you can do it there.
Why can't you set the numberoflines to 1.Because wrapping makes sense but number of lines 0 doesnt make sense.. and also you have to set proper frame for label.
I think so u are facing the problem with the uilabel increasing the height , If u need the multiple lines in label then u have to give the property of havin numberoflines=0; after that u have to resize the label frame according to the size of text u are giving to the label.
Please check the below code i may be useful to u,
NSString *someText = [NSString stringWithFormat:#"%#",[[arrFulldetails objectAtIndex:indexPath.row]valueForKey:#"MessageText"]];
CGSize constraintSize;
constraintSize.width = 300.0f;
constraintSize.height =100000;
CGSize stringSize =[someText sizeWithFont: [UIFont boldSystemFontOfSize: 17] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
CGRect rect ;
rect = CGRectMake(10, 150, 210, 20+stringSize.height);
along with setting:
self.myTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.myTextLabel.numberOfLines = 0;
You might also need to set your label's layout constraints to it's superview. I ran into this when I neglected to pin my label's trailing constraint to the my label's superview.