Adding strings with wordwrap to a custom UIView

Discussion in 'iOS Programming' started by mraheel, Nov 11, 2009.

  1. mraheel macrumors regular

    Apr 18, 2009
    Hey guys,

    I've finally accomplished making a custom view in Code and no xibs.

    I cant go on forward now, I got an array with Strings. Each string could be a few words to large sentences.

    I want my custom view to show those strings.

    About the custom View: the width is fixed and so is the height(it shouldnt be)

    How can i add the strings and if necessary wordwrap them to continue in the next line, and have a gap between 2 strings?

    the challenge is to Calculate the height of the strings if wordwrapped.
    then calculate the gaps between each string(this should be fixed) and then present that as the customView Height.

    All this has to be done in drawRect{} ??
  2. bredell macrumors regular

    Mar 30, 2008
    Uppsala, Sweden
    As far as I know there's no method that calculates the word wrapping for you. If you roll your own you can use the NSString method sizeWithFont:constrainedToSize:lineBreakMode which calculates the bounding rectangle for the text image.

    This will all be a lot easier if you use the standard classes that provide word wraps for you. Why can't you use a UIWebView?
  3. mraheel thread starter macrumors regular

    Apr 18, 2009
    Well, i intend to create something more after and if i accomplish this. I think webview should be a wastage of resources?? and there might be more than 8 if I use and slap it on the custom view.

    Im able to accomplish some things, heres the code
    UIFont *cellFont = [UIFont fontWithName:@"Verdana" size:16];
    CGSize constraintSize = CGSizeMake(boxRect.size.width - 40.0f, MAXFLOAT);
    //sttr is the NSString
    CGSize labelSize = [sttr sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];	
    	CGRect textRect = CGRectMake(13.0 +10, 40, boxRect.size.width - 40, labelSize.height + 100);
    	[sttr drawInRect:textRect
    			withFont:[UIFont systemFontOfSize:15.0]
    Now this works fine, But only if I was dealing with only a single string. I can simply calculate the height of the string and make it the Boxheight (customView height). Since thats not the case..

    I have to calculate each string heights, add them up, present it as the boxheight. Then draw all the strings, each separated atleast by 20.0 and then draw them at a Yfloat below the previous string..

    Im sure, theres a way, but its hard to get through it.. with all the numbers and stuff..
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    What about considering using a UITextView or maybe even a UITableView (with or without separators)?
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    What you're describing is the equivalent of having a number of labels as subviews with one label for each string. You could do it that way. You just need to add each string to a label, figure out the height of that string and adjust the height of the label (or use sizeToFit) and lay out the labels with your desired gap between them.

    If you don't want to use labels then you need to do the same thing but draw the strings yourself. First figure out the bounding box for each string. Then draw the first string, then the second string below the first etc. It's a little tedious but I don't think it's complicated. You should save the bounding rects for each string outside of drawRect. Just do the drawing in drawRect.
  6. mraheel thread starter macrumors regular

    Apr 18, 2009
    Yea, i did just that, I wouldnt know what good would do adding strings to UILabel. I did the similar thing for strings
    I looped through the array of strings, calculated the height required, then saved it and then re-added that with the current height of string to get the Y Axis. I managed that,

    But is this the right way to do it?

    I kept the custom view height static for the moment, since i'd have to again calculate the height of all strings and present it early on in DrawRect{}.

    Theres a lag on tapping though..(initialising, bringing to view).
  7. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Sure. You could save the bounding box that you draw each string into or the top left corner or whatever info you use to draw each string.

    Not sure about that. You need to profile it I guess. Or if you like just comment out the contents of your drawRect method or parts of it bit by bit and see if it has any effect.

Share This Page