Author Topic: UILabel Max Lines Not working for UIInput  (Read 2160 times)

callski

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 9
    • View Profile
UILabel Max Lines Not working for UIInput
« on: March 23, 2015, 10:44:13 AM »
I have a UILabel and a UIINput script attached to the UILabel object. The "Max Lines" on UILabel is set to 2, but when I go to input text on iOS I can add as many lines as I want. How do I limit input to only two lines?

callski

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UILabel Max Lines Not working for UIInput
« Reply #1 on: March 23, 2015, 11:00:54 AM »
Another issue I am noticing is the window's standalone version will only show a single line unless "max lines" is set to 0. Max lines can be set to 5, the UILabel text field is showing two lines, but the application only renders the first line. It's really weird....

callski

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UILabel Max Lines Not working for UIInput
« Reply #2 on: March 23, 2015, 11:21:02 AM »
So I'm just going to assume this feature with NGUI is broken. I created a workaround. Set the Max Lines to 0, attach the script below to the object, and set the UIInput's OnChange event to trigger the OnTextChanged method. This does the trick.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class LimitLines : MonoBehaviour
  5. {
  6.     public int TotalLines = 2;
  7.     private UILabel _label;
  8.     private UIInput _input;
  9.  
  10.     void Start()
  11.     {
  12.         _label = this.GetComponent<UILabel>();
  13.         _input = this.GetComponent<UIInput>();
  14.     }
  15.  
  16.     public void OnTextChanged()
  17.     {
  18.         string[] lines = _input.value.Split('\n');
  19.         string output = "";
  20.  
  21.         for (var i = 0; i < lines.Length && i < TotalLines; i++)
  22.         {
  23.             output += i > 0 ? '\n' + lines[i] : lines[i];
  24.         }
  25.  
  26.         _label.text = output;
  27.         _input.value = output;
  28.     }
  29. }
  30.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UILabel Max Lines Not working for UIInput
« Reply #3 on: March 24, 2015, 10:28:14 AM »
The only max lines setting that will work with the input field other than '0' is '1'. You can see this on line 1233 of UIInput.cs. The label overflow needs to be set to "clamp" as well.