Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

hintText for TextArea?

There's no hintText property for Titanium.UI.TextArea (that I can find).

I could set value and remove it on focus, but that seems like a clunky way of going about things.

I want it to work identically to Titanium.UI.TextField hintText.

Any Best Practice available?

Thanks!

/J

— asked August 9th 2010 by Joacim Boive
  • hinttext
  • iphone
  • textarea
  • textfield
0 Comments

7 Answers

  • Accepted Answer

    Hi there

    I puzzled over this a while back, it seems that the textArea doesn't support a hint text property in the iPhone SDK. Yes it's inconsistent, but if you really want it you 'could' display a label over the textArea with the hint text when the textArea is blurred and contains no text. However, you may also need to setFocus to the textArea in the label click event - in case the user clicks directly over the label.

    Note: using a label means that hint text could be formatted differently (i.e. greyed-out and italicised)

    Cheers,
    Chris

    — answered August 10th 2010 by Chris Reed
    permalink
    0 Comments
  • Just another example of how to do this manually:

    var textArea = Titanium.UI.createTextArea({
        value : 'Enter TExt',
    });
    textArea._hintText = textArea.value;
    
    textArea.addEventListener('focus',function(e){
        if(e.source.value == e.source._hintText){
            e.source.value = "";
        }
    });
    textArea.addEventListener('blur',function(e){
        if(e.source.value==""){
            e.source.value = e.source._hintText;
        }
    });
    
    — answered May 3rd 2012 by Dan Boorn
    permalink
    5 Comments
    • Works quite well! Thanks

      — commented May 19th 2012 by Dario Marcelino
    • Thanks!! It's working well.

      — commented September 26th 2012 by Anand T
    • how to change text area color while entering text from grey to black

      — commented June 24th 2013 by saravanan selvam
    • Thank you very much

      — commented October 27th 2014 by Srikanth P
    • thank you..it's working..

      — commented December 21st 2014 by Antonio Giampetruzzi
  • Here's some code that works rather well:

    var value = "Whatever is going into the text area";
    var hintText = "Notes";
    
    var field = Ti.UI.createTextArea({
        value: value,
        color: 'blue'
    });
    
    var hint = Ti.UI.createLabel({
        text: hintText,
        color: 'gray',
        textAlign: 'left',
        left: '3%',
        top: '20%',
        height: '20%',
        width: '100%',
        backgroundColor: 'transparent',
        touchEnabled: true
    });
    
    field.add(hint);
    
    if (field.value.length > 0) {
        hint.hide();
    }
    
    //*** Hint text appears when TextArea is blank, disappears when text is entered
    field.addEventListener('change', function(e) {
        if (e.source.value.length > 0) {
            hint.hide();
        } else {
            hint.show();
        }
    });
    //*** Make sure that TextArea gets focus if they tap the hint label
    hint.addEventListener('click', function(e) {
        field.focus();
    });
    
    — answered August 5th 2011 by Mark Pemburn
    permalink
    0 Comments
  • I am using Alloy and this is my code.

    var hint = 'Please make the description as accurate as possible.';
    
    hintText(hint);
    
    $.mytextAreaID.addEventListener('focus', function(e) {
        if($.mytextAreaID.value == hint){
            $.mytextAreaID.setValue("");
        }
    });
    
    $.mytextAreaID.addEventListener('blur', function(e) {
        hintText();
    });
    
    
    
    function hintText(hintText){   
        if ($.mytextAreaID.value.length == 0) {
            $.mytextAreaID.setValue(hint);
        }
    
    }
    
    — answered July 5th 2013 by Keith Levi Lumanog
    permalink
    2 Comments
    • @Keith Levi Lumanog - thanks for the idea, it works but has a few small issues. Here's my completed code based off yours:

      // iOS requires custom code for hints on textAreas
      var hint = "Type your description here";
      hintText(hint);
      $.messageTextArea.addEventListener('focus', function(e) {
      if ($.messageTextArea.value == hint){
             $.messageTextArea.setValue("");
             $.messageTextArea.setColor("#000");
          }
      });
      
      
      $.messageTextArea.addEventListener('blur', function(e) {
          hintText(hint);
      });
      
      function hintText(hint) {   
          if ($.messageTextArea.value.length == 0) {
          // set the color to lighter color
          $.messageTextArea.setColor("#ccc");
              $.messageTextArea.setValue(hint);
          }
      }
      

      — commented October 16th 2013 by Steven House
    • @Steven +1. Works like a charm. All I needed to change was the id. Thanks!

      — commented December 12th 2013 by Michael Stelly
  • Hi Chris,

    Thanks for the input. I actually already developed a similar solution to the Text Field, before I read the docs and found out about the hintText-property…

    I thought I'd do a little more digging this time around… ;)

    But thanks for confirming my suspicions!

    HTH

    /J

    — answered August 10th 2010 by Joacim Boive
    permalink
    0 Comments
  • A CommonJS module based on Dan's example:
    https://gist.github.com/2728758

    — answered May 19th 2012 by Dario Marcelino
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.