Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 11, 2009, 03:23 AM   #1
mraheel
macrumors regular
 
Join Date: Apr 2009
Adding strings with wordwrap to a custom UIView

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{} ??

Last edited by mraheel; Nov 11, 2009 at 03:24 AM. Reason: subscribe..
mraheel is offline   0 Reply With Quote
Old Nov 11, 2009, 04:16 AM   #2
bredell
macrumors regular
 
Join Date: Mar 2008
Location: Uppsala, Sweden
Send a message via Skype™ to bredell
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?
bredell is offline   0 Reply With Quote
Old Nov 11, 2009, 06:32 AM   #3
mraheel
Thread Starter
macrumors regular
 
Join Date: Apr 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
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]
	   lineBreakMode:UILineBreakModeWordWrap
		   alignment:UIBaselineAdjustmentAlignBaselines];
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..
mraheel is offline   0 Reply With Quote
Old Nov 11, 2009, 09:49 AM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
What about considering using a UITextView or maybe even a UITableView (with or without separators)?
__________________
dejo is offline   0 Reply With Quote
Old Nov 11, 2009, 10:30 AM   #5
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Nov 11, 2009, 01:29 PM   #6
mraheel
Thread Starter
macrumors regular
 
Join Date: Apr 2009
Quote:
Originally Posted by PhoneyDeveloper View Post
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.
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).
mraheel is offline   0 Reply With Quote
Old Nov 11, 2009, 03:17 PM   #7
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
Quote:
But is this the right way to do it?
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.

Quote:
Theres a lag on tapping though..(initialising, bringing to view).
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.
PhoneyDeveloper is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How do I reference a subview in a custom UIView class? moonman239 iPhone/iPad Programming 1 Nov 7, 2013 01:38 PM
Adding a Custom Phone Label when adding or Editing contacts Chris Kavanagh iOS 7 0 Oct 20, 2013 04:28 PM
Adding a custom ring tone GIZBUG iPhone 0 Oct 17, 2012 10:26 AM
Adding/Modifying custom words programatically to iOS dictionary from a csv file ashwinr87 iPhone/iPad Programming 1 Sep 27, 2012 11:38 AM
Archiving Custom UIView Ides iPhone/iPad Programming 14 Aug 8, 2012 05:44 PM

Forum Jump

All times are GMT -5. The time now is 01:10 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC