Example #1
0
        private UIStringAttributes GetLineHeightAttributes()
        {
            var attributes = new UIStringAttributes();

            var paragraphStyle = new NSMutableParagraphStyle()
            {
                MinimumLineHeight = (nfloat)LineHeight,
                Alignment         = TextAlignment.ToNativeTextAlignment(),
                LineBreakMode     = GetLineBreakMode(),
            };

            if (LineStackingStrategy != LineStackingStrategy.MaxHeight)
            {
                paragraphStyle.MaximumLineHeight = (nfloat)LineHeight;
            }
            attributes.ParagraphStyle = paragraphStyle;

            // BaselineOffset calculation is only required when the text's font size does not take up the entire line height
            // Otherwise this calculation will result in invalid values causing the text to be pushed out of the UILabel's rect
            if (Font != null && Font.LineHeight < LineHeight)
            {
                // iOS puts text at the bottom of the line box, whereas Windows puts it at the top. Empirically this offset gives similar positioning to Windows.
                // (Note that Descender is typically a negative value.)
                var verticalOffset = ((float)LineHeight - (float)Font.LineHeight) * .56f + (float)Font.Descender;
                attributes.BaselineOffset = verticalOffset;
            }

            return(attributes);
        }
Example #2
0
        private void UpdateTextBoxView()
        {
            if (_contentElement != null)
            {
                if (_textBoxView is TextBoxView || _textBoxView is SecureTextBoxView)
                {
                    return;
                }

                if (_isPassword)
                {
                    _textBoxView = new SecureTextBoxView(this)
                    {
                        UsesSingleLineMode = true, Alignment = TextAlignment.ToNativeTextAlignment()
                    };
                    _revealView = new TextBoxView(this)
                    {
                        UsesSingleLineMode = true, Alignment = TextAlignment.ToNativeTextAlignment()
                    };
                    _isSecured = true;
                }
                else
                {
                    var textWrapping       = TextWrapping;
                    var usesSingleLineMode = !(AcceptsReturn || textWrapping != TextWrapping.NoWrap);
                    _textBoxView = new TextBoxView(this)
                    {
                        UsesSingleLineMode = usesSingleLineMode,
                        LineBreakMode      = textWrapping == TextWrapping.WrapWholeWords ? NSLineBreakMode.ByWordWrapping : NSLineBreakMode.CharWrapping,
                        Alignment          = TextAlignment.ToNativeTextAlignment()
                    };
                }

                _contentElement.Content = _textBoxView;
                _textBoxView.SetTextNative(Text);
                InitializeProperties();
            }
        }
Example #3
0
        private UIStringAttributes GetAttributes()
        {
            var attributes = new UIStringAttributes();

            var font = UIFontHelper.TryGetFont((float)FontSize, FontWeight, FontStyle, FontFamily);

            attributes.Font            = font;
            attributes.ForegroundColor = (Foreground as SolidColorBrush)?.ColorWithOpacity;

            if (TextDecorations != TextDecorations.None)
            {
                attributes.UnderlineStyle = (TextDecorations & TextDecorations.Underline) == TextDecorations.Underline
                                        ? NSUnderlineStyle.Single
                                        : NSUnderlineStyle.None;

                attributes.StrikethroughStyle = (TextDecorations & TextDecorations.Strikethrough) == TextDecorations.Strikethrough
                                        ? NSUnderlineStyle.Single
                                        : NSUnderlineStyle.None;
            }

            var paragraphStyle = new NSMutableParagraphStyle()
            {
                MinimumLineHeight = (nfloat)LineHeight,
                Alignment         = TextAlignment.ToNativeTextAlignment(),
                LineBreakMode     = GetLineBreakMode(),
            };

            // For unknown reasons, the LineBreakMode must be set to WordWrap
            // when applied to a NSTextStorage for text to wrap.
            if (UseLayoutManager)
            {
                paragraphStyle.LineBreakMode = UILineBreakMode.WordWrap;
            }

            if (LineStackingStrategy != LineStackingStrategy.MaxHeight)
            {
                paragraphStyle.MaximumLineHeight = (nfloat)LineHeight;
            }
            attributes.ParagraphStyle = paragraphStyle;

            if (LineHeight != 0 && font != null)
            {
                // iOS puts text at the bottom of the line box, whereas Windows puts it at the top.
                // Empirically this offset gives similar positioning to Windows.
                // Note: Descender is typically a negative value.
                var verticalOffset = LineHeight - font.LineHeight + font.Descender;

                // Because we're trying to move the text up (toward the top of the line box),
                // we only set BaselineOffset to a positive value.
                // A negative value indicates that the the text is already bottom-aligned.
                attributes.BaselineOffset = Math.Max(0, (float)verticalOffset);
            }

            if (CharacterSpacing != 0)
            {
                //CharacterSpacing is in 1/1000 of an em, iOS KerningAdjustment is in points. 1 em = 12 points
                attributes.KerningAdjustment = (CharacterSpacing / 1000f) * 12;
            }

            return(attributes);
        }
Example #4
0
        private UIStringAttributes GetAttributes(NSAttributedStringTarget target)
        {
            var attributes = new UIStringAttributes();

            // We only apply these values to NSTextStorage, as they're already applied to UILabel.
            // See: UpdateFont, UpdateTextColor, etc.
            if (target == NSAttributedStringTarget.NSTextStorage)
            {
                attributes.Font            = _currentFont;
                attributes.ForegroundColor = _currentColor;
            }

            if (target == NSAttributedStringTarget.NSTextStorage || LineHeight != 0)
            {
                var paragraphStyle = new NSMutableParagraphStyle()
                {
                    MinimumLineHeight = (nfloat)LineHeight,
                    Alignment         = TextAlignment.ToNativeTextAlignment(),
                    LineBreakMode     = GetLineBreakMode(),
                };

                // For unknown reasons, the LineBreakMode must be set to WordWrap
                // when applied to a NSTextStorage for text to wrap (but not when applied to a UILabel).
                if (target == NSAttributedStringTarget.NSTextStorage)
                {
                    paragraphStyle.LineBreakMode = UILineBreakMode.WordWrap;
                }

                if (LineStackingStrategy != LineStackingStrategy.MaxHeight)
                {
                    paragraphStyle.MaximumLineHeight = (nfloat)LineHeight;
                }
                attributes.ParagraphStyle = paragraphStyle;

                if (Font != null)
                {
                    // iOS puts text at the bottom of the line box, whereas Windows puts it at the top.
                    // Empirically this offset gives similar positioning to Windows.
                    // Note: Descender is typically a negative value.
                    var verticalOffset = LineHeight - Font.LineHeight + Font.Descender;

                    // For unknown reasons, the verticalOffset must be divided by 2
                    // when applied to a UILabel (but not when applied to a NSTextStorage).
                    if (target == NSAttributedStringTarget.UILabel)
                    {
                        verticalOffset /= 2;
                    }

                    // Because we're trying to move the text up (toward the top of the line box),
                    // we only set BaselineOffset to a positive value.
                    // A negative value indicates that the the text is already bottom-aligned.
                    attributes.BaselineOffset = Math.Max(0, (float)verticalOffset);
                }
            }

            if (CharacterSpacing != 0)
            {
                //CharacterSpacing is in 1/1000 of an em, iOS KerningAdjustment is in points. 1 em = 12 points
                attributes.KerningAdjustment = (CharacterSpacing / 1000f) * 12;
            }

            return(attributes);
        }
Example #5
0
 private void UpdateTextAlignment()
 {
     base.TextAlignment = TextAlignment.ToNativeTextAlignment();
 }
Example #6
0
        private NSStringAttributes GetAttributes()
        {
            var attributes = new NSStringAttributes();

            var font = NSFontHelper.TryGetFont((float)FontSize, FontWeight, FontStyle, FontFamily);

            attributes.Font = font;

            if (TextDecorations != TextDecorations.None)
            {
                attributes.UnderlineStyle = (int)((TextDecorations & TextDecorations.Underline) == TextDecorations.Underline
                                        ? NSUnderlineStyle.Single
                                        : NSUnderlineStyle.None);

                attributes.StrikethroughStyle = (int)((TextDecorations & TextDecorations.Strikethrough) == TextDecorations.Strikethrough
                                        ? NSUnderlineStyle.Single
                                        : NSUnderlineStyle.None);
            }

            var paragraphStyle = new NSMutableParagraphStyle()
            {
                MinimumLineHeight = (nfloat)LineHeight,
                Alignment         = TextAlignment.ToNativeTextAlignment(),
                LineBreakMode     = GetLineBreakMode(),
            };

            // For unknown reasons, the LineBreakMode must be set to WordWrap
            // when applied to a NSTextStorage for text to wrap.
            if (UseLayoutManager)
            {
                paragraphStyle.LineBreakMode = NSLineBreakMode.ByWordWrapping;
            }

            if (LineStackingStrategy != LineStackingStrategy.MaxHeight)
            {
                paragraphStyle.MaximumLineHeight = (nfloat)LineHeight;
            }
            attributes.ParagraphStyle = paragraphStyle;

            if (LineHeight != 0 && font != null)
            {
                // iOS puts text at the bottom of the line box, whereas Windows puts it at the top.
                // Empirically this offset gives similar positioning to Windows.
                // Note: Descender is typically a negative value.
                var verticalOffset = LineHeight - font.XHeight /* MACOS TODO XHeight ? */ + font.Descender;

                // Because we're trying to move the text up (toward the top of the line box),
                // we only set BaselineOffset to a positive value.
                // A negative value indicates that the the text is already bottom-aligned.
                attributes.BaselineOffset = Math.Max(0, (float)verticalOffset);
            }

            if (CharacterSpacing != 0)
            {
                //CharacterSpacing is in 1/1000 of an em, iOS KerningAdjustment is in points. 1 em = 12 points
                attributes.KerningAdjustment = (CharacterSpacing / 1000f) * 12;
            }

            // Foreground checks should be kept at the end since we **may** use the attributes to calculate text size
            // for gradient brushes.
            // TODO: Support other brushes (e.g. gradients):
            if (Brush.TryGetColorWithOpacity(Foreground, out var color))
            {
                attributes.ForegroundColor = color;
            }
            else
            {
                attributes.ForegroundColor = Colors.Transparent;
            }

            return(attributes);
        }