static nfloat GetFontWeight(UIFontWeight weight) { switch (weight) { case UIFontWeight.UltraLight: return(UIFontWeightConstants.UltraLight); case UIFontWeight.Thin: return(UIFontWeightConstants.Thin); case UIFontWeight.Light: return(UIFontWeightConstants.Light); case UIFontWeight.Regular: return(UIFontWeightConstants.Regular); case UIFontWeight.Medium: return(UIFontWeightConstants.Medium); case UIFontWeight.Semibold: return(UIFontWeightConstants.Semibold); case UIFontWeight.Bold: return(UIFontWeightConstants.Bold); case UIFontWeight.Heavy: return(UIFontWeightConstants.Heavy); case UIFontWeight.Black: return(UIFontWeightConstants.Black); default: throw new ArgumentException(weight.ToString()); } }
public static void SetupTextFieldAppearance(UITextField textField, UIColor textColor, nfloat textSize, string hint, UIColor hintColor, UIColor cursorColor, UIColor backgroundColor, UIFontWeight fontWeight = UIFontWeight.Regular, UIReturnKeyType returnKeyType = UIReturnKeyType.Default, UIView view = null, int tag = 0) { textField.TextColor = textColor; textField.Font = UIFont.SystemFontOfSize(textSize, fontWeight); textField.AttributedPlaceholder = new NSAttributedString(hint, new UIStringAttributes() { ForegroundColor = hintColor }); textField.TintColor = cursorColor; textField.BackgroundColor = backgroundColor; textField.ReturnKeyType = returnKeyType; if (view != null) { textField.Tag = tag; textField.ShouldReturn -= (field) => TextFieldShouldReturn(field, view); textField.ShouldReturn += (field) => TextFieldShouldReturn(field, view); } }
public static UIFont MonospacedDigitSystemFontOfSize(nfloat fontSize, UIFontWeight weight) { return(MonospacedDigitSystemFontOfSize(fontSize, GetFontWeight(weight))); }
public static UIFont SystemFontOfSize(nfloat size, UIFontWeight weight) { return(SystemFontOfSize(size, GetFontWeight(weight))); }
public static UIFont GetMonospacedSystemFont(nfloat size, UIFontWeight weight) => GetMonospacedSystemFont(size, GetFontWeight(weight));
public static void SetupLabelAppearance(UILabel label, string text, UIColor color, nfloat textSize, UIFontWeight fontWeight = UIFontWeight.Regular) { label.Text = text; label.TextColor = color; label.Font = UIFont.SystemFontOfSize(textSize, fontWeight); }
public static void SetupLabelAppearance(UILabel label, string text, UIColor color, nfloat textSize, UIFontWeight fontWeight = UIFontWeight.Regular, bool italic = false) { if (string.IsNullOrEmpty(text)) { text = ""; } label.Text = text; label.TextColor = color; if (italic) { label.Font = UIFont.ItalicSystemFontOfSize(textSize); } else { label.Font = UIFont.SystemFontOfSize(textSize, fontWeight); } }
internal static UIFont BestFont(string family, nfloat size, bool bold = false, bool italic = false, Assembly assembly = null) { if (family == null) { family = ".AppleSystemUIFont"; } if (family.ToLower() == "monospace") { family = "Menlo"; } if (family.ToLower() == "serif") { family = "Times New Roman"; } if (family.ToLower() == "sans-serif") { family = ".AppleSystemUIFont"; } if (size < 0) { size = (nfloat)(UIFont.LabelFontSize * Math.Abs(size)); } // is it in the cache? var key = new FontKey(family, size, bold, italic); Dictionary <FontKey, UIFont> dictionary = UiFontCache; lock (dictionary) { if (dictionary.TryGetValue(key, out UIFont storedUiFont)) { return(storedUiFont); } } UIFont bestAttemptFont = null; if (UIFont.FamilyNames.Contains(family)) { // a system font var fontNames = FontFamilies.FontsForFamily(family); string baseFontName = null; string reqFontName = null; //string fallbackFontName = null; if (fontNames != null && fontNames.Count > 0) { if (fontNames.Count == 1) { baseFontName = fontNames[0]; if (!bold && !italic) { reqFontName = fontNames[0]; } } else { int shortestMatchLength = int.MaxValue; int shortestBaseLength = int.MaxValue; foreach (var fontName in fontNames) { var lowerName = fontName.ToLower(); bool nameHasBold = lowerName.Contains("bold") || lowerName == "avenir-black"; bool nameHasItalic = lowerName.Contains("italic") || lowerName.Contains("oblique"); // assume the shortest name is the base font name if (lowerName.Length < shortestBaseLength) { baseFontName = fontName; shortestBaseLength = lowerName.Length; } // assume the shortest name with matching attributes is a match if (nameHasBold == bold && nameHasItalic == italic && lowerName.Length < shortestMatchLength) { reqFontName = fontName; shortestMatchLength = lowerName.Length; } if (lowerName.Contains("-regular")) { baseFontName = fontName; shortestBaseLength = -1; if (!bold && !italic) { reqFontName = fontName; shortestMatchLength = -1; break; } } } } } if (reqFontName != null) { bestAttemptFont = UIFont.FromName(reqFontName, size); } if (bestAttemptFont == null && baseFontName != null && !bold && !italic) { bestAttemptFont = UIFont.FromName(baseFontName, size); } } else { // an embedded font or a explicitly named system font? bestAttemptFont = EmbeddedFont(family, size, assembly); if (bestAttemptFont == null && !family.StartsWith(".SFUI")) { bestAttemptFont = UIFont.FromName(family, size); } } if (bestAttemptFont != null) { if (bold || italic) { if (bestAttemptFont.FontDescriptor.CreateWithTraits( (bold ? UIFontDescriptorSymbolicTraits.Bold : UIFontDescriptorSymbolicTraits.ClassUnknown) | (italic ? UIFontDescriptorSymbolicTraits.Italic : UIFontDescriptorSymbolicTraits.ClassUnknown) ) is UIFontDescriptor descriptor) { bestAttemptFont = UIFont.FromDescriptor(descriptor, size); } } // we have a match but is wasn't cached - so let's cache it for future reference lock (dictionary) { if (!dictionary.TryGetValue(key, out UIFont storedUiFont)) { // It could have been added by another thread so only add if we're really sure it's no there! dictionary.Add(key, bestAttemptFont); } } return(bestAttemptFont); } UIFontWeight fontWeight = UIFontWeight.Regular; if (family.StartsWith(".SFUI")) { var parts = family.Split("-"); if (parts.Length > 1) { var weightText = parts[1]; switch (weightText) { case nameof(UIFontWeight.Black): fontWeight = UIFontWeight.Black; break; case nameof(UIFontWeight.Bold): fontWeight = UIFontWeight.Bold; break; case nameof(UIFontWeight.Heavy): fontWeight = UIFontWeight.Heavy; break; case nameof(UIFontWeight.Light): fontWeight = UIFontWeight.Light; break; case nameof(UIFontWeight.Medium): fontWeight = UIFontWeight.Medium; break; case nameof(UIFontWeight.Regular): fontWeight = UIFontWeight.Medium; break; case nameof(UIFontWeight.Semibold): fontWeight = UIFontWeight.Semibold; break; case nameof(UIFontWeight.Thin): fontWeight = UIFontWeight.Thin; break; case nameof(UIFontWeight.UltraLight): fontWeight = UIFontWeight.UltraLight; break; default: fontWeight = UIFontWeight.Regular; break; } } // fall back to a system font } // fall back to a system font if (bold && italic || (fontWeight != UIFontWeight.Regular && (bold || italic))) { UIFont systemFont = UIFont.SystemFontOfSize(size, fontWeight); UIFontDescriptorSymbolicTraits traits = (bold ? UIFontDescriptorSymbolicTraits.Bold : (UIFontDescriptorSymbolicTraits)0) | (italic ? UIFontDescriptorSymbolicTraits.Italic : (UIFontDescriptorSymbolicTraits)0); var descriptor = systemFont.FontDescriptor.CreateWithTraits(traits); bestAttemptFont = UIFont.FromDescriptor(descriptor, size); } if (bestAttemptFont == null && italic) { bestAttemptFont = UIFont.ItalicSystemFontOfSize(size); } if (bestAttemptFont == null && bold) { bestAttemptFont = UIFont.BoldSystemFontOfSize(size); } if (bestAttemptFont == null) { bestAttemptFont = UIFont.SystemFontOfSize(size, fontWeight); } return(bestAttemptFont); }
public static UIFont GetSystemFont(UIFontTextStyle style, UIFontWeight weight = UIFontWeight.Regular) { var font = UIFont.GetPreferredFontForTextStyle(style); return(UIFont.SystemFontOfSize(size: font.PointSize, weight: weight)); }
public static void SetupButtonUnderlineAppearance(UIButton button, UIColor color, nfloat textSize, string title, UIFontWeight fontWeight = UIFontWeight.Regular) { var customString = new NSMutableAttributedString(title, new UIStringAttributes { Font = UIFont.SystemFontOfSize(textSize, fontWeight), ForegroundColor = color, UnderlineStyle = NSUnderlineStyle.Single, }); button.SetAttributedTitle(customString, UIControlState.Normal); }
public static void SetupButtonAppearance(UIButton button, UIColor color, nfloat textSize, string title, UIFontWeight fontWeight = UIFontWeight.Regular) { button.SetTitle(title, UIControlState.Normal); button.SetTitleColor(color, UIControlState.Normal); button.TitleLabel.Font = UIFont.SystemFontOfSize(textSize, fontWeight); }
static nfloat GetFontWeight(UIFontWeight weight) { switch (weight) { case UIFontWeight.UltraLight: return UIFontWeightConstants.UltraLight; case UIFontWeight.Thin: return UIFontWeightConstants.Thin; case UIFontWeight.Light: return UIFontWeightConstants.Light; case UIFontWeight.Regular: return UIFontWeightConstants.Regular; case UIFontWeight.Medium: return UIFontWeightConstants.Medium; case UIFontWeight.Semibold: return UIFontWeightConstants.Semibold; case UIFontWeight.Bold: return UIFontWeightConstants.Bold; case UIFontWeight.Heavy: return UIFontWeightConstants.Heavy; case UIFontWeight.Black: return UIFontWeightConstants.Black; default: throw new ArgumentException (weight.ToString ()); } }
public static UIFont SystemFontOfSize(nfloat size, UIFontWeight weight) { return SystemFontOfSize (size, GetFontWeight (weight)); }
public static UIFont MonospacedDigitSystemFontOfSize(nfloat fontSize, UIFontWeight weight) { return MonospacedDigitSystemFontOfSize (fontSize, GetFontWeight (weight)); }