Beispiel #1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            //TextUtils.ListFontNames ("open");
            _parsedStylesOne = CssTextStyleParser.Parse(File.ReadAllText("StyleOne.css"));
            _parsedStylesTwo = CssTextStyleParser.Parse(File.ReadAllText("StyleTwo.css"));
            TextStyle.Main.SetStyles(_parsedStylesOne);

            // TEMP
            var stopwatch = Stopwatch.StartNew();

            // Create a StyleManager to handle any CSS changes automatically
            _styleManager = new StyleManager(TextStyle.Main);
            _styleManager.Add(labelOne, "h2", headingOne);
            _styleManager.Add(labelTwo, "h1", headingTwo);
            _styleManager.Add(labelThree, "h2", headingThree, new List <CssTag> {
                new CssTag("spot")
                {
                    CSS = "spot{color:" + Colors.SpotColor.ToHex() + "}"
                }
            });
            _styleManager.Add(body, "body", textBody);
            _styleManager.Add(entry, "body", @"hello <i>world</i>", enableHtmlEditing: true);

            body.DataDetectorTypes = UIDataDetectorType.PhoneNumber;

            // Using extension methods
            //body.AttributedText = "Hello world <b>this is a test</b>".ToAttributedString ();

            // TEMP
            //labelOne.Style("h2", headingOne);
            //_styleManager.Add(labelOne, "h2", headingOne);

            Console.WriteLine("Elapsed time {0}", stopwatch.ElapsedMilliseconds);

            var tapper = new UITapGestureRecognizer(() => { View.EndEditing(true); })
            {
                CancelsTouchesInView = false
            };

            View.AddGestureRecognizer(tapper);

            AddUIElements();
        }
Beispiel #2
0
        static Dictionary <string, TextStyleParameters> MergeStyles(string defaultStyleID, List <CssTagStyle> customTags, bool mergeExistingStyles = true, bool includeExistingStyles = true)
        {
            var customCSS = new StringBuilder();

            foreach (var customTag in customTags)
            {
                customCSS.AppendLine(customTag.CSS);
            }

            var customStyles = CssTextStyleParser.Parse(customCSS.ToString());
            var defaultStyle = Instance._textStyles [defaultStyleID];

            if (defaultStyle == null)
            {
                throw new Exception("Default Style ID not found: " + defaultStyleID);
            }

            TextStyleParameters existingStyle;

            foreach (var style in customStyles)
            {
                if (mergeExistingStyles)
                {
                    Instance._textStyles.TryGetValue(style.Key, out existingStyle);

                    if (existingStyle != null)
                    {
                        style.Value.Merge(existingStyle, false);
                    }
                    else
                    {
                        style.Value.Merge(defaultStyle, false);
                    }
                }

                // If no font, use the default one
                if (string.IsNullOrEmpty(style.Value.Font))
                {
                    style.Value.Font = defaultStyle.Font;
                }
            }

            return(customStyles);
        }
Beispiel #3
0
        public void Init()
        {
            textStyle = SimpleIoc.Default.GetInstance <ITextStyle>();

            CSS1 = Assets.LoadString("NativeTextDemo.Resources.StyleOne.css");
            CSS2 = Assets.LoadString("NativeTextDemo.Resources.StyleTwo.css");

            // Pre-parse the style sheets
            _parsedStylesOne = CssTextStyleParser.Parse(CSS1);
            _parsedStylesTwo = CssTextStyleParser.Parse(CSS2);

            textStyle.SetStyles(_parsedStylesOne);
            isCss1 = true;

            CustomTags = new List <CssTag> {
                new CssTag("spot")
                {
                    CSS = "spot{color:" + ColorSwatches.SpotColor.ToHex() + "}"
                }
            };
        }
Beispiel #4
0
        /// <summary>
        /// Sets the CSS string
        /// </summary>
        /// <param name="css">Css Style Sheet</param>
        public virtual void SetCSS(string css)
        {
            var styles = CssTextStyleParser.Parse(css);

            SetStyles(styles);
        }
Beispiel #5
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            this.Window.SetSoftInputMode(Android.Views.SoftInput.AdjustPan);

            // Register Fonts
            TextStyle.Default.AddFont("Archistico-Normal", "Archistico_Simple.ttf");
            TextStyle.Default.AddFont("Avenir-Medium", "Avenir-Medium.ttf");
            TextStyle.Default.AddFont("Avenir-Book", "Avenir-Book.ttf");
            TextStyle.Default.AddFont("Avenir-Heavy", "Avenir-Heavy.ttf");
            TextStyle.Default.AddFont("BreeSerif-Regular", "BreeSerif-Regular.ttf");
            TextStyle.Default.AddFont("OpenSans-CondBold", "OpenSans-CondBold.ttf");
            TextStyle.Default.AddFont("OpenSans-CondLight", "OpenSans-CondLight.ttf");

            // Pre-parse the style sheets
            _parsedStylesOne = CssTextStyleParser.Parse(OpenCSSFile("StyleOne.css"));
            _parsedStylesTwo = CssTextStyleParser.Parse(OpenCSSFile("StyleTwo.css"));
            TextStyle.Default.SetStyles(_parsedStylesOne);

            // Get references to our UI Elements
            var labelOne   = FindViewById <TextView>(Resource.Id.labelOne);
            var labelTwo   = FindViewById <TextView>(Resource.Id.labelTwo);
            var labelThree = FindViewById <TextView>(Resource.Id.labelThree);
            var body       = FindViewById <TextView>(Resource.Id.body);
            var editText   = FindViewById <TextView>(Resource.Id.editText);

            editText.Click += (sender, e) =>
            {
                editText.SetCursorVisible(true);
            };

            // Create a StyleManager to handle any CSS changes automatically
            _styleManager = new StyleManager(TextStyle.Default);
            _styleManager.Add(labelOne, "h2", headingOne);
            _styleManager.Add(labelTwo, "h1", headingTwo);
            _styleManager.Add(labelThree, "h2", headingThree, new List <CssTag> {
                new CssTag("spot")
                {
                    CSS = "spot{color:" + Colors.SpotColor.ToHex() + "}"
                }
            });
            _styleManager.Add(body, "body", textBody);
            _styleManager.Add(editText, "body", editbody, enableHtmlEditing: true);

            // Dismiss keyboard on tap of background
            var layout = (LinearLayout)FindViewById(Resource.Id.layout);

            layout.Touch += (sender, e) =>
            {
                InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
                imm.HideSoftInputFromWindow(editText.WindowToken, 0);
                editText.ClearFocus();
            };

            // Create a toggle button for swapping between styles
            var toggleButton = FindViewById <ImageButton>(Resource.Id.refreshIcon);

            toggleButton.SetBackgroundColor(Color.Transparent);
            toggleButton.Click += (sender, e) =>
            {
                var styles = _isFirstStyleSheet ? _parsedStylesTwo : _parsedStylesOne;
                TextStyle.Default.SetStyles(styles);

                _isFirstStyleSheet = !_isFirstStyleSheet;
            };

            editText.ClearFocus();
        }