Beispiel #1
0
        public static void UpdateText(this EditText editText, InputView inputView)
        {
            // Is UpdateText being called only to transform the text
            // that's already set on the platform element?
            // If so then we want to retain the cursor position
            bool transformingPlatformText =
                (editText.Text == inputView.Text);

            var value = TextTransformUtilites.GetTransformedText(inputView.Text, inputView.TextTransform);

            if (!transformingPlatformText)
            {
                editText.Text = value;
            }
            else
            {
                // Setting the text causes the cursor to reset to position zero
                // so if we are transforming the text and then setting it to a
                // new value then we need to retain the cursor position
                if (value == editText.Text)
                {
                    return;
                }

                int selectionStart = editText.SelectionStart;
                editText.Text = value;
                editText.SetSelection(selectionStart);
            }
        }
Beispiel #2
0
        public static void UpdateText(this EditText editText, InputView inputView)
        {
            bool isPasswordEnabled =
                (editText.InputType & InputTypes.TextVariationPassword) == InputTypes.TextVariationPassword ||
                (editText.InputType & InputTypes.NumberVariationPassword) == InputTypes.NumberVariationPassword;

            // Setting the text causes the cursor to be reset to position zero.
            // So, let's retain the current cursor position and calculate a new cursor
            // position if the text was modified by a Converter.
            var oldText = editText.Text ?? string.Empty;
            var newText = TextTransformUtilites.GetTransformedText(
                inputView?.Text,
                isPasswordEnabled ? TextTransform.None : inputView.TextTransform
                );

            // Re-calculate the cursor offset position if the text was modified by a Converter.
            // but if the text is being set by code, let's just move the cursor to the end.
            var cursorOffset   = newText.Length - oldText.Length;
            int cursorPosition = editText.IsFocused ? editText.GetCursorPosition(cursorOffset) : newText.Length;

            if (oldText != newText)
            {
                editText.Text = newText;
            }

            editText.SetSelection(cursorPosition, cursorPosition);
        }
        public static void MapText(ButtonHandler handler, Button button)
        {
            var text = TextTransformUtilites.GetTransformedText(button.Text, button.TextTransform);

            handler.NativeView?.UpdateText(text);
            button.Handler?.UpdateValue(nameof(Button.ContentLayout));
        }
Beispiel #4
0
        public static void UpdateText(this TextBox platformControl, InputView inputView)
        {
            var hasFocus      = platformControl.FocusState != UI.Xaml.FocusState.Unfocused;
            var passwordBox   = platformControl as MauiPasswordTextBox;
            var isPassword    = passwordBox?.IsPassword ?? false;
            var textTransform = inputView?.TextTransform ?? TextTransform.None;

            // Setting the text causes the cursor to be reset to position zero.
            // So, let's retain the current cursor position and calculate a new cursor
            // position if the text was modified by a Converter.
            var oldText = platformControl.Text ?? string.Empty;
            var newText = TextTransformUtilites.GetTransformedText(
                inputView?.Text,
                isPassword ? TextTransform.None : textTransform
                );

            // Re-calculate the cursor offset position if the text was modified by a Converter.
            // but if the text is being set by code, let's just move the cursor to the end.
            var cursorOffset   = newText.Length - oldText.Length;
            int cursorPosition = hasFocus ? platformControl.GetCursorPosition(cursorOffset) : newText.Length;

            if (oldText != newText && passwordBox is not null)
            {
                passwordBox.Password = newText;
            }
            else if (oldText != newText)
            {
                platformControl.Text = newText;
            }

            platformControl.Select(cursorPosition, 0);
        }
Beispiel #5
0
        public static Tuple <Run, Color, Color> ToRunAndColorsTuple(this Span span, IFontManager fontManager, Font?defaultFont = null, Color?defaultColor = null, TextTransform defaultTextTransform = TextTransform.Default)
        {
            var transform = span.TextTransform != TextTransform.Default ? span.TextTransform : defaultTextTransform;

            var text = TextTransformUtilites.GetTransformedText(span.Text, transform);

            var run = new Run {
                Text = text ?? string.Empty
            };

            var font = span.ToFont();

            if (font.IsDefault && defaultFont.HasValue)
            {
                font = defaultFont.Value;
            }

            if (!font.IsDefault)
            {
                run.ApplyFont(font, fontManager);
            }

            if (span.IsSet(Span.TextDecorationsProperty))
            {
                run.TextDecorations = (global::Windows.UI.Text.TextDecorations)span.TextDecorations;
            }

            run.CharacterSpacing = span.CharacterSpacing.ToEm();

            return(Tuple.Create(run, span.TextColor, span.BackgroundColor));
        }
Beispiel #6
0
        public static void UpdateText(this UIButton nativeButton, Button button)
        {
            var text = TextTransformUtilites.GetTransformedText(button.Text, button.TextTransform);

            nativeButton.SetTitle(text, UIControlState.Normal);

            // Content layout depends on whether or not the text is empty; changing the text means
            // we may need to update the content layout
            nativeButton.UpdateContentLayout(button);
        }
Beispiel #7
0
        public static void UpdateText(this MaterialButton platformButton, Button button)
        {
            var text = TextTransformUtilites.GetTransformedText(button.Text, button.TextTransform);

            platformButton.Text = text;

            // Content layout depends on whether or not the text is empty; changing the text means
            // we may need to update the content layout
            platformButton.UpdateContentLayout(button);
        }
Beispiel #8
0
 public static void UpdateText(this TextBox platformControl, InputView inputView)
 {
     if (platformControl is MauiPasswordTextBox passwordBox)
     {
         passwordBox.Password = TextTransformUtilites.GetTransformedText(inputView.Text, passwordBox.IsPassword ? TextTransform.None : inputView.TextTransform);
     }
     else
     {
         platformControl.Text = TextTransformUtilites.GetTransformedText(inputView.Text, inputView.TextTransform);
     }
 }
Beispiel #9
0
        public static void UpdateText(this TextView textView, Label label)
        {
            switch (label.TextType)
            {
            case TextType.Text:
                textView.Text = TextTransformUtilites.GetTransformedText(label.Text, label.TextTransform);
                break;

            case TextType.Html:
                textView.UpdateTextHtml(label);
                break;
            }
        }
Beispiel #10
0
        public static void UpdateText(this TextBlock nativeControl, Label label)
        {
            switch (label.TextType)
            {
            case TextType.Html:
                nativeControl.UpdateTextHtml(label);
                break;

            default:
                nativeControl.Text = TextTransformUtilites.GetTransformedText(label.Text, label.TextTransform);
                break;
            }
        }
Beispiel #11
0
        public static void UpdateText(this TextView textView, Label label)
        {
            switch (label.TextType)
            {
            case TextType.Text:
                if (label.FormattedText != null)
                {
                    textView.TextFormatted = label.ToSpannableString();
                }
                else
                {
                    textView.Text = TextTransformUtilites.GetTransformedText(label.Text, label.TextTransform);
                }
                break;

            case TextType.Html:
                textView.UpdateTextHtml(label);
                break;
            }
        }
Beispiel #12
0
        public static void UpdateText(this TextBlock platformControl, Label label)
        {
            switch (label.TextType)
            {
            case TextType.Html:
                platformControl.UpdateTextHtml(label);
                break;

            default:
                if (label.FormattedText != null)
                {
                    platformControl.UpdateInlines(label);
                }
                else
                {
                    platformControl.Text = TextTransformUtilites.GetTransformedText(label.Text, label.TextTransform);
                }
                break;
            }
        }
Beispiel #13
0
        public static void UpdateText(this UILabel platformLabel, Label label)
        {
            switch (label.TextType)
            {
            case TextType.Html:
                platformLabel.UpdateTextHtml(label);
                break;

            default:
                if (label.FormattedText != null)
                {
                    platformLabel.AttributedText = label.ToNSAttributedString();
                }
                else
                {
                    platformLabel.Text = TextTransformUtilites.GetTransformedText(label.Text, label.TextTransform);
                }
                break;
            }
        }
        public static Run ToRun(this Span span, IFontManager fontManager, Font?defaultFont = null, Color?defaultColor = null, TextTransform defaultTextTransform = TextTransform.Default)
        {
            var transform = span.TextTransform != TextTransform.Default ? span.TextTransform : defaultTextTransform;

            var text = TextTransformUtilites.GetTransformedText(span.Text, transform);

            var run = new Run {
                Text = text ?? string.Empty
            };

            var fgcolor = span.TextColor ?? defaultColor;

            if (fgcolor is not null)
            {
                run.Foreground = fgcolor.ToPlatform();
            }

            // NOTE: Background is not supported in Run

            var font = span.ToFont();

            if (font.IsDefault && defaultFont.HasValue)
            {
                font = defaultFont.Value;
            }

            if (!font.IsDefault)
            {
                run.ApplyFont(font, fontManager);
            }

            if (span.IsSet(Span.TextDecorationsProperty))
            {
                run.TextDecorations = (global::Windows.UI.Text.TextDecorations)span.TextDecorations;
            }

            run.CharacterSpacing = span.CharacterSpacing.ToEm();

            return(run);
        }
Beispiel #15
0
        public static void UpdateText(this UITextField textField, InputView inputView)
        {
            // Setting the text causes the cursor to be reset to the end of the UITextView.
            // So, let's set back the cursor to the last known position and calculate a new
            // position if needed when the text was modified by a Converter.
            var oldText = textField.Text ?? string.Empty;
            var newText = TextTransformUtilites.GetTransformedText(
                inputView?.Text,
                textField.SecureTextEntry ? TextTransform.Default : inputView.TextTransform
                );

            // Re-calculate the cursor offset position if the text was modified by a Converter.
            // but if the text is being set by code, let's just move the cursor to the end.
            var cursorOffset   = newText.Length - oldText.Length;
            var cursorPosition = textField.IsEditing ? textField.GetCursorPosition(cursorOffset) : newText.Length;

            if (oldText != newText)
            {
                textField.Text = newText;
            }

            textField.SetTextRange(cursorPosition, 0);
        }
Beispiel #16
0
 public static void UpdateText(this TEntry entry, InputView inputView)
 {
     entry.Text = TextTransformUtilites.GetTransformedText(entry.Text, inputView.TextTransform);
 }
Beispiel #17
0
 public static void UpdateText(this AutoSuggestBox platformControl, InputView inputView)
 {
     platformControl.Text = TextTransformUtilites.GetTransformedText(inputView.Text, inputView.TextTransform);
 }
Beispiel #18
0
 public static void UpdateText(this SearchView searchView, InputView inputView)
 {
     searchView.SetQuery(TextTransformUtilites.GetTransformedText(inputView.Text, inputView.TextTransform), false);
 }
        internal static SpannableString ToSpannableStringNewWay(
            this FormattedString formattedString,
            IFontManager fontManager,
            Context?context = null,
            double defaultCharacterSpacing           = 0d,
            TextAlignment defaultHorizontalAlignment = TextAlignment.Start,
            Font?defaultFont                       = null,
            Graphics.Color?defaultColor            = null,
            TextTransform defaultTextTransform     = TextTransform.Default,
            TextDecorations defaultTextDecorations = TextDecorations.None)
        {
            if (formattedString == null)
            {
                return(new SpannableString(string.Empty));
            }

            var defaultFontSize = defaultFont?.Size ?? fontManager.DefaultFontSize;

            var builder = new StringBuilder();

            for (int i = 0; i < formattedString.Spans.Count; i++)
            {
                Span span = formattedString.Spans[i];

                var transform = span.TextTransform != TextTransform.Default ? span.TextTransform : defaultTextTransform;

                var text = TextTransformUtilites.GetTransformedText(span.Text, transform);
                if (text == null)
                {
                    continue;
                }

                builder.Append(text);
            }

            var spannable = new SpannableString(builder.ToString());

            var c = 0;

            for (int i = 0; i < formattedString.Spans.Count; i++)
            {
                Span span = formattedString.Spans[i];
                var  text = span.Text;
                if (text == null)
                {
                    continue;
                }

                int start = c;
                int end   = start + text.Length;
                c = end;

                // TextColor
                var textColor = span.TextColor ?? defaultColor;
                if (textColor is not null)
                {
                    spannable.SetSpan(new ForegroundColorSpan(textColor.ToPlatform()), start, end, SpanTypes.InclusiveExclusive);
                }

                // BackgroundColor
                if (span.BackgroundColor is not null)
                {
                    spannable.SetSpan(new BackgroundColorSpan(span.BackgroundColor.ToPlatform()), start, end, SpanTypes.InclusiveExclusive);
                }

                // LineHeight
                if (span.LineHeight >= 0)
                {
                    spannable.SetSpan(new LineHeightSpan(span.LineHeight), start, end, SpanTypes.InclusiveExclusive);
                }

                // CharacterSpacing
                var characterSpacing = span.CharacterSpacing >= 0
                                        ? span.CharacterSpacing
                                        : defaultCharacterSpacing;
                if (characterSpacing >= 0)
                {
                    spannable.SetSpan(new LetterSpacingSpan(characterSpacing.ToEm()), start, end, SpanTypes.InclusiveInclusive);
                }

                // Font
                var font = span.ToFont(defaultFontSize);
                if (font.IsDefault && defaultFont.HasValue)
                {
                    font = defaultFont.Value;
                }
                if (!font.IsDefault)
                {
                    spannable.SetSpan(new FontSpan(font, fontManager, context), start, end, SpanTypes.InclusiveInclusive);
                }

                // TextDecorations
                var textDecorations = span.IsSet(Span.TextDecorationsProperty)
                                        ? span.TextDecorations
                                        : defaultTextDecorations;
                if (textDecorations.HasFlag(TextDecorations.Strikethrough))
                {
                    spannable.SetSpan(new StrikethroughSpan(), start, end, SpanTypes.InclusiveInclusive);
                }
                if (textDecorations.HasFlag(TextDecorations.Underline))
                {
                    spannable.SetSpan(new UnderlineSpan(), start, end, SpanTypes.InclusiveInclusive);
                }
            }

            return(spannable);
        }
        public static NSAttributedString ToNSAttributedString(this Span span, IFontManager fontManager, double defaultLineHeight = 0d, TextAlignment defaultHorizontalAlignment = TextAlignment.Start, Font?defaultFont = null, Color?defaultColor = null, TextTransform defaultTextTransform = TextTransform.Default)
        {
            var transform = span.TextTransform != TextTransform.Default ? span.TextTransform : defaultTextTransform;

            var text = TextTransformUtilites.GetTransformedText(span.Text, transform);

            if (text is null)
            {
                return(new NSAttributedString(string.Empty));
            }

            var style      = new NSMutableParagraphStyle();
            var lineHeight = span.LineHeight >= 0
                                ? span.LineHeight
                                : defaultLineHeight;

            if (lineHeight >= 0)
            {
                style.LineHeightMultiple = new nfloat(lineHeight);
            }

            style.Alignment = defaultHorizontalAlignment switch
            {
                TextAlignment.Start => UITextAlignment.Left,
                TextAlignment.Center => UITextAlignment.Center,
                TextAlignment.End => UITextAlignment.Right,
                _ => UITextAlignment.Left
            };

            var font = span.ToFont();

            if (font.IsDefault && defaultFont.HasValue)
            {
                font = defaultFont.Value;
            }

            var hasUnderline     = false;
            var hasStrikethrough = false;

            if (span.IsSet(Span.TextDecorationsProperty))
            {
                var textDecorations = span.TextDecorations;
                hasUnderline     = (textDecorations & TextDecorations.Underline) != 0;
                hasStrikethrough = (textDecorations & TextDecorations.Strikethrough) != 0;
            }

            var platformFont = font.IsDefault ? null : font.ToUIFont(fontManager);

#if !MACOS
            var attrString = new NSAttributedString(
                text,
                platformFont,
                (span.TextColor ?? defaultColor)?.ToPlatform(),
                span.BackgroundColor?.ToPlatform(),
                underlineStyle: hasUnderline ? NSUnderlineStyle.Single : NSUnderlineStyle.None,
                strikethroughStyle: hasStrikethrough ? NSUnderlineStyle.Single : NSUnderlineStyle.None,
                paragraphStyle: style,
                kerning: (float)span.CharacterSpacing);
#else
            var attrString = new NSAttributedString(
                text,
                platformFont,
                (span.TextColor ?? defaultColor)?.ToPlatform(),
                span.BackgroundColor?.ToPlatform(),
                underlineStyle: hasUnderline ? NSUnderlineStyle.Single : NSUnderlineStyle.None,
                strikethroughStyle: hasStrikethrough ? NSUnderlineStyle.Single : NSUnderlineStyle.None,
                paragraphStyle: style,
                kerningAdjustment: (float)span.CharacterSpacing);
#endif

            return(attrString);
        }
    }
Beispiel #21
0
 public static void UpdateText(this UISearchBar uiSearchBar, SearchBar searchBar)
 {
     uiSearchBar.Text = TextTransformUtilites.GetTransformedText(searchBar.Text, searchBar.TextTransform);
 }
Beispiel #22
0
 public virtual string UpdateFormsText(string source, TextTransform textTransform)
 => TextTransformUtilites.GetTransformedText(source, textTransform);
Beispiel #23
0
 public static void UpdateText(this UITextField textField, InputView inputView)
 {
     textField.Text = TextTransformUtilites.GetTransformedText(inputView.Text, inputView.TextTransform);
 }
Beispiel #24
0
        public static SpannableString ToSpannableString(this FormattedString formattedString, IFontManager fontManager, TextPaint?textPaint = null, Context?context = null, double defaultLineHeight = 0d, TextAlignment defaultHorizontalAlignment = TextAlignment.Start, Font?defaultFont = null, Graphics.Color?defaultColor = null, TextTransform defaultTextTransform = TextTransform.Default)
        {
            if (formattedString == null)
            {
                return(new SpannableString(string.Empty));
            }

            var builder = new StringBuilder();

            for (int i = 0; i < formattedString.Spans.Count; i++)
            {
                Span span = formattedString.Spans[i];

                var transform = span.TextTransform != TextTransform.Default ? span.TextTransform : defaultTextTransform;

                var text = TextTransformUtilites.GetTransformedText(span.Text, transform);
                if (text == null)
                {
                    continue;
                }

                builder.Append(text);
            }

            var spannable = new SpannableString(builder.ToString());

            var c = 0;

            for (int i = 0; i < formattedString.Spans.Count; i++)
            {
                Span span = formattedString.Spans[i];
                var  text = span.Text;
                if (text == null)
                {
                    continue;
                }

                int start = c;
                int end   = start + text.Length;
                c = end;

                var fgcolor = span.TextColor ?? defaultColor;
                if (fgcolor != null)
                {
                    spannable.SetSpan(new ForegroundColorSpan(fgcolor.ToPlatform()), start, end, SpanTypes.InclusiveExclusive);
                }

                if (span.BackgroundColor != null)
                {
                    spannable.SetSpan(new BackgroundColorSpan(span.BackgroundColor.ToPlatform()), start, end, SpanTypes.InclusiveExclusive);
                }

                var lineHeight = span.LineHeight >= 0
                                        ? span.LineHeight
                                        : defaultLineHeight;

                if (lineHeight >= 0)
                {
                    spannable.SetSpan(new LineHeightSpan(textPaint, lineHeight), start, end, SpanTypes.InclusiveExclusive);
                }

                var font = span.ToFont();
                if (font.IsDefault && defaultFont.HasValue)
                {
                    font = defaultFont.Value;
                }

                if (!font.IsDefault)
                {
                    spannable.SetSpan(
                        new FontSpan(font, context, span.CharacterSpacing.ToEm(), defaultHorizontalAlignment, fontManager),
                        start,
                        end,
                        SpanTypes.InclusiveInclusive);
                }

                if (span.IsSet(Span.TextDecorationsProperty))
                {
                    spannable.SetSpan(new TextDecorationSpan(span), start, end, SpanTypes.InclusiveInclusive);
                }
            }
            return(spannable);
        }