public TextFormatValues TextFormat(IStyledObject control, TextFormatValues defaulValue = TextFormatValues.Text) { IDictionary <Type, IStyle> styles = _getStyles(control); if (styles.ContainsKey(typeof(TextFormat))) { var format = (TextFormat)styles[typeof(TextFormat)]; return(format.Format); } return(defaulValue); }
protected override IBound Apply(IStyleSheet stylesheet, IBound styleBound, IBound maxBound) { base.Apply(stylesheet, styleBound, maxBound); var style = stylesheet.Helper; // text-format _textFormat = style.TextFormat(this); SetText(); // background color, background image, borders using (var background = stylesheet.Background(this, styleBound)) SetBackground(background); // font stylesheet.SetFontSettings(this, _view, styleBound.Height); // text color _textColor = style.Color(this).ToColorOrTransparent(); _view.SetTextColor(_textColor); //selected color _selectedColor = style.SelectedColor(this).ToNullableColor(); //selected background _selectionBehaviour = new SelectionBehaviour(style.SelectedBackground(this).ToNullableColor(), this, stylesheet); // word wrap _singleLine = style.WhiteSpace(this) == WhiteSpaceKind.Nowrap; if (_singleLine) { _view.SetSingleLine(); } // text align TextAlignValues align = style.TextAlign(this, DefaultAlignValues); ApplyTextAlign(align); if (_singleLine) { _view.Ellipsize = align == TextAlignValues.Right ? TextUtils.TruncateAt.Start : TextUtils.TruncateAt.End; } // text padding int pl = style.PaddingLeft(this, styleBound.Width).Round(); int pt = style.PaddingTop(this, styleBound.Height).Round(); int pr = style.PaddingRight(this, styleBound.Width).Round(); int pb = style.PaddingBottom(this, styleBound.Height).Round(); _view.SetPadding(pl, pt, pr, pb); return(SizeToContent(styleBound, maxBound, style)); }
public override void FromString(string s) { s = s.Trim(); TextFormatValues result; if (!Enum.TryParse(s, true, out result)) { throw new Exception("Invalid text-format value: " + s); } Format = result; }
protected override IBound ReApply(IDictionary <Type, IStyle> styles, IBound styleBound, IBound maxBound) { IStyleHelper helper = StyleSheetContext.Current.CreateHelper(styles, CurrentStyleSheet, this); if (styles.Count > 0) { // text-format ITextFormat textFormat; if (helper.TryGet(out textFormat)) { _textFormat = textFormat.Format; SetText(); } // background color, background image, borders Drawable background; if (helper.BackgroundChanged(CurrentStyleSheet, Frame.Bound, out background)) { using (background) SetBackground(background); } // font helper.SetFontSettings(_view, styleBound.Height); // text color ITextColor textColor; if (helper.TryGet(out textColor)) { _textColor = textColor.ToColorOrTransparent(); _view.SetTextColor(_textColor); } //selected color ISelectedColor selectedColor; if (helper.TryGet(out selectedColor)) { _selectedColor = selectedColor.ToNullableColor(); } //selected background ISelectedBackground selectedBackground; if (helper.TryGet(out selectedBackground)) { _selectionBehaviour.SelectedColor = selectedBackground.ToNullableColor(); } // word wrap IWhiteSpace whiteSpace; if (helper.TryGet(out whiteSpace)) { _singleLine = whiteSpace.Kind == WhiteSpaceKind.Nowrap; _view.SetSingleLine(_singleLine); } // text align ITextAlign textAlign; if (helper.TryGet(out textAlign)) { ApplyTextAlign(textAlign.Align); if (_singleLine) { _view.Ellipsize = textAlign.Align == TextAlignValues.Right ? TextUtils.TruncateAt.Start : TextUtils.TruncateAt.End; } } // text padding int pl = helper.GetSizeOrDefault <IPaddingLeft>(styleBound.Width, _view.PaddingLeft).Round(); int pt = helper.GetSizeOrDefault <IPaddingTop>(styleBound.Height, _view.PaddingTop).Round(); int pr = helper.GetSizeOrDefault <IPaddingRight>(styleBound.Width, _view.PaddingRight).Round(); int pb = helper.GetSizeOrDefault <IPaddingBottom>(styleBound.Height, _view.PaddingBottom).Round(); _view.SetPadding(pl, pt, pr, pb); } IBound bound = SizeToContent(styleBound, maxBound, CurrentStyleSheet.Helper); // reload image with new size helper.ReloadBackgroundImage(CurrentStyleSheet, bound, this); return(bound); }
protected override IBound ReApply(IDictionary <Type, IStyle> styles, IBound styleBound, IBound maxBound) { // we forbid execution of SetSpannedString() _blinkAllowed = false; IStyleHelper helper = StyleSheetContext.Current.CreateHelper(styles, CurrentStyleSheet, this); if (styles.Count > 0) { _view.Text = _text; // background color, borders, background image UIImage image = helper.SetBackgroundSettings(this); if (image != null && image != _backgroundImage) { _backgroundImage.Dispose(); _backgroundImage = image; _view.Layer.Contents = _backgroundImage.CGImage; _view.BackgroundColor = UIColor.Clear; } // font UIFont f; if (helper.FontChanged(styleBound.Height, out f)) { _view.Font = f; } // word wrap IWhiteSpace whiteSpace; bool whiteSpaceChanged = helper.TryGet(out whiteSpace); bool nowrap = whiteSpace.Kind == WhiteSpaceKind.Nowrap; if (whiteSpaceChanged) { if (!nowrap) { _view.TextContainer.LineBreakMode = UILineBreakMode.WordWrap; _view.TextContainer.MaximumNumberOfLines = 0; } else { _view.TextContainer.LineBreakMode = UILineBreakMode.TailTruncation; _view.TextContainer.MaximumNumberOfLines = 1; } } // text-format ITextFormat textFormat; bool formatChanged = helper.TryGet(out textFormat); _textFormat = textFormat.Format; switch (_textFormat) { case TextFormatValues.Text: { _view.AttributedText = new NSAttributedString(); _view.Text = _text; // text color ITextColor textColor; if (helper.TryGet(out textColor) || formatChanged) { _view.TextColor = _textColor = textColor.ToColorOrClear(); } // selected-color ISelectedColor selectedColor; if (helper.TryGet(out selectedColor) || formatChanged) { _selectedColor = selectedColor.ToNullableColor(); } } break; case TextFormatValues.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}"); ITextColor textColor; formatChanged |= helper.TryGet(out textColor); _textHtmlSpan = string.Format(span, textColor.Value.Hex, "{0}"); // selected-color ISelectedColor selectedColor; formatChanged |= helper.TryGet(out selectedColor); if (selectedColor.ToNullableColor() != null) { _selectedHtmlSpan = string.Format(span, selectedColor.Value.Hex, "{0}"); } if (formatChanged) { SetSpannedText(_textHtmlSpan); } } break; default: throw new NotImplementedException("Text format not found: " + _textFormat); } // selected-background ISelectedBackground selectedBackground; if (helper.TryGet(out selectedBackground)) { UIColor color = selectedBackground.ToNullableColor(); if (_backgroundImage != null && color != null) { _selectedBackgroundImage = GetFilteredImage(_backgroundImage, color).CGImage; color.Dispose(); } else { _selectedBackground = color; } } // text align ITextAlign textAlign; if (helper.TryGet(out textAlign)) { SetTextAlign(textAlign.Align, nowrap); } // text padding float screenWidth = ApplicationContext.Current.DisplayProvider.Width; float screenHeight = ApplicationContext.Current.DisplayProvider.Height; float pl = helper.Get <IPaddingLeft>().CalcSize(styleBound.Width, screenWidth); float pt = helper.Get <IPaddingTop>().CalcSize(styleBound.Height, screenHeight); float pr = helper.Get <IPaddingRight>().CalcSize(styleBound.Width, screenWidth); float pb = helper.Get <IPaddingBottom>().CalcSize(styleBound.Height, screenHeight); _view.TextContainerInset = new UIEdgeInsets(pt, pl, pb, pr); } _blinkAllowed = true; // resize by background image IBound bound = GetBoundByImage(styleBound, maxBound, _backgroundImage); // size to content return(MergeBoundByContent(CurrentStyleSheet.Helper, bound, maxBound, _view.TextContainerInset, _backgroundImage != null)); }
protected override IBound Apply(IStyleSheet stylesheet, IBound styleBound, IBound maxBound) { base.Apply(stylesheet, styleBound, maxBound); _view.Text = _text; IStyleSheetHelper style = stylesheet.Helper; // background color, borders, background image _backgroundImage = stylesheet.SetBackgroundSettings(this); if (_backgroundImage != null) { _view.Layer.Contents = _backgroundImage.CGImage; _view.BackgroundColor = UIColor.Clear; } // text-format _textFormat = style.TextFormat(this); // font UIFont f = stylesheet.Font(this, styleBound.Height); if (f != null) { _view.Font = f; } switch (_textFormat) { case TextFormatValues.Text: _view.Text = _text; // text color _view.TextColor = _textColor = style.Color(this).ToColorOrClear(); // selected-color _selectedColor = style.SelectedColor(this).ToNullableColor(); break; case TextFormatValues.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.Color(this).Hex, "{0}"); // selected-color IColorInfo selectedColor = style.SelectedColor(this); if (selectedColor != null) { _selectedHtmlSpan = string.Format(span, selectedColor.Hex, "{0}"); } SetSpannedText(_textHtmlSpan); break; default: throw new NotImplementedException("Text format not found: " + _textFormat); } // selected-background UIColor selectedBackground = style.SelectedBackground(this).ToNullableColor(); if (selectedBackground != null) { if (_backgroundImage != null) { _selectedBackgroundImage = GetFilteredImage(_backgroundImage, selectedBackground).CGImage; selectedBackground.Dispose(); } else { _selectedBackground = selectedBackground; } } // word wrap bool nowrap = style.WhiteSpace(this) == WhiteSpaceKind.Nowrap; if (!nowrap) { _view.TextContainer.LineBreakMode = UILineBreakMode.WordWrap; _view.TextContainer.MaximumNumberOfLines = 0; } else { _view.TextContainer.LineBreakMode = UILineBreakMode.TailTruncation; _view.TextContainer.MaximumNumberOfLines = 1; } // text align SetTextAlign(style.TextAlign(this), nowrap); // text padding float pl = style.PaddingLeft(this, styleBound.Width); float pt = style.PaddingTop(this, styleBound.Height); float pr = style.PaddingRight(this, styleBound.Width); float pb = style.PaddingBottom(this, styleBound.Height); _view.TextContainerInset = new UIEdgeInsets(pt, pl, pb, pr); // resize by background image IBound bound = GetBoundByImage(styleBound, maxBound, _backgroundImage); // size to content return(MergeBoundByContent(style, bound, maxBound, _view.TextContainerInset, _backgroundImage != null)); }
protected override IBound Apply(IStyleSheet stylesheet, IBound styleBound, IBound maxBound) { base.Apply(stylesheet, styleBound, maxBound); IStyleSheetHelper style = stylesheet.Helper; // background color, borders, background image UIImage backgroundImage = stylesheet.SetBackgroundSettings(this); if (backgroundImage != null) { _view.SetBackgroundImage(backgroundImage, UIControlState.Normal); } // font UIFont f = stylesheet.Font(this, styleBound.Height); if (f != null) { _view.Font = f; } // word wrap bool nowrap = style.WhiteSpace(this) == WhiteSpaceKind.Nowrap; _view.TitleLabel.LineBreakMode = !nowrap ? UILineBreakMode.WordWrap : UILineBreakMode.TailTruncation; // text-format _textFormat = style.TextFormat(this); switch (_textFormat) { case TextFormatValues.Text: SetText(); // text color _textColor = style.Color(this).ToColorOrClear(); _view.SetTitleColor(_textColor, UIControlState.Normal); // selected-color using (UIColor selectedColor = style.SelectedColor(this).ToNullableColor()) if (selectedColor != null) { _view.SetTitleColor(selectedColor, UIControlState.Highlighted); } break; case TextFormatValues.Html: string whitespace = nowrap ? "nowrap" : "normal"; string span = string.Format( "<span style=\"font-family: {0}; font-size: {1:F0}; color: {2}; white-space: {3}\">{4}</span>" , _view.Font.FamilyName, _view.Font.PointSize, "{0}", whitespace, "{1}"); // text color _textHtmlSpan = string.Format(span, style.Color(this).Hex, "{0}"); // selected-color IColorInfo selectedColorInfo = style.SelectedColor(this); if (selectedColorInfo != null) { _selectedHtmlSpan = string.Format(span, selectedColorInfo.Hex, "{0}"); } SetSpannedText(_textHtmlSpan); break; default: throw new NotImplementedException("Text format not found: " + _textFormat); } // selected-background UIColor selectedBackground = style.SelectedBackground(this).ToNullableColor(); if (selectedBackground != null) { if (backgroundImage != null) { using (UIImage selectedImage = GetFilteredImage(backgroundImage, selectedBackground)) _view.SetBackgroundImage(selectedImage, UIControlState.Highlighted); selectedBackground.Dispose(); } else { _selectedBackground = selectedBackground; } } // text align SetTextAlign(style.TextAlign(this, TextAlignValues.Center), nowrap); { // text padding float pl = style.PaddingLeft(this, styleBound.Width); float pt = style.PaddingTop(this, styleBound.Height); float pr = style.PaddingRight(this, styleBound.Width); float pb = style.PaddingBottom(this, styleBound.Height); _view.ContentEdgeInsets = new UIEdgeInsets(pt, pl, pb, pr); // resize by background image IBound bound = GetBoundByImage(styleBound, maxBound, backgroundImage); // size to content bound = MergeBoundByContent(style, bound, maxBound, _view.ContentEdgeInsets, backgroundImage != null); _view.TitleLabel.PreferredMaxLayoutWidth = bound.Width - pl - pr; return(bound); } }