Exemple #1
0
        /// <summary>
        /// Creates a new attributed string from the specified styled text and theme.
        /// </summary>
        /// <param name="styledText">The styled text.</param>
        /// <param name="theme">The theme to apply.</param>
        /// <param name="fontSize">The size of the font to use.</param>
        /// <returns>A new attributed string.</returns>
        public static NSMutableAttributedString FromStyledText(StyledText styledText, LabelTheme theme, int fontSize)
        {
            string text = styledText.ToString();
            NSMutableAttributedString attributedText = new NSMutableAttributedString(text);
            NSRange fullRange = new NSRange(0, text.Length);

            foreach (StyledText.TextPart part in styledText.Parts)
            {
                FontStyle fontStyle = FontStyle.None;
                if (part.Style == StyledText.Style.Bold)
                {
                    fontStyle = FontStyle.Bold;
                }

                UIFont font =
                    string.IsNullOrEmpty(theme.FontName)
                    ? UIFont.SystemFontOfSize(fontSize)
                    : UIFont.FromName(theme.FontName, fontSize);

                font = font.ApplyStyle(fontStyle);

                NSRange range = new NSRange(part.StartIndex, part.Text.Length);

                attributedText.AddAttribute(UIStringAttributeKey.Font, font, range);
            }

            attributedText.AddAttribute(UIStringAttributeKey.BackgroundColor, theme.BackgroundColor.ToUIColor(), fullRange);
            attributedText.AddAttribute(UIStringAttributeKey.ForegroundColor, theme.FontColor.ToUIColor(), fullRange);

            return(attributedText);
        }
Exemple #2
0
        private static UIFont GetFont(string fontName, int fontSize, FontStyle fontStyle = FontStyle.None)
        {
            UIFont font =
                string.IsNullOrEmpty(fontName)
                ? UIFont.SystemFontOfSize(fontSize)
                : UIFont.FromName(fontName, fontSize);

            return(font.ApplyStyle(fontStyle));
        }