Ejemplo n.º 1
0
        /// <summary>
        /// 解析图像值。
        /// </summary>
        /// <returns>解析后的字符串。</returns>
        public string ParseImage(string text)
        {
            float fs    = GetComponent <Text>().fontSize;
            int   index = 0;

            m_ImageInfos.Clear();
            CacheSB.Length = 0;
            foreach (Match match in ImageRegex.Matches(text))
            {
                string[] param     = match.Groups[1].Value.Trim().Split(',');
                string   innerchar = (param.Length >= 3 && param[2].Length > 0) ? param[2].Substring(0, 1) : "哈"; //默认用哈,最接近正方形
                string   innertext = innerchar;                                                                   //图像位置用"M"字符占位
                int      prelen    = 0;
                if (param.Length >= 2)
                {
                    float scale;
                    if (float.TryParse(param[1], out scale))
                    {
                        if (Math.Abs(scale - 1) >= 0.0001f)
                        {
                            int    imgsize = (int)(fs * scale);         //必须取整
                            string pre     = string.Format("<size={0}>", imgsize);
                            innertext = string.Format("{0}{1}</size>", pre, innerchar);
                            prelen    = pre.Length;
                        }
                    }
                }
                float imgscale = 1;
                if (param.Length >= 4)
                {
                    float scale;
                    if (float.TryParse(param[3], out scale))
                    {
                        imgscale = scale;
                    }
                }

                ImageInfo info = new ImageInfo();
                CacheSB.Append(text.Substring(index, match.Index - index));       //匹配目标前的那一部分
                info.StartIndex = CacheSB.Length + prelen;
                info.EndIndex   = info.StartIndex + 1;
                info.Name       = param[0];
                info.Scale      = imgscale;
                m_ImageInfos.Add(info);
                CacheSB.Append(innertext);

                int cut = match.Length - innertext.Length;          //匹配调整后缩短的长度
                index = match.Index + match.Length;
                AdujstIndex(m_ColorInfos, info.StartIndex, info.StartIndex + match.Length, cut, 0);
                AdujstIndex(m_HrefInfos, info.StartIndex, info.StartIndex + match.Length, cut, 0);
            }
            CacheSB.Append(text.Substring(index, text.Length - index));
            return(CacheSB.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 解析超链接值。
        /// </summary>
        /// <returns>解析后的字符串。</returns>
        public string ParseHref(string text)
        {
            int index = 0;

            m_HrefInfos.Clear();
            CacheSB.Length = 0;
            foreach (Match match in HrefRegex.Matches(text))
            {
                string   param     = match.Groups[1].Value.Trim();
                string   innertext = match.Groups[2].Value;
                int      cut       = match.Length - innertext.Length;       //匹配调整后缩短的长度
                HrefInfo info      = new HrefInfo();
                CacheSB.Append(text.Substring(index, match.Index - index)); //匹配目标前的那一部分
                info.StartIndex = CacheSB.Length;
                info.EndIndex   = info.StartIndex + innertext.Length;
                info.Param      = param;
                m_HrefInfos.Add(info);
                CacheSB.Append(innertext);
                index = match.Index + match.Length;
                AdujstIndex(m_ColorInfos, info.StartIndex, info.StartIndex + match.Length, cut, 7);       //7为"</href>"标签的长度
            }
            CacheSB.Append(text.Substring(index, text.Length - index));
            return(CacheSB.ToString());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 解析颜色值。
        /// </summary>
        /// <returns>解析后的字符串。</returns>
        public string ParseColor(string text)
        {
            Color def   = GetComponent <Text>().color;
            int   index = 0;

            m_ColorInfos.Clear();
            CacheSB.Length = 0;
            CacheSB.Append(UnderLineChar);      //首个解析函数要加下划线占位符
            foreach (Match match in ColorRegex.Matches(text))
            {
                string    colorstr  = match.Groups[1].Value.Trim();
                string    innertext = match.Groups[2].Value;
                ColorInfo info      = new ColorInfo();
                CacheSB.Append(text.Substring(index, match.Index - index));       //匹配目标前的那一部分
                info.StartIndex = CacheSB.Length;
                info.EndIndex   = info.StartIndex + innertext.Length;
                info.TextColor  = UiUtil.ToColor(colorstr, def);
                m_ColorInfos.Add(info);
                CacheSB.Append(innertext);
                index = match.Index + match.Length;
            }
            CacheSB.Append(text.Substring(index, text.Length - index));
            return(CacheSB.ToString());
        }