calculating TextView height

Discussion in 'iOS Development' started by cybohemia, May 5, 2009.

  1. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Given a font, a string, and the width of a TextView, is there a way to figure out the minimal height it should be in order to show the entire string?
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    [someString sizeWithFont:yourFont] to get the size of the font for the string that you want to use for your textView.

    If the width is larger than the width of your view, figure out how many times larger. Then, its as simple as

    textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, fontHeight * calculatedNumberOfLines];


    Sorry, no sample code, but it's so simple you should be able to figure it out.
  3. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Thanks for the help!

Share This Page