Working with UILabels

Dears,


How can I align some UILabels to the left?

I just want to align... it's not necessary a constraint. Is it possible? How can I do it?


And also,

Is there any way to clear the content of all UILabels?


If it's not possible, how can I create a FOR LOOP to clear the content of my UILabels.

They are named as: lbCenter01, lbCenter02, lbCenter03, lbCenter04...


Thank you so much

Best regards,


Bruno Lelli

Accepted Reply

How can I align some UILabels to the left?

I just want to align... it's not necessary a constraint. Is it possible? How can I do it?


Interface Builder is just for that:

- set the first label position : control drag to its supervies and select leading (and probably top as well)

- then for each other label, control-drag to the label above it ans select leading and vertical space


Is there any way to clear the content of all UILabels?

Yes, if the labels are directly a subView of the controller's view (it is possible otherwise but a bit more complex)


        for aView in self.view.subviews where aView is UILabel {
            aView as! UILabel).text = ""
        }


Another way would be to create a collection of IBOutlets ;

@IBOutlet var lblCenter: [UILabel]!


and you reset with :

for index in 0..<lblCenter.count {

lblCenter[index] = ""

}

Replies

How can I align some UILabels to the left?

I just want to align... it's not necessary a constraint. Is it possible? How can I do it?


Interface Builder is just for that:

- set the first label position : control drag to its supervies and select leading (and probably top as well)

- then for each other label, control-drag to the label above it ans select leading and vertical space


Is there any way to clear the content of all UILabels?

Yes, if the labels are directly a subView of the controller's view (it is possible otherwise but a bit more complex)


        for aView in self.view.subviews where aView is UILabel {
            aView as! UILabel).text = ""
        }


Another way would be to create a collection of IBOutlets ;

@IBOutlet var lblCenter: [UILabel]!


and you reset with :

for index in 0..<lblCenter.count {

lblCenter[index] = ""

}

UILabel does have a textAlignment property that you can set to .left in code, or on the storyboard.

of course you would have corrected yourself:


for index in 0..<lblCenter.count {

lblCenter[index].text = ""

}


and added the missing opening bracket

(aView as! UILabel).text = ""