Display NSAttributedString using a CATextLayer in Monotouch - c#

I'm using a CATextLayer to render an NSAttributedString. When done in this method the color does not render correctly. When done using CTStringAttributes the color works, but the NSAttributedString does not know its own size. This is the code I'm using:
var caTextLayer = new CATextLayer ();
var attributedString = new NSAttributedString
(
"test string",
ForegroundColor = UIColor.Blue.CGColor,
Font = new CTFont ("Arial", 24),
KerningAdjustment = 72f
);
caTextLayer.AttributedString = attributedString;
caTextLayer.Frame = UIScreen.MainScreen.Bounds;
caTextLayer.ContentsScale = UIScreen.MainScreen.Scale;
myViewController.View.Layer.InsertSublayer(layer3, 1);
caTextLayer.SetNeedsDisplay ();

You need to use CTStringAttributes if you are using CoreText layers

Related

How to calculate the average color of a superpixel in opencvsharp C#

I am trying to calculate the average(mean) color of a superpixel without luck, I am using the astronaut image as an example and want to display the last image as result, I would really appreciate it if someone can help.
using OpenCvSharp;
using OpenCvSharp.XImgProc;
var imgPath = "C:\\img";
var imgName = "astronaut.png";
var imgFullPath = Path.Combine(imgPath, imgName);
var bgrImage = Cv2.ImRead(imgFullPath, ImreadModes.Color);
var hsvImage = new Mat();
Cv2.CvtColor(bgrImage, hsvImage, ColorConversionCodes.BGR2HSV);
var superpixelSLIC = SuperpixelSLIC.Create(bgrImage, SLICType.SLIC, 75);
superpixelSLIC.Iterate();
superpixelSLIC.EnforceLabelConnectivity();
var numberOfSuperpixels = superpixelSLIC.GetNumberOfSuperpixels();
var labels = new Mat(bgrImage.Size(), MatType.CV_32SC1);
superpixelSLIC.GetLabels(labels);
using var labelContourMask = new Mat();
superpixelSLIC.GetLabelContourMask(labelContourMask);
Cv2.ImShow("labelContourMask", labelContourMask);
Cv2.ImShow("bgrImage", bgrImage);
Cv2.WaitKey();
Cv2.DestroyAllWindows();
Input image
Expected result
I tried to display the labels but getting an error, I am not sure if displaying the labels is needed but I tried it anyway
Cv2.ImShow("labels", labels);
Cv2.WaitKey();
Cv2.DestroyAllWindows();

How to customize a tooltip for each individual datapoint in a wpf toolkit lineseries chart?

I have seen several questions about doing a custom tooltip for a single line series.
I need a custom tool-tip for each data-point. I would like to add more to the tool-tip than just the dependent value path and the independent value path.
Example I have 2 data points on the same line one with a value(Y-axis)
of 2, date(x-axis) of 4/28/2016, and configuration of A. The other has
a value of 3, date of 4/29/2016, and configuration of B.
How would I also show the configurations? This is all done in code behind because I have a dynamic number of lineseries. So I can't just assign a style to each lineseries in the xaml.
var MyLineSeries = new LineSeries();
lMyLineSeries.DependentValuePath = "Y";
lMyLineSeries.IndependentValuePath = "X";
lMyLineSeries.DataPointStyle = lToolTipDataPointStyle;
This is my code for creating the tool tip style.
var lToolTipDataPointStyle = new Style(typeof(LineDataPoint));
var lTemplate = new ControlTemplate(typeof(LineDataPoint));
var lGridElement = new FrameworkElementFactory(typeof(Border));
//Tooltip
var lStackPanel = new StackPanel();
var lValueContentControl = new ContentControl();
lValueContentControl.SetBinding(ContentControl.ContentProperty, new Binding(myLineSeries.DependentValuePath));
lValueContentControl.ContentStringFormat = "Value: {0}";
var lConfigurationContentControl = new ContentControl();
lConfigurationContentControl.SetBinding(ContentControl.ContentProperty, new Binding())//This is what Idk what to bind to???
lConfigurationContentControl.ContentStringFormat = "Configuration: {0}";
lStackPanel.Children.Add(lValueContentControl);
lStackPanel.Children.Add(lConfigurationContentControl);
lGridElement.SetValue(ToolTipService.ToolTipProperty, lStackPanel);
var lEllipseElement = new FrameworkElementFactory(typeof(Ellipse));
lEllipseElement.SetValue(Ellipse.StrokeThicknessProperty, new TemplateBindingExtension(Border.BorderThicknessProperty));
lEllipseElement.SetValue(Ellipse.StrokeProperty, new TemplateBindingExtension(Border.BorderBrushProperty));
lEllipseElement.SetValue(Ellipse.FillProperty, new TemplateBindingExtension(Grid.BackgroundProperty));
lGridElement.AppendChild(lEllipseElement);
lTemplate.VisualTree = lGridElement;
var lTemplateSetter = new Setter();
lTemplateSetter.Property = LineDataPoint.TemplateProperty;
lTemplateSetter.Value = lTemplate;
lToolTipDataPointStyle.Setters.Add(lTemplateSetter);
return lToolTipDataPointStyle;
I figured it out by using the Tag on the Line series.
myLineSeries.Tag = "Configuration";
var lConfigurationContentControl = new ContentControl();
lConfigurationContentControl.SetBinding(ContentControl.ContentProperty, new Binding(myLineSeries.Tag.ToString()))
lConfigurationContentControl.ContentStringFormat = "Configuration: {0}";

Showing Font Selection Dialog in GTK#

In MY gtk# Application im trying to show the font selection dialog.Im trying to use the following code,but the FontSelectionDialog constructor need some arguments also does the control execution wait for a font to be selected to set the string font
Can someone guide me?
Gtk.FontSelectionDialog fs = new FontSelectionDialog()
fs.Show ();
font=fs.FontName;
Updated according to additional question
This should help:
FontSelectionDialog dialog = null;
try {
dialog = new FontSelectionDialog("Choose a font");
dialog.Run ();
var name = dialog.FontName;
var pattern = #"^(?<fontName>.*)\s(?<fontSize>\d+(?:\.\d+)?)$";
var regex = new Regex(pattern);
var match = regex.Match(name);
if(match.Success)
{
var fontName = match.Groups["fontName"].Value;
var fontSize = float.Parse(match.Groups["fontSize"].Value);
var font = new System.Drawing.Font(fontName, fontSize);
}
} finally {
if (dialog != null)
dialog.Destroy ();
}

Font rendering with custom font widths using Xamarin.iOS

I'm using Xamarin.iOS and need to render text strings that have a custom amount of spacing between each gylph. I am currently building a NSMutableAttributedString in which I am passing my font and paragraphstyple information. I been able to display the text string but I need to be able to set a custom amount of spacing between each gylph. Is there a dictionary element that I can add to allow me to configure this value in iOS? Below is the method I am using to build the attributedstring.
public NSMutableAttributedString Build()
{
// Create a new attributed string from text
var atts = new NSMutableAttributedString(String);
var attRange = new NSRange(0, atts.Length);
var attsDic = new NSMutableDictionary();
var fontObject = new NSObject(Font.Handle);
attsDic.Add((NSString)"NSFont", fontObject);
var alignmentSettings = new CTParagraphStyleSettings()
{
Alignment = CTTextAlignment.Left,
};
var paragraphStyle = new CTParagraphStyle(alignmentSettings);
var psObject = new NSObject(paragraphStyle.Handle);
attsDic.Add((NSString)"NSParagraphStyle", psObject);
atts.SetAttributes(attsDic, attRange);
return atts;
}

Monotouch Custom Font with Attributes

How can i specify font bold/italic etc. properties in monotouch?
Actually possible in native library
http://www.freetimestudios.com/2010/09/20/ipad-and-ios-4-custom-font-loading/
NSDictionary *fontAttributes =
[NSDictionary dictionaryWithObjectsAndKeys:
#"Courier", (NSString *)kCTFontFamilyNameAttribute,
#"Bold", (NSString *)kCTFontStyleNameAttribute,
[NSNumber numberWithFloat:16.f], (NSString *)kCTFontSizeAttribute,
nil];
CTFontDescriptorRef descriptor =
CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attributes);
CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0, NULL);
CFRelease(descriptor);
The MonoTouch / C# code to match your code snippet would look like this:
CTFontDescriptorAttributes fda = new CTFontDescriptorAttributes () {
FamilyName = "Courier",
StyleName = "Bold",
Size = 16.0f
};
CTFontDescriptor fd = new CTFontDescriptor (fda);
CTFont font = new CTFont (fd, 0);

Categories

Resources