TextView, TextField, or Label?!?!?

Discussion in 'iOS Programming' started by Abrexas, Dec 28, 2011.

  1. Abrexas macrumors member

    Jul 29, 2011
    Hey guys,

    I am writing an app for my iPhone that has a pretty simple concept. Two buttons and one display. One button adds a word to the display, and the other removes the last word.

    I have all that stuff figured out. What I need however, is for the display to scroll horizontally. I don't know if this is possible with Label or TextFeild, but I feel like I would prefer to use one of these.

    There is no need for the user to be able to edit the display, so I don't want a keyboard to pop up. (I already know how to do this, just letting you know how it works)

    So that's pretty much it. Any suggestions as to what one I should use and where I can find the methods that I will need to make this work?

    Thank you all very much in advance ^_^
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    AFAIK, you can't really have any of those scroll horizontally...

    I'd try making a UIScrollView that only allows horizontal scrolling, and then put a UILabel inside it to achieve the effect you want...

    Let me know how it well it works...
  3. Scott90 macrumors 6502

    Jul 14, 2008
    If you need the label to scroll automatically (similar to a marquee in HTML), you could add an animation to it using UIView's class method animateWithDuration:animations:. If you want it to scroll when the user pans or flicks, you could try something with gesture recognizers or put the label in a scrollview.
  4. Abrexas thread starter macrumors member

    Jul 29, 2011
    Sorry for the late response. Your suggestion worked! So I owe you a thanks ^_^

    What I ended up doing to get it to work was a set a UIScrollView where I wanted to see the string, and inside of that I put an empty label. Then when the button click adds words to the string, i set the label to get bigger and I set the scroll range to the same width as the label so that you could never scroll much further than the string.

    I put the whole thing on top of an empty textField (with user activity turned off) so that it looks like it is in a UITextField =P

    If you want to see the code for this to get an idea than let me know. I don't have access to the code right now but I will certainly post it for you.

    Thanks again! ^^
  5. ArtOfWarfare macrumors G3


    Nov 26, 2007
    Great to hear it worked so well!

    Just curious... what happens if instead of using a separate UILabel and UIScrollView you used a single UITextView? My understanding is a UITextView is a subclass of a UIScrollView, so you might be able to modify the contentSize of it as more text is added...
  6. Abrexas, Dec 31, 2011
    Last edited: Dec 31, 2011

    Abrexas thread starter macrumors member

    Jul 29, 2011
    That is a great question, and unfortunately I do not have the answer haha. Every time I tried to use a TextView it would always add a new line, even when I tried my hardest to tell it not to haha.

    What I have works exactly how I want it to, almost, so I think I will stick with it so I can move onto other parts of the program. Might look into your suggestion after though. ^^

    I am stuck in one more spot however. As the label is appended, the scroll is still locked onto the beginning of the label, and I have to manually scroll over. does anyone know how to make the scroll lock to the end of the label? so like as the label gets longer, you always see the newer text unless you scroll back manually?

    Thanks for all the responses!
    - Steven French

    Just figured it out ^^
    To do this I just used the setContentOffset:CGPointMake( x, y ); method and set the x value to the end label length minus the size of the ScrollView.

Share This Page