Multiple paragraphSpacing in NSMutableParagraphStyle - ios

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.

Related

IOS: Justify text in UILabel not working in UI

I want to align justify my text in UILabel but it seem like not work. However, another align is left,right,center work.
I'm using XCode 7.2. I have tested on simulator and real device but it produce same problem
Align Justify
My text:
Don't worry, your data will not be sold.Don't worry,your data wills not be sold. Connecting your accounts will benefit your E score and your profile viewing experience. Don't worry, your data will not be sold.Don't worry, your data wills not be sold. Connecting your accounts will benefit your ECT score and your profile viewing experience.
with font : Helvetica Neue 13.0 and trailing/leading: 10
Same problem if I use align in here to justify text
I don't know why this happened to me. Please give me some instruction for fix it. Any help would be great appreciated
It seems like a bug of UILabel,but you can fix it with a tiny change in your storyboard.
Click the more button in the same line of NSTextAlignments,add a little Head Indent ,such as 0.1 .
Your UILabel will work just fine.
This should work. Here is what I get from the simulator:
What I've done:
Drag & drop an UILabel on the storyboard and add some constrains, and colours as well.
Set the text as "attributed"
Put your text in the text field.
Click on justify.
Change the police
Numbers of lines 0.
At this point you should have this:
Now from the storyboard to your controller add an IBOutlet (Ctrl + drag it to the top of your controller). It should be like this:
Now add some code in your viewDidLoad fct:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Justified
let attributedString = NSAttributedString(string: label.text!, attributes: [ NSParagraphStyleAttributeName: paragraphStyle, NSBaselineOffsetAttributeName: NSNumber(float: 0)
])
label.attributedText = attributedString
label.numberOfLines = 0
The last thing to do is to run your simulator to see if it does what you are expected:
P.S: With xCode 7.2 works definitely. It works for me on both version.
Here is one solution to solve my problem right now. I set the alignment justified for my UILabel programmatically and it work
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentJustified;
NSDictionary *attribute = #{
NSParagraphStyleAttributeName: paragraph,
NSFontAttributeName: self.describesLabel.font,
NSBaselineOffsetAttributeName:[NSNumber numberWithFloat:0]
};
NSAttributedString *attributeMessage = [[NSAttributedString alloc] initWithString:self.describesLabel.text attributes:attribute];
self.describesLabel.attributedText = attributeMessage;
Don't use attributed text just use Plain text and select all the text and make it justify. I am facing the same problem fixed it by changing attributed to plain text in Storyboard.
If it doesn't work then you have to fix it by code :-
Using Attributed Text:-
NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle.alignment = NSTextAlignmentJustified;
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:yourFont,NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName, nil];
NSAttributedString *attributeMessage = [[NSAttributedString alloc] initWithString:yourTextString attributes:attrsDictionary];
self.yourLabel.attributedText = attributeMessage;
Using Plain Text :-
self.yourLabel.textAlignment = NSTextAlignmentJustified;
self.yourLabel.font = yourFont;
self.yourLabel.text = yourTextString;
Hope It helps...
You can do it programatically if its not working from storyboard or nib
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentJustified;
NSAttributedString *string = [[NSAttributedString alloc] initWithString:#"Your text here"];
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
paragraphStyle, NSParagraphStyleAttributeName ,
[NSNumber numberWithFloat:0],NSBaselineOffsetAttributeName,
nil]];
self.yourLabel.attributedText = string;

iOS : UILabel multiple lines - second line of label needs to start with 10 pixels

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.

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:

NSAttributedString end of first line indent

I want to have the first line in an NSAttributedString for a UITextView indented from the right side on the first line.
So the firstLineHeadIndent in NSParagraphStyle will indent the first line from the left. I want to do the same thing but from the right in my UITextView.
Here's a screenshot of how I want the text to wrap.
The Setting Text Margins article from the Text System User Interface Layer Programming Guide has this figure:
As you can see, there's no built-in mechanism to have a first line tail indent.
However, NSTextContainer has a property exclusionPaths which represents parts of its rectangular area from which text should be excluded. So, you could add a path for the upper-right corner to prevent text from going there.
UIBezierPath* path = /* compute path for upper-right portion that you want to exclude */;
NSMutableArray* paths = [textView.textContainer.exclusionPaths mutableCopy];
[paths addObject:path];
textView.textContainer.exclusionPaths = paths;
I'd suggest to create 2 different NSParagraphStyle: one specific for the first line and the second one for the rest of the text.
//Creating first Line Paragraph Style
NSMutableParagraphStyle *firstLineStyle = [[NSMutableParagraphStyle alloc] init];
[firstLineStyle setFirstLineHeadIndent:10];
[firstLineStyle setTailIndent:200]; //Note that according to the doc, it's in point, and go from the origin text (left for most case) to the end, it's more a length that a "margin" (from right) that's why I put a "high value"
//Read there: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMutableParagraphStyle_Class/index.html#//apple_ref/occ/instp/NSMutableParagraphStyle/tailIndent
//Creating Rest of Text Paragraph Style
NSMutableParagraphStyle *restOfTextStyle = [[NSMutableParagraphStyle alloc] init];
[restOfTextStyle setAlignement:NSTextAlignmentJustified];
//Other settings if needed
//Creating the NSAttributedString
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:originalString];
[attributedString addAttribute:NSParagraphStyleAttributeName value:firstLineStyle range:rangeOfFirstLine];
[attributedString addAttribute:NSParagraphStyleAttributeName
value:restOfTextStyle
range:NSMakeRange(rangeOfFirstLine.location+rangeOfFirstLine.length,
[originalString length]-(rangeOfFirstLine.location+rangeOfFirstLine.length))];
//Setting the NSAttributedString to your UITextView
[yourTextView setAttributedText:attributedString];

How to format a string for proper display in ios

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.

Resources