0

How to set label and its dynamic text to center of uiview.

 myView = [[UIView alloc] initWithFrame:CGRectMake(0,20,self.view.frame.size.width,32)];
    [myView setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubView:myView];

    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(myView.frame.size.width/2,10,0, 12)];
    [myLabel setFont:[UIFont Helvetica12]];
    [myLabel setText:[listDiction objectForKey @"dynaimicText"];
    [myLabel sizeToFit];
    myLabel.center = myView.center;
    [myView addSubview:myLabel];

Need your advice on this.

KkMIW
  • 1,092
  • 1
  • 17
  • 27

1 Answers1

0

replace your lines with this order,

[myView addSubview:myLabel];
[myLabel sizeToFit];
myLabel.center = myView.center;

but wait, if your text is dynamic then, you've to add, myLabel.numberOfLines = 0; (for multiple line) also if your text size is > myView height then text's will be clipped when it'll increase myView height.

For that, you've to calculate label text size and resize your myView before creating myLabel.

This question Calculating number of lines of dynamic UILabel (iOS7) may help.

Community
  • 1
  • 1
Hemang
  • 26,840
  • 19
  • 119
  • 186
  • text is dynamic but maximum length for dynamic data is 15 so mylabel.numberoflines is not need. after replace UILabel is not visible at all. – KkMIW Nov 15 '14 at 08:50