How to center a UILabel on UIView

前端 未结 7 1984
日久生厌
日久生厌 2020-12-13 06:12

How can I center the UILabel on the UIView? I am using the following code

float width = weatherView.bounds.size.width; 
float height = weatherView.bounds.siz         


        
相关标签:
7条回答
  • 2020-12-13 07:00

    Something like this should do the trick... Make sure that your label is set to have centered alignment and is sufficiently big/wide to handle your text string.

    float viewWidth = weatherView.frame.size.width;
    float viewHeight = weatherView.frame.size.height;
    float labelWidth = label.frame.size.width;
    float labelHeight = label.frame.size.height;
    
    float xpos = (viewWidth/2.0f) - (labelWidth/2.0f);
    float ypos = (viewHeight/2.0f) - (labelHeight/2.0f);
    
    [label setFrame:CGRectMake(xpos,ypos,labelWidth,labelHeight)];
    

    I think that should do what you are asking for.

    0 讨论(0)
提交回复
热议问题