IOS: Adjust UIButton height depend on title text using Autolayout?

前端 未结 4 1832
囚心锁ツ
囚心锁ツ 2021-01-02 12:40

I have a UIButton and it can change the title at the runtime. Therefore, I want to increase the UIButton height depend on the title text for displa

4条回答
  •  长情又很酷
    2021-01-02 13:22

    The point is that if you set sizeToFit property, then the text will always be in one line and the width of the button will increase unless you put a next-line sign \n to explicitly say that you want it to be several lines.

    You put '\n' in the end of the first line like "line \n line" which represents

    line
    line
    

    If you want to have two different string values (with \n positioned differently) for Portrait and Landscape you can check the orientation condition using UIDeviceOrientation (UIDevice.currentDevice.orientation) described here and set a string value depending on the orientation of the device

提交回复
热议问题