How to count no of lines at run time in UILable?

Discussion in 'iOS Programming' started by Narendar Singh, Jun 25, 2012.

  1. Narendar Singh macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #1
    I have a UILabel, I want check no of lines at run time based on the text I display in label.

    Some times I have to display short text and some time very long string. In very long text I have to display "read more" button but in short string only the I have to display string. How to achieve this.

    I have to display normally 3 lines but if it goes more than 3 lines then I need to display "read more button"

    Thanks
     
  2. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #2
    One way to accomplish this would be to use textRectForBounds:limitedToNumberOfLines: to determine if the text fits in the area for 3 lines if it doesn't then you can display the show more button.
     
  3. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    You can calculate the size of your text based upon a string, if this exceeds above a certain number, you can set it to read more. I think this is an approach you could take a look at!
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Use the sizeWithFont: methods in NWStringDrawing.h to determine the height of a given string.
     
  5. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #5
    Thanks Guys,

    My approach is, I am using sizeToFit() it returns the actual number of lines based on its contents, for say if content is just small text like "My name is Narendar" then it returns 1 line after using sizeToFit and if content is more it returns maximum no of lines.

    Thanks :)
     

Share This Page