Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 520
Linh Avatar asked Mar 25 '16 10:03

Linh


2 Answers

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

like image 70
wj2061 Avatar answered Nov 16 '22 17:11

wj2061


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.

like image 2
KeyMaker00 Avatar answered Nov 16 '22 16:11

KeyMaker00