IOS: Justify text in UILabel not working in UI
Asked Answered
B

5

9

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 enter image description here

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 enter image description here

I don't know why this happened to me. Please give me some instruction for fix it. Any help would be great appreciated

Boatbill answered 25/3, 2016 at 10:31 Comment(2)
It's not suppose to work on the last line. Everything is normal.Dairying
In first look, i thought you answering or questioning.. because your post says you already getting what you asking.. lol ;)Maxson
G
5

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 .
enter image description here

Your UILabel will work just fine.
enter image description here

Gunfight answered 3/4, 2016 at 10:8 Comment(1)
thank you so much. Change the indent value make my UILabel jutifyBoatbill
T
2

This should work. Here is what I get from the simulator: enter image description here

What I've done:

  1. Drag & drop an UILabel on the storyboard and add some constrains, and colours as well.
  2. Set the text as "attributed"
  3. Put your text in the text field.
  4. Click on justify.
  5. Change the police
  6. Numbers of lines 0.

At this point you should have this: enter image description here

Now from the storyboard to your controller add an IBOutlet (Ctrl + drag it to the top of your controller). It should be like this: enter image description here

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: enter image description here

P.S: With xCode 7.2 works definitely. It works for me on both version.

Trimetric answered 25/3, 2016 at 10:47 Comment(8)
which version of xcode use using? how you achieve it.Boatbill
Version 7.3 (7D175) but should work as well with version 7.2. Juste one minute, I m typing more info ;)Trimetric
i'm sure 100% that not work in Xcode Version 7.2 (7C68). i will update Xcode nowBoatbill
i apologize for my confusion. it still happensBoatbill
@PhanVănLinh: no worries, have a look at my added screenshot.Trimetric
@KeyMarker00 I have update my post, with my Xcode imageBoatbill
I have to go home now. I will check your update later. Thank you so much for your timeBoatbill
Ah! because it's already justified. If you stretch your label box a bit more you will see that the word "accounts" will be on the first line.Trimetric
B
1

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;
Boatbill answered 28/3, 2016 at 10:15 Comment(2)
Hi Phan. This is the same solution I've given you but yours is in Obj-C. A bit of recognition would be gread ;(Trimetric
@Trimetric I miss your Swift code. It same my code in Obj-C right?. I don't know swift, sorry for my mistakeBoatbill
I
1

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

Ignatz answered 2/4, 2016 at 12:51 Comment(0)
C
0

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;
Counteraccusation answered 3/4, 2016 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.