Keep UITextView aligned... Strange problem

Discussion in 'iOS Programming' started by animefx, Jul 28, 2012.

  1. animefx macrumors regular

    Joined:
    May 10, 2005
    Location:
    Illinois
    #1
    I mostly finished with my 1st iPhone app, but I'm having one issue I'm having trouble resolving.

    I have 3 labels and just below that a single UITextView control. I'm looping through a for statement and outputting the data to the TextView using NSString withFormat method. The problem is I want each part to be left aligned with it's corresponding label above. Each time through the loop I use 2 \n\n and then append the string on the previous so I have a list of data in my TextView. It gets kind of sloppy though. I realize I might be able to use a tableview a bit more effectively for something like this but as far as I can tell you can make tableviews transparent and that's how I have my TextView currently.

    Any suggestions?
     
  2. Reason077 macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #2
    Does the next need to be editable? If you're just displaying text, you're probably better off using UILabels instead of UITextView. UILabels can be multi-line.
     
  3. animefx thread starter macrumors regular

    Joined:
    May 10, 2005
    Location:
    Illinois
    #3
    labels is a good idea but the problem is I could be displaying a little data or a lot of data and I want the user to be able to scroll through them. I suppose I could create a scroll view and create the labels during runtime for every piece of data but that would probably be a pain
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Maybe a screenshot of what the issue is would help.
     
  5. animefx thread starter macrumors regular

    Joined:
    May 10, 2005
    Location:
    Illinois
    #5
    I wrote a class called return a certain number of spaces to put in-between other objects in my textview depending upon the length of what's in my variables. Works flawlessly:

    Code:
    #import "FormatSpacing.h"
    
    @implementation FormatSpacing
    
    - (NSString *) LengthOfIncorrect: (int) a{
       
        switch (a) {
            case 1:
                return @"          ";
                break;
            case 2:
                return @"        ";
                break;
            case 3:
                return @"      ";
                break;
            default: //aka case 0, 4, 5, etc
                return @"error";
                break;
        }
        
    }
    
    
    - (NSString *) LengthOfCorrect: (int) b{
        
        switch (b) {
            case 1:
                return @"         ";
                break;
            case 2:
                return @"       ";
                break;
            case 3:
                return @"     ";
                break;
            default: //aka case 0, 4, 5, etc
                return @"error";
                break;
        }
        
    }
    
    @end
    
     
  6. Reason077 macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #6
    So you want each bit of text to be individually scrollable? That doesn't sound like a good UI design!

    But if that's what you really want to do, you can put each label inside it's own scrollview. (There's no reason you can't do this in Interface Builder, if there's a fixed number of labels).

    However, I think a better approach, in terms of UI, would be to use a UITableView. If you don't want the table to get too long, put a 2-3 line summary of the text in each table cell (UILabel can automatically display a "..." at the end). When the user taps on the cell, push a detail view controller that contains the full text.
     
  7. animefx thread starter macrumors regular

    Joined:
    May 10, 2005
    Location:
    Illinois
    #7
    Its just 1 textview divided into 3 colums... the reason I went with the textview vs. tableview is I need the textview to be transparent. It's important they see all the information on the screen at once vs. having to go through multiple screens.

    I'll post a screenshot later tonight so you guys can see what I mean. I'm fickle about design too, but I think you guys will approve :)



    ----------

    I just noticed I could use "short" instead of int in those methods. Save a little bit of memory.
     
  8. Reason077, Jul 31, 2012
    Last edited by a moderator: Jul 31, 2012

    Reason077 macrumors 68000

    Reason077

    Joined:
    Aug 14, 2007
    #8
    Hmm, I wouldn't recommend that. In many cases you won't actually be saving any memory because of alignment. And even when you do, the savings aren't significant until you have at least 10s or 100s of thousands of instances.

    It's also a bad habit to get in to, because you'll get weird hard-to-diagnose problems one day when you accidentally do overflow a short...
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    There's probably no reason to ever use short for anything.

    Stack memory is free, as long as you don't run out. The difference in stack memory used for a handful of shorts or ints isn't significantly different.
     

Share This Page