Get the NSString height

前端 未结 2 696
眼角桃花
眼角桃花 2020-12-10 04:35

I have an NSString, and I want to know its height to create an appropriate UILabel.

Doing this

NSString *string = @\"this is an example\"; 
CGSize si         


        
2条回答
  •  一整个雨季
    2020-12-10 05:29

    According to the Documentation

    This method returns the width and height of the string constrained to the specified width. Although it computes where line breaks would occur, this method does not actually wrap the text to additional lines. If the size of the string exceeds the given width, this method truncates the text (for layout purposes only) using the specified line break mode until it does conform to the maximum width; it then returns the size of the resulting truncated string.

    You should to use -[NSString sizeWithFont:constrainedToSize:lineBreakMode:] which has similar behavior but you can uuse CGFLOAT_MAX as the height in the size passed in.

提交回复
热议问题