Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bktz

macrumors newbie
Original poster
Jun 23, 2009
6
0
Hi i am currently working on an application in my spare time (my hobby) that is very simple. It starts out as a list and then you select one item from the list and it tells you something about that item. The problem that i am having is that i am using UITextView to display the text information but i want to be able to format the text in such a way that i can make one line bold and then the next regularly formatted. The only reason i used UITextView in the first place is because it allows for scrolling through the text and i would like to keep that. Is it possible to do this using UITextView? If not is there another way for me to do this?

PS. I am still fairly new to program so please be specific in your answers and of course any help is greatly appreciated.

Here is an example of what i am trying to do (The make this bold was added in photoshop just to be clear):

http://img199.imageshack.us/img199/8769/picture1copyn.png
 
This doesn't necessarily help solve your problem but it does get you on the right path...

From the UITextView Class Reference:

This class [UITextView] does not support multiple styles for text. The font, color, and text alignment attributes you specify always apply to the entire contents of the text view. To display more complex styling in your application, you need to use a UIWebView object and render your content using HTML.
 
Thanks for the reply, but i read that already as well. But that is somewhat why i have a problem. I know it does not allow me to use multiple formatting but i wanted to know if there was a way that it could still be done. Or if i have to stop using UITextView and if i have to stop using UITextView what could u recommend that i do?
 
If this is only to display the text look at using UIWebView. In that case you need to generate an html file with your content.

Also, look at using UITableView. You can display your text in rows using labels. One label at the top of the row can use a bold font and the second label can use a non-bold font. Also, you could use a grouped table and have your headings be the section headings.

You could also just place multiple labels into a scrollview.

UITextView doesn't support multiple styles. There's no way that I know to make it do so.

If your text needs to be editable then I don't know offhand what you should do.
 
I'd go with the UITableView. The web view is acceptable though if you just want it done quickly.
 
The text does not need to be editable so im just going to go with webview but a little problem (well not problem but just pain/bother) that im having is that ALLLL the html has to be in a single line. I know this may be a stupid question to ask to sum but is there a way that i can write my html in more than one line?
This is the code that i have now:
Code:
- (void)loadView
{
	CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
	UIView *view = [[UIView alloc] initWithFrame:appFrame];
	view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
	self.view = view;
	[view release];
	
	CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
	webview2 = [[UIWebView alloc] initWithFrame:webFrame];
	webview2.backgroundColor = [UIColor whiteColor];
	webview2.scalesPageToFit = YES;
	[self.view addSubview:webview2];
	
	NSString *html = @"<html><head><meta name=""viewport"" content=""width=320""/></head><body><h1>Header</h1><p>This is some of my introduction..BLA BLA BLA!!</body</html>";
	[webview2 loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.apple.com"]];

}
 
Your html doesn't have to be all in one line. Just use \n as an end of line character.

You may find it simpler to use a mutable string. That way you can build up the contents of the html bit by bit.
 
Your html doesn't have to be all in one line. Just use \n as an end of line character.

You may find it simpler to use a mutable string. That way you can build up the contents of the html bit by bit.

thanks for \n, it makes my life alot easier. About the mutable string, how does that work? i checked the UIWebView Class Refrence on the apple website but couldnt find much information about it. (Link to the UIWebView Class Refrence)
 
ok so i read over it but im still just having a small problem that i was hoping that i could get a little bit of help with. I still dont fully understand how i can have multiple lines of code with html using /n. I still get an error. Would someone be able to give me a simple example of how i would be able to do it.
This is what i had that was not working:
Code:
- (void)loadView
{
	CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
	UIView *view = [[UIView alloc] initWithFrame:appFrame];
	view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
	self.view = view;
	[view release];
	
	CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
	webview2 = [[UIWebView alloc] initWithFrame:webFrame];
	webview2.backgroundColor = [UIColor whiteColor];
	webview2.scalesPageToFit = YES;
	[self.view addSubview:webview2];
	
	NSString *html = @"<html><head><meta name=""viewport"" content=""width=320""/></head><body><h1>Header</h1><p>This is some of my introduction..BLA BLA BLA!!\n;
	</body</html>";
	[webview2 loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.apple.com"]];

}
 
You need to escape the double quotes in that string. I think you're trying to do it like this "" but you need to do it like this \"
 
and then it should allow me to code the html string on multiple lines?
 
and then it should allow me to code the html string on multiple lines?

Yes, but that sentence can have several meanings. You can have multiple lines in your html string separated by newlines, \n. You can build up your string by using a mutable string where each chunk of the string that you add is another line, like this:

Code:
NSMutableString* s = @"<html><head>\n";
[s appendString:@"<meta name=\"viewport\" content=\"width=320\"/>\n"];
[s appendString:@"</head><body>\n"];
// add the body here...

If you were to save this to a file or print it out it would have multiple lines because of the embedded newlines.

You also can build a string in code using multiple lines in your source file like this

Code:
NSString* s = @"first line\
second line\
last line\
";

The \ character at the end of the line is a continuation character.
 
You don't seem to be doing any variable substitution so it must surely be easier to store your HTML in a separate file and load it from disk (you could use [NSString stringWithContentsOfFile])?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.