pixelfreak
pixelfreak

Reputation: 17834

UIView layout behavior

I am still pretty new with how layout behaves in Cocoa. Say I have a UIView with UIActivityIndicator and UILabel as its subviews. The UILabel frame is as big as the UIView and it has its textAlignment set to center. The text says "Loading xxx...", where xxx is a string that can change depending on context.

Now, how do I make it so that my UIActivityIndicator is always placed exactly to the left of the label no matter how long the text gets? Remember the text is center-aligned.

Plz let me know if you need more clarifications.

Upvotes: 0

Views: 345

Answers (1)

Armentage
Armentage

Reputation: 12753

A simple way to implement this is to calculate the width of the text in you UILabel, and then use the results to position your UIActivityIndicator

// width & height of your activity indicator
CGFloat wai = 20, hai = 20;
CGSize textSize = [[label text] sizeWithFont:[label font]];
CGFloat w = textSize.width;
CGFloat ax = ((WIDTH_OF_THE_DISPLAY - w) / 2) - wai);
activityIndicator.frame = CGRectMake(ax, (HEIGHT_OF_DISPLAY- hai) /2, wai, hai);

Upvotes: 1

Related Questions