Ejemplo n.º 1
0
        private static void recursivePrepareSpannableIndexes(Context context, string fullText, SpannableStringBuilder text, IList<IconFontDescriptorWrapper> iconFontDescriptors, int start)
        {
            // Try to find a {...} in the string and extract expression from it
            string stringText = text.ToString();
            int startIndex = stringText.IndexOf("{", start, StringComparison.Ordinal);
            if (startIndex == -1)
            {
                return;
            }
            int endIndex = stringText.IndexOf("}", startIndex, StringComparison.Ordinal) + 1;
            string expression = stringText.Substring(startIndex + 1, endIndex - 1 - (startIndex + 1));

            // Split the expression and retrieve the icon key
            string[] strokes = expression.Split(" ", true);
            string key = strokes[0];

            // Loop through the descriptors to find a key match
            IconFontDescriptorWrapper iconFontDescriptor = null;
            Icon icon = null;
            for (int i = 0; i < iconFontDescriptors.Count; i++)
            {
                iconFontDescriptor = iconFontDescriptors[i];
                icon = iconFontDescriptor.getIcon(key);
                if (icon != null)
                {
                    break;
                }
            }

            // If no match, ignore and continue
            if (icon == null)
            {
                recursivePrepareSpannableIndexes(context, fullText, text, iconFontDescriptors, endIndex);
                return;
            }

            // See if any more stroke within {} should be applied
            float iconSizePx = -1;
            int iconColor = int.MaxValue;
            float iconSizeRatio = -1;
            bool spin = false;
            for (int i = 1; i < strokes.Length; i++)
            {
                string stroke = strokes[i];

                // Look for "spin"
                if (stroke.Equals("spin", StringComparison.CurrentCultureIgnoreCase))
                {
                    spin = true;
                }

                // Look for an icon size
                else if (stroke.matches("([0-9]*(\\.[0-9]*)?)dp"))
                {
                    iconSizePx = dpToPx(context, Convert.ToSingle(stroke.Substring(0, stroke.Length - 2)));
                }
                else if (stroke.matches("([0-9]*(\\.[0-9]*)?)sp"))
                {
                    iconSizePx = spToPx(context, Convert.ToSingle(stroke.Substring(0, stroke.Length - 2)));
                }
                else if (stroke.matches("([0-9]*)px"))
                {
                    iconSizePx = Convert.ToInt32(stroke.Substring(0, stroke.Length - 2));
                }
                else if (stroke.matches("@dimen/(.*)"))
                {
                    iconSizePx = getPxFromDimen(context, stroke.Substring(7));
                    if (iconSizePx < 0)
                    {
                        throw new System.ArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
                    }
                }
                else if (stroke.matches("([0-9]*(\\.[0-9]*)?)%"))
                {
                    iconSizeRatio = Convert.ToSingle(stroke.Substring(0, stroke.Length - 1)) / 100f;
                }

                // Look for an icon color
                else if (stroke.matches("#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})"))
                {
                    iconColor = Color.parseColor(stroke);
                }
                else if (stroke.matches("@color/(.*)"))
                {
                    iconColor = getColorFromResource(context, stroke.Substring(7));
                    if (iconColor == int.MaxValue)
                    {
                        throw new System.ArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
                    }
                }
                else
                {
                    throw new System.ArgumentException("Unknown expression " + stroke + " in \"" + fullText + "\"");
                }

            }

            // Replace the character and apply the typeface
            text = text.replace(startIndex, endIndex, "" + icon.character());
            text.setSpan(new CustomTypefaceSpan(icon, iconFontDescriptor.getTypeface(context), iconSizePx, iconSizeRatio, iconColor, spin), startIndex, startIndex + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            recursivePrepareSpannableIndexes(context, fullText, text, iconFontDescriptors, startIndex);
        }