예제 #1
0
		private void SetPlaceholder(ExtendedLabel view)
		{
			if (view == null)
			{
				return;
			}

			if (view.FormattedText != null)
			{
				this.Control.AttributedText = view.FormattedText.ToAttributed(view.Font, view.TextColor);
				LayoutSubviews();
				return;
			}

			if (!string.IsNullOrEmpty(view.Text))
			{
				this.UpdateUi(view);
				LayoutSubviews();
				return;
			}

			if (string.IsNullOrWhiteSpace(view.Placeholder) && view.FormattedPlaceholder == null)
			{
				return;
			}

			var formattedPlaceholder = view.FormattedPlaceholder ?? view.Placeholder;

			Control.AttributedText = formattedPlaceholder.ToAttributed(view.Font, view.TextColor);

			LayoutSubviews();
		}
예제 #2
0
		/// <summary>
		/// Updates the UI.
		/// </summary>
		/// <param name="view">
		/// The view.
		/// </param>
		private void UpdateUi(ExtendedLabel view)
		{
			// Prefer font set through Font property.
			if (view.Font == Font.Default)
			{
				if (view.FontSize > 0)
				{
					this.Control.Font = UIFont.FromName(this.Control.Font.Name, (float)view.FontSize);
				}

				if (!string.IsNullOrEmpty(view.FontName))
				{
					var fontName = Path.GetFileNameWithoutExtension(view.FontName);

					var font = UIFont.FromName(fontName, this.Control.Font.PointSize);

					if (font != null)
					{
						this.Control.Font = font;
					}
				}

				#region ======= This is for backward compatability with obsolete attrbute 'FontNameIOS' ========
				if (!string.IsNullOrEmpty(view.FontNameIOS))
				{
					var font = UIFont.FromName(view.FontNameIOS, (view.FontSize > 0) ? (float)view.FontSize : 12.0f);

					if (font != null)
					{
						this.Control.Font = font;
					}
				}
				#endregion ====== End of obsolete section ==========================================================
			}

			//Do not create attributed string if it is not necesarry
			//if (!view.IsUnderline && !view.IsStrikeThrough && !view.IsDropShadow)
			//{
			//    return;
			//}

			var underline = view.IsUnderline ? NSUnderlineStyle.Single : NSUnderlineStyle.None;
			var strikethrough = view.IsStrikeThrough ? NSUnderlineStyle.Single : NSUnderlineStyle.None;

			NSShadow dropShadow = null;

			if (view.IsDropShadow)
			{
				dropShadow = new NSShadow
				{
					ShadowColor = view.DropShadowColor.ToUIColor(),
					ShadowBlurRadius = 1.4f,
					ShadowOffset = new CoreGraphics.CGSize(new CoreGraphics.CGPoint(0.3f, 0.8f))
				};
			}

			// For some reason, if we try and convert Color.Default to a UIColor, the resulting color is
			// either white or transparent. The net result is the ExtendedLabel does not display.
			// Only setting the control's TextColor if is not Color.Default will prevent this issue.
			if (view.TextColor != Color.Default)
			{
				this.Control.TextColor = view.TextColor.ToUIColor();
			}

			this.Control.AttributedText = new NSMutableAttributedString(view.Text,
				this.Control.Font,
				underlineStyle: underline,
				strikethroughStyle: strikethrough,
				shadow: dropShadow); ;
		}
예제 #3
0
        /// <summary>
        /// Updates the UI.
        /// </summary>
        /// <param name="view">
        /// The view.
        /// </param>
        /// <param name="control">
        /// The control.
        /// </param>
        private void UpdateUi(ExtendedLabel view, TextBlock control)
        {
            if (view.FontSize > 0)
                control.FontSize = (float)view.FontSize;
            //control.FontSize = (view.FontSize > 0) ? (float)view.FontSize : 12.0f;

            ////need to do this ahead of font change due to unexpected behaviour if done later.
            if (view.IsStrikeThrough)
            {
                //isn't perfect, but it's a start
                var border = new Border
                {
                    Height = 2,
                    Width = Control.ActualWidth,
                    Background = control.Foreground,
                    HorizontalAlignment = HorizontalAlignment.Center
                };
                Canvas.SetTop(border, control.FontSize * .72);
                //Canvas.SetTop(border, (this.Control.ActualHeight / 2) - 0.5);
                this.Children.Add(border);

                //// STILL IN DEVELOPMENT === alternative and possibly more flexible grid method - Got stuck making this controls parent the grid below

                //var strikeThroughGrid = new System.Windows.Controls.Grid();
                //strikeThroughGrid.VerticalAlignment = VerticalAlignment.Top;
                //strikeThroughGrid.HorizontalAlignment = HorizontalAlignment.Left;
                ////define rows
                //var colDef1 = new System.Windows.Controls.RowDefinition { Height = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star) };
                //var colDef2 = new System.Windows.Controls.RowDefinition { Height = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star) };
                //strikeThroughGrid.RowDefinitions.Add(colDef1);
                //strikeThroughGrid.RowDefinitions.Add(colDef2);

                ////add textblock
                //strikeThroughGrid.Children.Add(control);
                //System.Windows.Controls.Grid.SetRowSpan(control,2);

                ////add border
                //var strikethroughBorder = new Border{BorderThickness = new System.Windows.Thickness(0,0,0,2) , BorderBrush = control.Foreground , Padding = new System.Windows.Thickness(0,0,0,control.FontSize*0.3)};
                //strikeThroughGrid.Children.Add(strikethroughBorder);

            }

            if (!string.IsNullOrEmpty(view.FontName))
            {
                string filename = view.FontName;
                //if no extension given then assume and add .ttf
                if (filename.LastIndexOf(".", StringComparison.Ordinal) != filename.Length - 4)
                {
                    filename = string.Format("{0}.ttf", filename);
                }

                if (LocalFontFileExists(filename)) //only substitute custom font if exists
                {
                    control.FontFamily =
                        new FontFamily(string.Format(@"\Assets\Fonts\{0}#{1}", filename,
                            string.IsNullOrEmpty(view.FriendlyFontName)
                                ? filename.Substring(0, filename.Length - 4)
                                : view.FriendlyFontName));
                }
            }

            if (view.IsUnderline)
                control.TextDecorations = TextDecorations.Underline;
        }