public static UIFont GetFontWithTraits(this UIFontDescriptorSymbolicTraits traits, nfloat size) { var desc = new UIFontDescriptor(); desc = desc.CreateWithTraits(traits); return(UIFont.FromDescriptor(desc, size)); }
internal static bool IsBold(UIFont font) { UIFontDescriptor fontDescriptor = font.FontDescriptor; UIFontDescriptorSymbolicTraits traits = fontDescriptor.SymbolicTraits; return(traits.HasFlag(UIFontDescriptorSymbolicTraits.Bold)); }
UIFont MakeFont(string name, float fsize, UIFontDescriptorSymbolicTraits traits) { var desc = UIFontDescriptor.FromName(name, fsize); desc = desc.CreateWithTraits(traits); return(UIFont.FromDescriptor(desc, fsize)); }
void CreateNativeFont(FontFamily familyName, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont) { sizeInPoints = ConversionHelpers.GraphicsUnitConversion(unit, GraphicsUnit.Point, 96f, emSize); bold = FontStyle.Bold == (style & FontStyle.Bold); italic = FontStyle.Italic == (style & FontStyle.Italic); underLine = FontStyle.Underline == (style & FontStyle.Underline); this.size = emSize; this.unit = unit; var size = sizeInPoints * 96f / 72f; UIFontDescriptorSymbolicTraits traits = 0; if (bold) { traits |= UIFontDescriptorSymbolicTraits.Bold; } if (italic) { traits |= UIFontDescriptorSymbolicTraits.Italic; } UIFont font = null; if (NSThread.Current.IsMainThread) { font = MakeFont(familyName.Name, size, traits); } else { NSThread.MainThread.InvokeOnMainThread(() => { font = MakeFont(familyName.Name, size, traits); }); } nativeFont = font.ToCTFont(); }
/// <summary> /// Applies a font style to the current font. /// </summary> /// <param name="font">A font.</param> /// <param name="fontStyle">The font style.</param> /// <returns>A new font with the specified style applied.</returns> public static UIFont ApplyStyle(this UIFont font, FontStyle fontStyle) { if (fontStyle == FontStyle.None) { return(font); } UIFontDescriptorSymbolicTraits traits = UIFontDescriptorSymbolicTraits.ClassUnknown; if ((fontStyle & FontStyle.Bold) == FontStyle.Bold) { traits = UIFontDescriptorSymbolicTraits.Bold; } if ((fontStyle & FontStyle.Italic) == FontStyle.Italic) { traits |= UIFontDescriptorSymbolicTraits.Italic; } UIFontDescriptor fontDescriptor = font.FontDescriptor.CreateWithTraits(traits); if (fontDescriptor == null) { throw new ArgumentException("The font style (" + fontStyle + ") is not supported for font (" + font.Name + ").", nameof(fontStyle)); } return(UIFont.FromDescriptor(fontDescriptor, 0)); }
public static UIFont WithTraits(this UIFont font, UIFontDescriptorSymbolicTraits traits) { // https://stackoverflow.com/questions/18862868/setting-bold-font-on-ios-uilabel/21776838#21776838 var descriptor = font.FontDescriptor.CreateWithTraits(traits); return(UIFont.FromDescriptor(descriptor, 0)); }
public Element MakeMultilineSample(string text, UIFontDescriptorSymbolicTraits trait) { var font = UIFont.FromDescriptor(baseline.CreateWithTraits(trait), 0); return(new StyledMultilineElement(text) { Font = font }); }
internal static UIFont WithTraitsOfFont(this UIFont self, UIFont font) { UIFontDescriptor fontDescriptor = self.FontDescriptor; UIFontDescriptorSymbolicTraits traits = fontDescriptor.SymbolicTraits; traits = traits | font.FontDescriptor.SymbolicTraits; UIFontDescriptor boldFontDescriptor = fontDescriptor.CreateWithTraits(traits); return(UIFont.FromDescriptor(boldFontDescriptor, self.PointSize)); }
internal static UIFont Bold(this UIFont font) { UIFontDescriptor fontDescriptor = font.FontDescriptor; UIFontDescriptorSymbolicTraits traits = fontDescriptor.SymbolicTraits; traits = traits | UIFontDescriptorSymbolicTraits.Bold; UIFontDescriptor boldFontDescriptor = fontDescriptor.CreateWithTraits(traits); return(UIFont.FromDescriptor(boldFontDescriptor, font.PointSize)); }
void UpdateStyleAttributes(UIFontDescriptorSymbolicTraits fontAttr) { var selectionRange = Control.SelectedRange; bool hasFlag = false; AttributedText.EnumerateAttribute(UIStringAttributeKey.Font, selectionRange, NSAttributedStringEnumeration.LongestEffectiveRangeNotRequired, (NSObject value, NSRange range, ref bool stop) => { if (value != null) { var font = (UIFont)value; var descriptor = font.FontDescriptor; if (descriptor.SymbolicTraits.HasFlag(fontAttr)) { hasFlag = true; AttributedText.RemoveAttribute(UIStringAttributeKey.Font, range); var newFont = UIFont.SystemFontOfSize(font.PointSize); if (fontAttr == UIFontDescriptorSymbolicTraits.Bold && descriptor.SymbolicTraits.HasFlag(UIFontDescriptorSymbolicTraits.Italic)) { newFont = UIFont.ItalicSystemFontOfSize(font.PointSize); } else if (fontAttr == UIFontDescriptorSymbolicTraits.Italic && descriptor.SymbolicTraits.HasFlag(UIFontDescriptorSymbolicTraits.Bold)) { newFont = UIFont.BoldSystemFontOfSize(font.PointSize); } AttributedText.AddAttribute(UIStringAttributeKey.Font, newFont, range); CurrentFont = newFont; } } }); if (!hasFlag) { AttributedText.EnumerateAttribute(UIStringAttributeKey.Font, selectionRange, NSAttributedStringEnumeration.None, (NSObject value, NSRange range, ref bool stop) => { if (value != null) { var font = (UIFont)value; var descriptor = font.FontDescriptor; var traits = descriptor.SymbolicTraits; var newTraits = (UIFontDescriptorSymbolicTraits)((uint)fontAttr + (uint)traits); var newDescriptor = descriptor.CreateWithTraits(newTraits); var newFont = UIFont.FromDescriptor(newDescriptor, font.PointSize); AttributedText.RemoveAttribute(UIStringAttributeKey.Font, range); AttributedText.AddAttribute(UIStringAttributeKey.Font, newFont, range); CurrentFont = newFont; } }); } }
public static UIFont ToFont(this Font font) { var nsFont = UIFont.FromName(font.Name, font.Size); if (nsFont == null) { return(null); } if (font.Style == FontStyle.Unknown) { return(nsFont); } UIFontDescriptorSymbolicTraits trait = UIFontDescriptorSymbolicTraits.ClassUnknown; switch (font.Style) { case FontStyle.Bold: trait = UIFontDescriptorSymbolicTraits.Bold; break; case FontStyle.BoldItalic: trait = UIFontDescriptorSymbolicTraits.Bold | UIFontDescriptorSymbolicTraits.Italic; break; case FontStyle.Italic: trait = UIFontDescriptorSymbolicTraits.Italic; break; default: trait = 0; break; } using (var desc = nsFont.FontDescriptor.CreateWithTraits(trait)) { return(UIFont.FromDescriptor(desc, font.Size)); } }
public RadioButton_Mobile() { View = new UIView(); //Main statck layer.BorderWidth = 1.5f; layer.CornerRadius = 10; layer.BorderColor = UIColor.FromRGB(205, 205, 205).CGColor; Layer.AddSublayer(layer); //inner layer innerLayer.BorderWidth = 1.5f; innerLayer.CornerRadius = 10; innerLayer.BorderColor = UIColor.FromRGB(205, 205, 205).CGColor; mainStackView.Layer.AddSublayer(innerLayer); descriptor = new UIFontDescriptor().CreateWithFamily(amntLabl.Font.FamilyName); traits = traits | UIFontDescriptorSymbolicTraits.Bold; descriptor = descriptor.CreateWithTraits(traits); //heading hedinglbl.Text = "Complete your Payment"; hedinglbl.TextColor = UIColor.FromRGB(85, 85, 85); hedinglbl.Font = UIFont.FromDescriptor(descriptor, 20); mainStackView.AddSubview(hedinglbl); //payable payamountlabl.Text = "Total payable amount"; payamountlabl.TextColor = UIColor.Black; mainStackView.AddSubview(payamountlabl); //amount amntLabl.Text = "$120"; amntLabl.TextColor = UIColor.FromRGB(0, 125, 230); amntLabl.Font = UIFont.FromDescriptor(descriptor, 18); mainStackView.AddSubview(amntLabl); //amount selcetPaylbl.Text = "Select your payment mode"; mainStackView.AddSubview(selcetPaylbl); //RadioGroup sfr.Axis = UILayoutConstraintAxis.Vertical; sfr.Spacing = 20; netBanking.SetTitle("Net banking", UIControlState.Normal); netBanking.StateChanged += paymentMode_StateChanged; sfr.AddArrangedSubview(netBanking); debitCard.SetTitle("Debit card", UIControlState.Normal); debitCard.StateChanged += paymentMode_StateChanged; sfr.AddArrangedSubview(debitCard); creditCard.SetTitle("Credit card", UIControlState.Normal); creditCard.StateChanged += paymentMode_StateChanged; sfr.AddArrangedSubview(creditCard); mainStackView.AddSubview(sfr); View.AddSubview(mainStackView); button.SetTitle("Pay Now", UIControlState.Normal); button.SetTitleColor(UIColor.LightGray, UIControlState.Disabled); button.SetTitleColor(UIColor.White, UIControlState.Normal); button.TouchDown += Button_Clicked; button.BackgroundColor = UIColor.FromRGB(0, 125, 230); button.Enabled = false; alertView.Message = "Payment Successfull !"; alertView.AddButton("OK"); View.AddSubview(button); AddSubview(View); }
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 WithTraits(this UIFont font, UIFontDescriptorSymbolicTraits traits) { var descriptor = font.FontDescriptor.CreateWithTraits(traits); return(UIFont.FromDescriptor(descriptor, 0)); }
public virtual UIFontDescriptor FontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits symbolicTraits) { return(default(UIFontDescriptor)); }
public Element MakeMultilineSample (string text, UIFontDescriptorSymbolicTraits trait) { var font = UIFont.FromDescriptor (baseline.CreateWithTraits (trait), 0); return new StyledMultilineElement (text) { Font = font }; }
internal unsafe static XFontSource CreateFontSource(string familyName, FontResolvingOptions fontResolvingOptions, string typefaceKey) { if (string.IsNullOrEmpty(typefaceKey)) { typefaceKey = XGlyphTypeface.ComputeKey(familyName, fontResolvingOptions); } var descriptor = UIFont.FromName(familyName, 20.0f).FontDescriptor; UIFontDescriptorSymbolicTraits traits = 0; if (fontResolvingOptions.IsItalic) { traits |= UIFontDescriptorSymbolicTraits.Italic; } if (fontResolvingOptions.IsBold) { traits |= UIFontDescriptorSymbolicTraits.Bold; } var uifont = UIFont.FromDescriptor(descriptor.CreateWithTraits(traits), 20.0f); var cgFont = CGFont.CreateWithFontName(uifont.Name); IntPtr tags = CGFontCopyTableTags(cgFont.Handle); nint tableCount = CFArrayGetCount(tags); nint totalSize = sizeof(FontHeader) + sizeof(TableEntry) * tableCount; for (int index = 0; index < tableCount; ++index) { nint tableSize = 0; int aTag = (int)CFArrayGetValueAtIndex(tags, index); IntPtr tableDataRef = CGFontCopyTableForTag(cgFont.Handle, aTag); if (tableDataRef != IntPtr.Zero) { tableSize = CFDataGetLength(tableDataRef); CFRelease(tableDataRef); } totalSize += (tableSize + 3) & ~3; } var data = new byte[totalSize]; fixed(byte *dataStart = data) { byte *dataPtr = dataStart; UInt16 entrySelector = 0; UInt16 searchRange = 1; while (searchRange < tableCount >> 1) { entrySelector++; searchRange <<= 1; } searchRange <<= 4; UInt16 rangeShift = (UInt16)((tableCount << 4) - searchRange); FontHeader *offsetTable = (FontHeader *)dataPtr; offsetTable->fVersion = (Int32)SwapBytes((UInt16)1); offsetTable->fNumTables = SwapBytes((UInt16)tableCount); offsetTable->fSearchRange = SwapBytes((UInt16)searchRange); offsetTable->fEntrySelector = SwapBytes((UInt16)entrySelector); offsetTable->fRangeShift = SwapBytes((UInt16)rangeShift); dataPtr += sizeof(FontHeader); TableEntry *entry = (TableEntry *)dataPtr; dataPtr += sizeof(TableEntry) * tableCount; for (int index = 0; index < tableCount; ++index) { int aTag = (int)CFArrayGetValueAtIndex(tags, index); IntPtr tableDataRef = CGFontCopyTableForTag(cgFont.Handle, aTag); nint tableSize = 0; if (tableDataRef != IntPtr.Zero) { tableSize = CFDataGetLength(tableDataRef); Buffer.MemoryCopy((byte *)CFDataGetBytePtr(tableDataRef), dataPtr, tableSize, tableSize); entry->fTag = SwapBytes((UInt32)aTag); entry->fCheckSum = SwapBytes(CalcTableCheckSum((UInt32 *)dataPtr, tableSize)); UInt32 offset = (UInt32)(dataPtr - dataStart); entry->fOffset = SwapBytes((UInt32)offset); entry->fLength = SwapBytes((UInt32)tableSize); CFRelease(tableDataRef); } dataPtr += (tableSize + 3) & ~3; ++entry; } } var fontSource = XFontSource.GetOrCreateFrom(typefaceKey, data); return(fontSource); }
private static UIFont StyleText(UIFont currentFont, UIFontDescriptorSymbolicTraits symbolicTraits) { var descriptor = currentFont.FontDescriptor.CreateWithTraits(symbolicTraits); return(UIFont.FromDescriptor(descriptor, 0)); }