UITextView formatting

Discussion in 'iOS Programming' started by bktz, Jun 23, 2009.

  1. bktz macrumors newbie

    Joined:
    Jun 23, 2009
    #1
    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
     
  2. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #2
    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.
     
  3. bktz thread starter macrumors newbie

    Joined:
    Jun 23, 2009
    #3
    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?
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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.
     
  5. Saladinos macrumors 68000

    Saladinos

    Joined:
    Feb 26, 2008
    #5
    I'd go with the UITableView. The web view is acceptable though if you just want it done quickly.
     
  6. bktz thread starter macrumors newbie

    Joined:
    Jun 23, 2009
    #6
    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"]];
    
    }
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    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.
     
  8. bktz thread starter macrumors newbie

    Joined:
    Jun 23, 2009
    #8
    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)
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Give the class reference for NSMutableString a look instead.
     
  10. bktz thread starter macrumors newbie

    Joined:
    Jun 23, 2009
    #10
    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"]];
    
    }
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    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 \"
     
  12. bktz thread starter macrumors newbie

    Joined:
    Jun 23, 2009
    #12
    and then it should allow me to code the html string on multiple lines?
     
  13. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    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.
     
  14. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #14
    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])?
     

Share This Page