Exemple #1
0
        public override Bound Apply(StyleSheet.StyleSheet stylesheet, Bound styleBound, Bound maxBound)
        {
            base.Apply(stylesheet, styleBound, maxBound);

            StyleHelper style = stylesheet.GetHelper <StyleHelper> ();

            _view.Text = _text;

            // background color, borders
            style.SetBackgroundSettings(this);

            //  text-format
            _textFormat = style.TextFormat(this);

            // font
            UIFont f = style.Font(this, styleBound.Height);

            if (f != null)
            {
                _view.Font = f;
            }

            switch (_textFormat)
            {
            case TextFormat.Format.Text:
                _view.Text = _text;

                // text color
                _view.TextColor = _textColor = style.ColorOrClear <BitMobile.Controls.StyleSheet.Color> (this);

                // selected-color
                _selectedColor = style.Color <SelectedColor> (this);

                break;

            case TextFormat.Format.Html:
                string span = string.Format("<span style=\"font-family: {0}; font-size: {1:F0}; color: {2}\">{3}</span>", _view.Font.FamilyName, _view.Font.PointSize, "{0}", "{1}");

                // text color
                _textHtmlSpan = string.Format(span, style.ColorString <BitMobile.Controls.StyleSheet.Color> (this), "{0}");

                // selected-color
                string selectedColor = style.ColorString <SelectedColor> (this);
                if (selectedColor != null)
                {
                    _selectedHtmlSpan = string.Format(span, selectedColor, "{0}");
                }

                SetSpannedText(_textHtmlSpan);
                break;

            default:
                throw new NotImplementedException("Text format not found: " + _textFormat);
            }

            // word wrap
            bool nowrap = style.WhiteSpaceKind(this) == WhiteSpace.Kind.Nowrap;

            if (!nowrap)
            {
                _view.TextContainer.LineBreakMode        = UILineBreakMode.WordWrap;
                _view.TextContainer.MaximumNumberOfLines = 0;
            }
            else
            {
                _view.TextContainer.LineBreakMode        = UILineBreakMode.TailTruncation;
                _view.TextContainer.MaximumNumberOfLines = 1;
            }

            // text align
            switch (style.TextAlign(this))
            {
            case TextAlign.Align.Left:
                _view.TextAlignment = UITextAlignment.Left;
                break;

            case TextAlign.Align.Center:
                _view.TextAlignment = UITextAlignment.Center;
                break;

            case TextAlign.Align.Right:
                if (nowrap)
                {
                    _view.TextContainer.LineBreakMode = UILineBreakMode.HeadTruncation;
                }
                _view.TextAlignment = UITextAlignment.Right;
                break;
            }

            // text padding
            float pl = style.Padding <PaddingLeft> (this, styleBound.Width);
            float pt = style.Padding <PaddingTop> (this, styleBound.Height);
            float pr = style.Padding <PaddingRight> (this, styleBound.Width);
            float pb = style.Padding <PaddingBottom> (this, styleBound.Height);

            _view.TextContainerInset = new UIEdgeInsets(pt, pl, pb, pr);

            Bound resultBound = styleBound;

            // size to content
            bool sizeByWidth  = style.SizeToContentWidth(this);
            bool sizeByHeight = style.SizeToContentHeight(this);

            if (sizeByWidth || sizeByHeight)
            {
                float measureWidth  = sizeByWidth ? maxBound.Width : styleBound.Width;
                float measureHeight = sizeByHeight ? maxBound.Height : styleBound.Height;

                SizeF size = _view.SizeThatFits(new SizeF(measureWidth, measureHeight));

                float w = sizeByWidth ? size.Width + pl + pr : styleBound.Width;
                float h = sizeByHeight ? size.Height + pt + pb : styleBound.Height;

                resultBound = new Bound(w, h);
            }

            return(resultBound);
        }
Exemple #2
0
        public override Bound Apply(StyleSheet.StyleSheet stylesheet, Bound styleBound, Bound maxBound)
        {
            base.Apply(stylesheet, styleBound, maxBound);

            var style = stylesheet.GetHelper <StyleHelper>();

            // text-format
            _textFormat = style.TextFormat(this);
            SetText();

            // background color, borders
            var background = style.Background(this, _applicationContext, true);

            _view.SetBackgroundDrawable(background);

            // font
            style.SetFontSettings(this, _view, styleBound.Height);

            // text color
            _textColor = style.ColorOrTransparent <Color>(this);
            _view.SetTextColor(_textColor);

            //selected color
            _selectedColor = style.Color <SelectedColor>(this);

            // word wrap
            bool nowrap = style.WhiteSpaceKind(this) == WhiteSpace.Kind.Nowrap;

            if (nowrap)
            {
                _view.SetSingleLine();
            }

            // text align
            switch (style.TextAlign(this))
            {
            case TextAlign.Align.Left:
                _view.Gravity = GravityFlags.Left | GravityFlags.Top;
                if (nowrap)
                {
                    _view.Ellipsize = Android.Text.TextUtils.TruncateAt.End;
                }
                break;

            case TextAlign.Align.Center:
                _view.Gravity = GravityFlags.Center | GravityFlags.Top;
                if (nowrap)
                {
                    _view.Ellipsize = Android.Text.TextUtils.TruncateAt.End;
                }
                break;

            case TextAlign.Align.Right:
                _view.Gravity = GravityFlags.Right | GravityFlags.Top;
                if (nowrap)
                {
                    _view.Ellipsize = Android.Text.TextUtils.TruncateAt.Start;
                }
                break;
            }

            // text padding
            int pl = style.Padding <PaddingLeft>(this, styleBound.Width).Round();
            int pt = style.Padding <PaddingTop>(this, styleBound.Height).Round();
            int pr = style.Padding <PaddingRight>(this, styleBound.Width).Round();
            int pb = style.Padding <PaddingBottom>(this, styleBound.Height).Round();

            _view.SetPadding(pl, pt, pr, pb);

            bool sizeByWith   = style.SizeToContentWidth(this);
            bool sizeByHeight = style.SizeToContentHeight(this);

            if (sizeByWith || sizeByHeight)
            {
                float measureWidth  = sizeByWith ? maxBound.Width : styleBound.Width;
                float measureHeight = sizeByHeight ? maxBound.Height : styleBound.Height;

                int wspec = View.MeasureSpec.MakeMeasureSpec(Math.Round(measureWidth), MeasureSpecMode.AtMost);
                int hspec = View.MeasureSpec.MakeMeasureSpec(Math.Round(measureHeight), MeasureSpecMode.AtMost);

                _view.Measure(wspec, hspec);
                float w = sizeByWith ? _view.MeasuredWidth : styleBound.Width;
                float h = sizeByHeight ? _view.MeasuredHeight : styleBound.Height;
                return(new Bound(w, h));
            }
            return(styleBound);
        }