/// <summary> /// Update content for single and detail /// </summary> public static void UpdateContent(CalloutStyle callout) { if (callout.Type == CalloutType.Custom) { return; } if (callout.Title == null) { callout.Content = -1; return; } var styleSubtitle = new Topten.RichTextKit.Style(); var styleTitle = new Topten.RichTextKit.Style(); var textBlockTitle = new TextBlock(); var textBlockSubtitle = new TextBlock(); if (callout.Type == CalloutType.Detail) { styleSubtitle.FontFamily = callout.SubtitleFont.FontFamily; styleSubtitle.FontSize = (float)callout.SubtitleFont.Size; styleSubtitle.FontItalic = callout.SubtitleFont.Italic; styleSubtitle.FontWeight = callout.SubtitleFont.Bold ? 700 : 400; styleSubtitle.TextColor = callout.SubtitleFontColor.ToSkia(); textBlockSubtitle.AddText(callout.Subtitle, styleSubtitle); textBlockSubtitle.Alignment = callout.SubtitleTextAlignment.ToRichTextKit(); } styleTitle.FontFamily = callout.TitleFont.FontFamily; styleTitle.FontSize = (float)callout.TitleFont.Size; styleTitle.FontItalic = callout.TitleFont.Italic; styleTitle.FontWeight = callout.TitleFont.Bold ? 700 : 400; styleTitle.TextColor = callout.TitleFontColor.ToSkia(); textBlockTitle.Alignment = callout.TitleTextAlignment.ToRichTextKit(); textBlockTitle.AddText(callout.Title, styleTitle); textBlockTitle.MaxWidth = textBlockSubtitle.MaxWidth = (float)callout.MaxWidth; // Layout TextBlocks textBlockTitle.Layout(); textBlockSubtitle.Layout(); // Get sizes var width = Math.Max(textBlockTitle.MeasuredWidth, textBlockSubtitle.MeasuredWidth); var height = textBlockTitle.MeasuredHeight + (callout.Type == CalloutType.Detail ? textBlockSubtitle.MeasuredHeight + (float)callout.Spacing : 0f); // Now we have the correct width, so make a new layout cycle for text alignment textBlockTitle.MaxWidth = textBlockSubtitle.MaxWidth = width; textBlockTitle.Layout(); textBlockSubtitle.Layout(); // Create bitmap from TextBlock using (var rec = new SKPictureRecorder()) using (var canvas = rec.BeginRecording(new SKRect(0, 0, width, height))) { // Draw text to canvas textBlockTitle.Paint(canvas, new TextPaintOptions() { IsAntialias = true }); if (callout.Type == CalloutType.Detail) { textBlockSubtitle.Paint(canvas, new SKPoint(0, textBlockTitle.MeasuredHeight + (float)callout.Spacing), new TextPaintOptions() { IsAntialias = true }); } // Create a SKPicture from canvas var picture = rec.EndRecording(); if (callout.InternalContent >= 0) { BitmapRegistry.Instance.Set(callout.InternalContent, picture); } else { callout.InternalContent = BitmapRegistry.Instance.Register(picture); } callout.Content = callout.InternalContent; } }
/// <summary> /// Update content for single and detail /// </summary> public static void UpdateContent(CalloutStyle callout) { if (callout.Type == CalloutType.Custom) { return; } if (callout.Title == null) { return; } var _styleSubtitle = new Topten.RichTextKit.Style(); var _styleTitle = new Topten.RichTextKit.Style(); var _textBlockTitle = new TextBlock(); var _textBlockSubtitle = new TextBlock(); if (callout.Type == CalloutType.Detail) { _styleSubtitle.FontFamily = callout.SubtitleFont.FontFamily; _styleSubtitle.FontSize = (float)callout.SubtitleFont.Size; _styleSubtitle.FontItalic = callout.SubtitleFont.Italic; _styleSubtitle.FontWeight = callout.SubtitleFont.Bold ? 700 : 400; _styleSubtitle.TextColor = callout.SubtitleFontColor.ToSkia(); _textBlockSubtitle.AddText(callout.Subtitle, _styleSubtitle); _textBlockSubtitle.Alignment = callout.SubtitleTextAlignment.ToRichTextKit(); } _styleTitle.FontFamily = callout.TitleFont.FontFamily; _styleTitle.FontSize = (float)callout.TitleFont.Size; _styleTitle.FontItalic = callout.TitleFont.Italic; _styleTitle.FontWeight = callout.TitleFont.Bold ? 700 : 400; _styleTitle.TextColor = callout.TitleFontColor.ToSkia(); _textBlockTitle.Alignment = callout.TitleTextAlignment.ToRichTextKit(); _textBlockTitle.AddText(callout.Title, _styleTitle); _textBlockTitle.MaxWidth = _textBlockSubtitle.MaxWidth = (float)callout.MaxWidth; // Layout TextBlocks _textBlockTitle.Layout(); _textBlockSubtitle.Layout(); // Get sizes var width = Math.Max(_textBlockTitle.MeasuredWidth, _textBlockSubtitle.MeasuredWidth); var height = _textBlockTitle.MeasuredHeight + (callout.Type == CalloutType.Detail ? _textBlockSubtitle.MeasuredHeight + callout.Spacing : 0); // Now we have the correct width, so make a new layout cycle for text alignment _textBlockTitle.MaxWidth = _textBlockSubtitle.MaxWidth = width; _textBlockTitle.Layout(); _textBlockSubtitle.Layout(); // Create bitmap from TextBlock var info = new SKImageInfo((int)width, (int)height, SKImageInfo.PlatformColorType, SKAlphaType.Premul); using (var surface = SKSurface.Create(info)) { var canvas = surface.Canvas; var memStream = new MemoryStream(); canvas.Clear(SKColors.Transparent); // surface.Canvas.Scale(DeviceDpi / 96.0f); _textBlockTitle.Paint(canvas, new TextPaintOptions() { IsAntialias = true }); _textBlockSubtitle.Paint(canvas, new SKPoint(0, _textBlockTitle.MeasuredHeight + (float)callout.Spacing), new TextPaintOptions() { IsAntialias = true }); // Create image from canvas var image = surface.Snapshot(); var data = image.Encode(SKEncodedImageFormat.Png, 100); if (callout.InternalContent >= 0) { BitmapRegistry.Instance.Set(callout.InternalContent, data.AsStream(true)); } else { callout.InternalContent = BitmapRegistry.Instance.Register(data.AsStream(true)); } callout.Content = callout.InternalContent; } }