Placing text on UIView in custom location

Posted by JAM on Stack Overflow See other posts from Stack Overflow or by JAM
Published on 2012-06-15T21:15:09Z Indexed on 2012/06/15 21:16 UTC
Read the original article Hit count: 146

Filed under:
|

I am trying to calculate a position to place label on the screen. The goal is to place "word" label in lower right corner of the first square block

If yellowish square is defined as

myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 70, 70)];
[self.view addSubview:myView];
[myView setBackgroundColor:[UIColor colorWithHexString:@"FFFFEC"]];

Using it, i'd like to place a label in it's lower right corner

l1 = [[UILabel alloc] init];
[l1 setText:@"word"];
[l1 setFrame:CGRectMake(myView.frame.origin.x + myView.frame.size.width, 
                        myView.frame.origin.y + myView.frame.size.height,
                       700, 700)];
[l1 setFont:[UIFont fontWithName:@"Arial" size:10.0]];
[l1 setBackgroundColor:[UIColor colorWithHexString:@"CCFFFEC"]];
[l1 setTextAlignment:UITextAlignmentRight];
[l1 sizeToFit];

This is what happens:

The matter here obviously, is in a correct offset. Knowing the font and size of text, how can one correctly calculate it?

enter image description here

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios