IOS: Justify text in UILabel not working in UI - ios

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;

Related

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.

Wrap Text in UISegmentedControl (iOS 9+)

I've seen a few other questions discussing wrapping text/multi-line content in segmented controls but all of the solutions are either super janky or deprecated since 9.0 (so please don't mark this as a duplicate unless you can cite a 9.0+ post with a solution).
All I want to do is wrap text within a segment to the next line... is there a clean/correct way to do this in Objective-C? Thanks in advance!
I haven't tested, but this seems like the rational approach...
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributtes = #{ NSParagraphStyleAttributeName: paragraphStyle };
[mySegmentedControl setTitleTextAttributes:attributes forState:UIControlStateSelected];

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 can I make a UITextView layout text the same as a UILabel?

I have a UILabel that I need to convert to a UITextView because reasons. When I do this, the text is not positioned the same, despite using the same (custom) font.
I found that if I set:
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
This gets the text very close, but if I superimpose the UITextView over top of the UILabel, I see the text positioning get farther apart with each new line.
The UILabel is green, the UITextView is black. This is using NSParagraphStyle to set min and max line height to 15.
I've played with setting the paragraph style and min/max line height, but I haven't been able to match it exactly. I'm not a printer, so I don't necessarily understand all of the font related terms in the documentation for NSLayoutManager and NSTextContainer and all that.
I only need to support iOS 7 and up.
I'm not going to switch to some crazy CoreText-based custom widget or use some random third party library. I'm okay with close enough if I have to. But it seems like there should be some combination of random properties to make them layout the same.
I took the solution for line spacing found at this link and applied it to your issue. I managed to get it incredibly close by adjusting the lineSpacing property. I tested with HelveticaNeue size 13 and managed to get it to line up as shown in the screen shot below.
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = -0.38;
NSDictionary *attrsDictionary =
#{ NSFontAttributeName: [UIFont fontWithName:#"HelveticaNeue" size:13.0f],
NSParagraphStyleAttributeName: paragraphStyle};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attrsDictionary];
I've been able to successfully 'impersonate' a non-editable multiline UILabel (as it happens, in a UITableViewCell subclass) with an equivalent editable multiline UITextView using the following :
_textView = UITextView.new;
_textView.font = _label.font;
_textView.textColor = _label.textColor;
_textView.textAlignment = _label.textAlignment;
_textView.backgroundColor = UIColor.clearColor;
_textView.textContainer.lineFragmentPadding = 0;
_textView.textContainerInset = UIEdgeInsetsZero;
and to make it behave well when doing actual edits, add the following to your UITextViewDelegate:
- (void)textViewDidChange:(UITextView *)textView
{
...
[textView scrollRangeToVisible:NSMakeRange(textView.text.length, 0)];
[textView scrollRectToVisible:[textView caretRectForPosition:textView.endOfDocument] animated:NO];
}

Multiple paragraphSpacing in NSMutableParagraphStyle

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.

Resources