Monotouch Custom Font with Attributes - c#

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);

Related

How to create MahApps.Metro accent resource from specific color?

I've noticed this on the MahApps site (bottom of the page): "It is also possible to create an accent resource dictionary dynamically by using a specific color.", but I found more nowhere.
Is there really a built-in method (or anything else) for this?I've only found ThemeManager.AddAccent(string name, Uri resourceAddress), and creating a new accent (currently) possible with new Accent(string name, Uri resourceAddress), so a name and a resource uri is everyhow needed...Any ideas?
Here is a simple sample to create a dynamic resource dictionary and add it to the ThemeManager:
public static class ThemeManagerHelper
{
public static void CreateAppStyleBy(Color color, bool changeImmediately = false)
{
// create a runtime accent resource dictionary
var resourceDictionary = new ResourceDictionary();
resourceDictionary.Add("HighlightColor", color);
resourceDictionary.Add("AccentColor", Color.FromArgb((byte)(204), color.R, color.G, color.B));
resourceDictionary.Add("AccentColor2", Color.FromArgb((byte)(153), color.R, color.G, color.B));
resourceDictionary.Add("AccentColor3", Color.FromArgb((byte)(102), color.R, color.G, color.B));
resourceDictionary.Add("AccentColor4", Color.FromArgb((byte)(51), color.R, color.G, color.B));
resourceDictionary.Add("HighlightBrush", new SolidColorBrush((Color)resourceDictionary["HighlightColor"]));
resourceDictionary.Add("AccentColorBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("AccentColorBrush2", new SolidColorBrush((Color)resourceDictionary["AccentColor2"]));
resourceDictionary.Add("AccentColorBrush3", new SolidColorBrush((Color)resourceDictionary["AccentColor3"]));
resourceDictionary.Add("AccentColorBrush4", new SolidColorBrush((Color)resourceDictionary["AccentColor4"]));
resourceDictionary.Add("WindowTitleColorBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("ProgressBrush", new LinearGradientBrush(
new GradientStopCollection(new[]
{
new GradientStop((Color)resourceDictionary["HighlightColor"], 0),
new GradientStop((Color)resourceDictionary["AccentColor3"], 1)
}),
new Point(0.001, 0.5), new Point(1.002, 0.5)));
resourceDictionary.Add("CheckmarkFill", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("RightArrowFill", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("IdealForegroundColor", Colors.White);
resourceDictionary.Add("IdealForegroundColorBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
resourceDictionary.Add("AccentSelectedColorBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
// DataGrid brushes since latest alpha after 1.1.2
resourceDictionary.Add("MetroDataGrid.HighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("MetroDataGrid.HighlightTextBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
resourceDictionary.Add("MetroDataGrid.MouseOverHighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor3"]));
resourceDictionary.Add("MetroDataGrid.FocusBorderBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor"]));
resourceDictionary.Add("MetroDataGrid.InactiveSelectionHighlightBrush", new SolidColorBrush((Color)resourceDictionary["AccentColor2"]));
resourceDictionary.Add("MetroDataGrid.InactiveSelectionHighlightTextBrush", new SolidColorBrush((Color)resourceDictionary["IdealForegroundColor"]));
// applying theme to MahApps
var resDictName = string.Format("ApplicationAccent_{0}.xaml", color.ToString().Replace("#", string.Empty));
var fileName = Path.Combine(Path.GetTempPath(), resDictName);
using (var writer = System.Xml.XmlWriter.Create(fileName, new System.Xml.XmlWriterSettings { Indent = true }))
{
System.Windows.Markup.XamlWriter.Save(resourceDictionary, writer);
writer.Close();
}
resourceDictionary = new ResourceDictionary() { Source = new Uri(fileName, UriKind.Absolute) };
var newAccent = new Accent { Name = resDictName, Resources = resourceDictionary };
ThemeManager.AddAccent(newAccent.Name, newAccent.Resources.Source);
if (changeImmediately)
{
var application = Application.Current;
var applicationTheme = ThemeManager.AppThemes.First(x => string.Equals(x.Name, "BaseLight"));
ThemeManager.ChangeAppStyle(application, newAccent, applicationTheme);
}
}
}
Usage:
ThemeManagerHelper.CreateAppStyleBy(Colors.Indigo, true);
This is taken from my code samples (MahAppsMetroThemesSample) hosted at GitHub
Hope this helps!
ThemeManager has since been changed and now natively supports creating a custom theme from a color.
Here's an example from MahApps documentation:
// Add Dark and Light versions of DarkRed
ThemeManager.Current.AddTheme(new Theme("CustomDarkRed", "CustomDarkRed", "Dark", "Red", Colors.DarkRed, Brushes.DarkRed, true, false));
ThemeManager.Current.AddTheme(new Theme("CustomLightRed", "CustomLightRed", "Light", "Red", Colors.DarkRed, Brushes.DarkRed, true, false));
//Set current theme to CustomDarkRed
ThemeManager.Current.ChangeTheme(this, "CustomDarkRed");

HTMLWorker itextSharp image src

I am trying to use HTMLWorker using the following:
public static string toWorks(string s)
{
string fontpath = System.Web.HttpContext.Current.Server.MapPath("~/Content/");
BaseFont bf = BaseFont.CreateFont(fontpath + "ARIALUNI.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var f = new Font(bf, 10, Font.NORMAL);
// var p = new Paragraph { Alignment = Element.ALIGN_LEFT, Font = f };
var styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.SPAN, HtmlTags.FONTSIZE, "10");
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
using (var sr = new StringReader(s))
{
List<IElement> list = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);
// var elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);
foreach (var e in list)
{
list.Add(e);
}
return list.ToString();
}
return null;
}
It converts:
src="/Content/UserFiles/635380078478327671/Images/test.png
To:
C:\Content\UserFiles\635380078478327671\Images\test.png
Any suggestion.
Please compare the following two examples:
HtmlMovies1
HtmlMovies2
If you use the first example to render an HTML file with images, you probably won't succeed. The second example introduces an ImageProvider implementation.
In the getImage() method of the ImageProvider interface, you get information about the path to an image. It is up to you to interpret this path. For instance: if the path is /Content/UserFiles/635380078478327671/Images/test.png, you can create an Image object by loading the bytes from that path, possibly after applying some minor changes to the path.
If you don't create an ImageProvider class, iText will do a single guess to find the path. In your case, that guess is wrong.
You can find the C# equivalent of the examples here: http://tinyurl.com/itextsharpIIA2C09

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 ();
}

Display NSAttributedString using a CATextLayer in Monotouch

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

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;
}

Categories

Resources