hacker
hacker

Reputation: 8947

how to position the dynamically sized label always the center in a view of specific width?

I had an application in which i had a button which needs to be located at the end of a dynamic label as like appending it ,I had tried this but its showing some times ok (correct position)and some times not ok(not at the right position)manner.I am doing like this.also i need to position the label always the center `

label = [[UILabel alloc]init];
    [label setFrame:CGRectMake(105,9,160, 30)];
    [label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
    label.textAlignment = UITextAlignmentLeft;
    label.numberOfLines = 1;
    label.text=myString;
    [[self.view viewWithTag:300] addSubview:label];
    CGSize expectedLabelSize = [label.text sizeWithFont:label.font];
     CGRect newFrame = label.frame;
    newFrame.size = expectedLabelSize;
    if(newFrame.size.width>160.0)
      newFrame.size.width=150.0; 
      label.frame = newFrame;
    [label setCenter:CGPointMake(300/2,40/2)];
     button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(newFrame.origin.x+newFrame.size.width,13,20,20)];
    [button setBackgroundImage:[UIImage imageNamed:@"arrow.png"] forState:UIControlStateNormal];

`can anybody point me in where i am going wrong?

Upvotes: 0

Views: 152

Answers (1)

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

Set this frame to button ..

[button setFrame:CGRectMake(newFrame.origin.x+newFrame.size.width,13,20,20)];

Your y coordinate is fixed so you need to change it dynamically

Upvotes: 1

Related Questions