Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How can I justify text alignment on my app.

Hi, I'm working on my app, is quite basic, like the fun facts app. The problem is I'm trying to making the alignment of my text to be justify, or forced the text to be squared. But the utilities panel shows me only 3 alignments, left, center and right. How can I choose justify or if is there a code for my UILabel to do that, I have no idea, Thanks

4 Answers

For label instances you are right in saying there is no justify option for text alignment in the right and attributes menu. What I suggest is to either put your text in a textView rather than a Label if its a lot of text or for example set your UILabel text alignment programmatically like so:

@IBOutlet weak var exampleLabel: UILabel!

override func viewDidLoad() {
    //include all of your code

    exampleLabel.textAlignment = NSTextAlignment.Justified

}

This should do that trick, don't be afraid when programming to type "."(full stop) after object to see what you can work with. The beauty of xcode is it helps you a long the way

Also don't forget to ALT+Click to find out what it is you are doing

Hey Dominic, thanks for the code but I did tried this code and doesn't work. I really don't know why but it doesn't! Here's my code:

@IBOutlet weak var factLabel: UILabel!

@IBOutlet weak var funFactButton: UIButton!


let factBook = FactBook()
let colorWheel = ColorWheel()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    factLabel.text = factBook.randomFact()

   factLabel.textAlignment = NSTextAlignment.Justified

}

When I do factLabel.textAlignment = NSTextAlignment.center, or left, or right the code does and it is alright but then when I say justify the text goes to the right.

I saw something about Mutable Paragraph, I just don't know how to apply to my code but here a code that does work: (I got from a site but is not my code)

var paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = NSTextAlignment.Justified

var attributedString = NSAttributedString(string: sampleText, attributes: [ NSParagraphStyleAttributeName: paragraphStyle, NSBaselineOffsetAttributeName: NSNumber(float: 0) ])

Thanks for helping me.

Hey again Fabiano Albuquerque,

You are right I built a project to see and I can't find a simple way to do or (as of this moment haven't found one way at all). My advice stands with replacing your label with a text view, they can do the same thing. Below I have linked to my github with a project I created demonstrating the two label and textView text alignment justify. If you take a look I have applied text alignment for both items programmatically, text view however actually gives you the option to justfy (I was just demonstrating the annoyance of the label)

https://github.com/dombryan94/labelAlignment

It might be worth asking the likes of Pasan Premaratne to see what he thinks

Sorry I couldn't help to much

It's alright, you did help me. Thanks a lot! I'll try all this and post here if I get it right for somebody else facing the same problem.

Thanks again.