NGUI 实现UIInput按字节长度限制输入

UIInput按字节长度限制输入

(汉字算2个字节,数字字母算1个字节)

如果需要限制20个汉字,就需要在页面上配置限制40个字符了。

而数字、字母等都是1个算一个字节,而汉字等算2个字节。

 

  • 方法一:在UIInput中进行修改
    ①增加下面的方法
protected int GetStringByteLength(string str)
    {
        byte[] bytestr = System.Text.Encoding.Default.GetBytes(str);
        return bytestr.Length;
    }

 ②修改下面的方法

public string Validate(string val) 
    { 
        if (string.IsNullOrEmpty(val)) return ""; 

        StringBuilder sb = new StringBuilder(val.Length); 

        for (int i = 0; i  0 && this.GetStringByteLength(val) >= characterLimit) 
        { 
            do 
            { 
                val = val.Substring(0, val.Length - 1); 
            } while (this.GetStringByteLength(val) > characterLimit); 
        } 
        return val; 
    } 

③修改下面的方法

protected virtual void Insert(string text)
    {
        string left = GetLeftText();
        string right = GetRightText();
        int rl = right.Length;

        StringBuilder sb = new StringBuilder(left.Length + right.Length + text.Length);
        sb.Append(left);

        // Append the new text
        for (int i = 0, imax = text.Length; i < imax; ++i)
        {
            // Can't go past the character limit
            if (characterLimit > 0 && this.GetStringByteLength(sb.ToString()) + 

this.GetStringByteLength(right) >= characterLimit) break;

            // If we have an input validator, validate the input first
            char c = text[i];
            if (onValidate != null) c = onValidate(sb.ToString(), sb.Length, c);
            else if (validation != Validation.None) c = Validate(sb.ToString(), sb.Length, c);

            // Append the character if it hasn't been invalidated
            if (c != 0) sb.Append(c);
        }

        // Advance the selection
        mSelectionStart = sb.Length;
        mSelectionEnd = mSelectionStart;

        // Append the text that follows it, ensuring that it's also validated after the inserted value
        for (int i = 0, imax = right.Length; i < imax; ++i)
        {
            char c = right[i];
            if (onValidate != null) c = onValidate(sb.ToString(), sb.Length, c);
            else if (validation != Validation.None) c = Validate(sb.ToString(), sb.Length, c);
            if (c != 0) sb.Append(c);
        }

        mValue = sb.ToString();
        UpdateLabel();
        ExecuteOnChange();
    }

 

  • 方法二:在上层逻辑中实现(推荐此方法)

 

    //下面的事件监听放入合适的方法中 调用
    EventDelegate.Add(this.mInput.onChange, this.OnInputChange);

    private void OnInputChange()
    {
        string str = this.mInput.value;
        if (characterLimit > 0 && this.GetStringByteLength(str) > characterLimit)
        {
            do
            {
                str = str.Substring(0, str.Length - 1);
            } while (this.GetStringByteLength(str) > characterLimit);
            this.mInput.value = str;
        }
    }
    private int GetStringByteLength(string str)
    {
        byte[] bytestr = System.Text.Encoding.Default.GetBytes(str);
        return bytestr.Length;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值