/// <summary> /// Handles the Element Changed messages /// </summary> /// <param name="e">The e.</param> protected override void OnElementChanged(ElementChangedEventArgs <Label> e) { base.OnElementChanged(e); if (this.Control != null && e.NewElement != null) { if (_helper == null) { _helper = new TextViewRenderHelper(Context); } if (_iconSpan == null) { _nativeLabel = (Android.Widget.TextView) this.Control; _iconLabel = (IconLabel)e.NewElement; //Set default value if (_iconLabel.IconSize == 0) { _iconLabel.IconSize = _iconLabel.FontSize; } _iconFont = _helper.TrySetFont("fontawesome-webfont.ttf"); _textFont = _iconLabel.Font.ToTypeface(); } SetText(); } }
protected override void OnElementChanged(ElementChangedEventArgs<Label> e) { base.OnElementChanged(e); iconLabel = (IconLabel)Element; nativeLabel = Control; if (iconLabel != null && nativeLabel != null && !string.IsNullOrEmpty(iconLabel.Icon)) SetText(iconLabel, nativeLabel); }
protected override void OnElementChanged(ElementChangedEventArgs <Label> e) { base.OnElementChanged(e); iconLabel = (IconLabel)Element; nativeLabel = Control; if (iconLabel != null && nativeLabel != null && !string.IsNullOrEmpty(iconLabel.Icon)) { SetText(iconLabel, nativeLabel); } }
/// <summary> /// Gets the font for the button (applied to all button text EXCEPT the icon) /// </summary> /// <param name="iconLabel"></param> /// <param name="targetLabel"></param> /// <returns></returns> private UIFont GetButtonFont(IconLabel iconLabel, UILabel targetLabel, nfloat fontSize) { UIFont btnTextFont = iconLabel.Font.ToUIFont(); if (iconLabel.Font != Font.Default && btnTextFont != null) { return(btnTextFont); } else if (iconLabel.Font == Font.Default) { return(UIFont.SystemFontOfSize(fontSize)); } return(btnTextFont); }
/// <summary> /// Handles the Element Changed messages /// </summary> /// <param name="e">The e.</param> protected override void OnElementChanged(ElementChangedEventArgs<Label> e) { base.OnElementChanged(e); if (this.Control != null && e.NewElement != null) { if (_helper == null) _helper = new TextViewRenderHelper(Context); if (_iconSpan == null) { _nativeLabel = (Android.Widget.TextView)this.Control; _iconLabel = (IconLabel)e.NewElement; //Set default value if (_iconLabel.IconSize == 0) _iconLabel.IconSize = _iconLabel.FontSize; _iconFont = _helper.TrySetFont("fontawesome-webfont.ttf"); _textFont = _iconLabel.Font.ToTypeface(); } SetText(); } }
protected override void OnElementChanged(ElementChangedEventArgs<Label> e) { base.OnElementChanged(e); if (this.Control != null && e.NewElement != null) { if (iconSpan == null) { nativeLabel = (Android.Widget.TextView)this.Control; iconLabel = (IconLabel)e.NewElement; //Set default value if (iconLabel.IconSize == 0) iconLabel.IconSize = iconLabel.FontSize; iconFont = TrySetFont("fontawesome-webfont.ttf"); textFont = iconLabel.Font.ToTypeface(); SetText(); } } }
protected override void OnElementChanged(ElementChangedEventArgs <Label> e) { base.OnElementChanged(e); if (this.Control != null && e.NewElement != null) { if (iconSpan == null) { nativeLabel = (Android.Widget.TextView) this.Control; iconLabel = (IconLabel)e.NewElement; //Set default value if (iconLabel.IconSize == 0) { iconLabel.IconSize = iconLabel.FontSize; } iconFont = TrySetFont("fontawesome-webfont.ttf"); textFont = iconLabel.Font.ToTypeface(); SetText(); } } }
private void SetText(IconLabel iconLabel, UILabel targetLabel) { var renderedIcon = iconLabel.Icon; var iconFontName = string.IsNullOrEmpty(iconLabel.IconFontName) ? "fontawesome" : iconLabel.IconFontName; var iconSize = (iconLabel.IconSize == 0) ? iconLabel.FontSize : iconLabel.IconSize; var faFont = UIFont.FromName(iconFontName, (float)iconSize); string combinedText = null; string separator = " "; if (iconLabel.ShowIconSeparator) separator = " | "; switch (iconLabel.Orientation) { case ImageOrientation.ImageToLeft: if (string.IsNullOrEmpty(iconLabel.Text)) combinedText = renderedIcon; else { combinedText = renderedIcon + separator + iconLabel.Text; } break; case ImageOrientation.ImageToRight: if (string.IsNullOrEmpty(iconLabel.Text)) combinedText = renderedIcon; else combinedText = iconLabel.Text + separator + renderedIcon; break; case ImageOrientation.ImageOnTop: throw new NotSupportedException("Image orientation top and bottom are not supported on iOS"); case ImageOrientation.ImageOnBottom: throw new NotSupportedException("Image orientation top and bottom are not supported on iOS"); } // string attributes for the icon var iconAttributes = new UIStringAttributes { ForegroundColor = iconLabel.IconColor.ToUIColor(), BackgroundColor = targetLabel.BackgroundColor, Font = faFont, TextAttachment = new NSTextAttachment() }; // TODO: Calculate an appropriate BaselineOffset for the main button text in order to center it vertically relative to the icon var btnAttributes = new UIStringAttributes { BackgroundColor = iconLabel.BackgroundColor.ToUIColor(), ForegroundColor = iconLabel.TextColor.ToUIColor(), Font = GetButtonFont(iconLabel, targetLabel, 17f), }; if (!string.IsNullOrEmpty(iconLabel.Text)) btnAttributes.BaselineOffset = 3; // Give the overall string the attributes of the button's text var prettyString = new NSMutableAttributedString(combinedText, btnAttributes); // Set the font for only the icon (1 char) prettyString.SetAttributes(iconAttributes.Dictionary, iconLabel.Orientation == ImageOrientation.ImageToLeft ? new NSRange(0, 1) : new NSRange(prettyString.Length - 1, 1)); // set the final formatted string as the button's text targetLabel.AttributedText = prettyString; if (iconLabel.TextAlignement == TextAlignment.Center) { // center the button's contents targetLabel.TextAlignment = UITextAlignment.Center; } else if (iconLabel.TextAlignement == TextAlignment.End) { targetLabel.TextAlignment = UITextAlignment.Right; } else if (iconLabel.TextAlignement == TextAlignment.Start) { targetLabel.TextAlignment = UITextAlignment.Left; } }
/// <summary> /// Gets the font for the button (applied to all button text EXCEPT the icon) /// </summary> /// <param name="iconLabel"></param> /// <param name="targetLabel"></param> /// <returns></returns> private UIFont GetButtonFont(IconLabel iconLabel, UILabel targetLabel, nfloat fontSize) { UIFont btnTextFont = iconLabel.Font.ToUIFont(); if (iconLabel.Font != Font.Default && btnTextFont != null) return btnTextFont; else if (iconLabel.Font == Font.Default) return UIFont.SystemFontOfSize(fontSize); return btnTextFont; }
private void SetText(IconLabel iconLabel, UILabel targetLabel) { var renderedIcon = iconLabel.Icon; var iconFontName = string.IsNullOrEmpty(iconLabel.IconFontName) ? "fontawesome" : iconLabel.IconFontName; var iconSize = (iconLabel.IconSize == 0) ? iconLabel.FontSize : iconLabel.IconSize; var faFont = UIFont.FromName(iconFontName, (float)iconSize); string combinedText = null; string separator = " "; if (iconLabel.ShowIconSeparator) { separator = " | "; } switch (iconLabel.Orientation) { case ImageOrientation.ImageToLeft: if (string.IsNullOrEmpty(iconLabel.Text)) { combinedText = renderedIcon; } else { combinedText = renderedIcon + separator + iconLabel.Text; } break; case ImageOrientation.ImageToRight: if (string.IsNullOrEmpty(iconLabel.Text)) { combinedText = renderedIcon; } else { combinedText = iconLabel.Text + separator + renderedIcon; } break; case ImageOrientation.ImageOnTop: throw new NotSupportedException("Image orientation top and bottom are not supported on iOS"); case ImageOrientation.ImageOnBottom: throw new NotSupportedException("Image orientation top and bottom are not supported on iOS"); } // string attributes for the icon var iconAttributes = new UIStringAttributes { ForegroundColor = iconLabel.IconColor.ToUIColor(), BackgroundColor = targetLabel.BackgroundColor, Font = faFont, TextAttachment = new NSTextAttachment() }; // TODO: Calculate an appropriate BaselineOffset for the main button text in order to center it vertically relative to the icon var btnAttributes = new UIStringAttributes { BackgroundColor = iconLabel.BackgroundColor.ToUIColor(), ForegroundColor = iconLabel.TextColor.ToUIColor(), Font = GetButtonFont(iconLabel, targetLabel, 17f), }; if (!string.IsNullOrEmpty(iconLabel.Text)) { btnAttributes.BaselineOffset = 3; } // Give the overall string the attributes of the button's text var prettyString = new NSMutableAttributedString(combinedText, btnAttributes); // Set the font for only the icon (1 char) prettyString.SetAttributes(iconAttributes.Dictionary, iconLabel.Orientation == ImageOrientation.ImageToLeft ? new NSRange(0, 1) : new NSRange(prettyString.Length - 1, 1)); // set the final formatted string as the button's text targetLabel.AttributedText = prettyString; if (iconLabel.TextAlignement == TextAlignment.Center) { // center the button's contents targetLabel.TextAlignment = UITextAlignment.Center; } else if (iconLabel.TextAlignement == TextAlignment.End) { targetLabel.TextAlignment = UITextAlignment.Right; } else if (iconLabel.TextAlignement == TextAlignment.Start) { targetLabel.TextAlignment = UITextAlignment.Left; } }
public IconLabelPage() { this.Title = "Icon Label (Font anwsome label)"; this.BackgroundColor = Color.Black; IconLabel label1 = new IconLabel() { Icon = "\uf1d8", Text = "Text and icon", TextColor = Color.White, IconColor = Color.White }; IconLabel label2 = new IconLabel() { Icon = "\uf1d8", Text = "Different color and size", IconColor = Color.Red, TextColor = Color.White, IconSize = 50, FontSize = 20 }; IconLabel label3 = null; Device.OnPlatform(null,() => { //Orientation boottom and top only supported on Android label3 = new IconLabel() { Icon = "\uf1d8", Text = "Orientation icon on top", TextColor = Color.White, IconColor = Color.White, FontSize = 20, Orientation = Enums.ImageOrientation.ImageOnTop }; },null); IconLabel label4 = new IconLabel() { Icon = "\uf1d8", Text = "Orientation icon on the right", TextColor = Color.White, IconColor = Color.White, FontSize = 20, Orientation = Enums.ImageOrientation.ImageToRight }; IconLabel label5 = null; Device.OnPlatform(null,() => { //Orientation boottom and top only supported on Android label5 = new IconLabel() { Icon = "\uf1d8", Text = "Orientation icon on bottom", TextColor = Color.White, IconColor = Color.White, FontSize = 20, Orientation = Enums.ImageOrientation.ImageOnBottom, }; },null); IconLabel label6 = new IconLabel() { Icon = "\uf1d8", Text = "TextAlignment Center", TextColor = Color.White, IconColor = Color.White, FontSize = 20, TextAlignement = TextAlignment.Center }; IconLabel label7 = new IconLabel() { Icon = "\uf1d8", Text = "TextAlignment End", TextColor = Color.White, IconColor = Color.White, FontSize = 20, TextAlignement = TextAlignment.End }; IconLabel label8 = new IconLabel() { Icon = "\uf1d8", Text = " TextAlignment Start", TextColor = Color.White, IconColor = Color.White, FontSize = 20, TextAlignement = TextAlignment.Start }; IconLabel label9 = new IconLabel() { Icon = "\uf1d8", Text = "With icon separator", ShowIconSeparator = true, TextColor = Color.Red, IconColor = Color.White, IconSize = 20, FontSize = 20 }; string up = "\uf102"; string down = "\uf103"; IconLabel SwitchIcon = new IconLabel() { Icon = up, IconColor = Color.Blue, IconSize = 30, }; Button switchBtn = new Button() { Text = "Toggle icon up/down", TextColor = Color.White }; switchBtn.Clicked += (s, a) => { SwitchIcon.Icon = SwitchIcon.Icon == up ? down : up; }; StackLayout content = null; Device.OnPlatform(()=> { content = new StackLayout() { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, Children ={ label1, label2, label4, label6, label7, label8, label9, new StackLayout() { Orientation = StackOrientation.Vertical, Children= { switchBtn, SwitchIcon } } } }; }, () => { content = new StackLayout() { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, Children ={ label1, label2, label3, label4, label5, label6, label7, label8, label9, new StackLayout() { Orientation = StackOrientation.Vertical, Children= { switchBtn, SwitchIcon } } } }; },null); this.Content = content; }