public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			textStorage = new InteractiveTextColoringTextStorage ();
			CGRect newTextViewRect = View.Bounds;
			newTextViewRect.X += 8;
			newTextViewRect.Width -= 16;

			var layoutManager = new NSLayoutManager ();
			var container = new NSTextContainer (new CGSize (newTextViewRect.Size.Width, float.MaxValue));
			container.WidthTracksTextView = true;

			layoutManager.AddTextContainer (container);
			textStorage.AddLayoutManager (layoutManager);

			var newTextView = new UITextView (newTextViewRect, container);
			newTextView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;
			newTextView.ScrollEnabled  = true;
			newTextView.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag;

			View.Add (newTextView);

			var tokens = new Dictionary<string, NSDictionary> ();
			tokens.Add ("Alice", new NSDictionary (UIStringAttributeKey.ForegroundColor, UIColor.Red));
			tokens.Add ("Rabbit", new NSDictionary (UIStringAttributeKey.ForegroundColor, UIColor.Orange));
			tokens.Add ("DefaultTokenName", new NSDictionary (UIStringAttributeKey.ForegroundColor, UIColor.Black));
			textStorage.Tokens = tokens;

			SetText ();
		}
		public override RectangleF CellFrameForTextContainer (NSTextContainer textContainer, RectangleF lineFrag, PointF position, uint charIndex)
		{
			var rect = base.CellFrameForTextContainer (textContainer, lineFrag, position, charIndex);

			return new RectangleF (
				rect.Location,
				new SizeF (
					rect.Width + padding * 2,
					rect.Height + padding * 2
				)
			);
		}
		public override CGRect CellFrameForTextContainer (NSTextContainer textContainer, CGRect lineFrag, CGPoint position, nuint charIndex)
		{
			var rect = base.CellFrameForTextContainer (textContainer, lineFrag, position, charIndex);

			return new CGRect (
				rect.Location,
				new CGSize (
					rect.Width + padding * 2,
					rect.Height + padding * 2
				)
			);
		}
Exemplo n.º 4
0
        // We can't restore the scoller until layout has completed (because the scroller
        // doesn't know how many lines there are until this happens).
        public void layoutManager_didCompleteLayoutForTextContainer_atEnd(NSLayoutManager layout, NSTextContainer container, bool atEnd)
        {
            if (!m_closed)
            {
                if (m_restorer != null && (m_applier.Applied || m_language == null))
                    if (m_restorer.OnCompletedLayout(layout, atEnd))
                        m_restorer = null;

                if (atEnd)
                {
                    Broadcaster.Invoke("layout completed", m_boss);
                    DoPruneRanges();
                }
            }
        }
Exemplo n.º 5
0
        public static void RecalculateSpanPositions(this NativeLabel control, Label element)
        {
            if (element?.FormattedText?.Spans == null ||
                element.FormattedText.Spans.Count == 0)
            {
                return;
            }

            var finalSize = control.Frame;

            if (finalSize.Width <= 0 || finalSize.Height <= 0)
            {
                return;
            }

#if __MOBILE__
            var inline = control.AttributedText;
#else
            var inline = control.AttributedStringValue;
#endif
            var range = new NSRange(0, inline.Length);

            NSTextStorage textStorage = new NSTextStorage();
            textStorage.SetString(inline);

            var layoutManager = new NSLayoutManager();
            textStorage.AddLayoutManager(layoutManager);

            var textContainer = new NSTextContainer(size: finalSize.Size)
            {
                LineFragmentPadding = 0
            };

            layoutManager.AddTextContainer(textContainer);

            var labelWidth = finalSize.Width;

            var currentLocation = 0;

            for (int i = 0; i < element.FormattedText.Spans.Count; i++)
            {
                var span = element.FormattedText.Spans[i];

                var location = currentLocation;
                var length   = span.Text?.Length ?? 0;

                if (length == 0)
                {
                    continue;
                }

                var startRect = GetCharacterBounds(new NSRange(location, 1), layoutManager, textContainer);
                var endRect   = GetCharacterBounds(new NSRange(location + length, 1), layoutManager, textContainer);

                var startLineHeight = startRect.Bottom - startRect.Top;
                var endLineHeight   = endRect.Bottom - endRect.Top;

                var defaultLineHeight = control.FindDefaultLineHeight(location, length);

                var yaxis       = startRect.Top;
                var lineHeights = new List <double>();
                while (yaxis < endRect.Bottom)
                {
                    double lineHeight;
                    if (yaxis == startRect.Top)                     // First Line
                    {
                        lineHeight = startRect.Bottom - startRect.Top;
                    }
                    else if (yaxis != endRect.Top)                     // Middle Line(s)
                    {
                        lineHeight = defaultLineHeight;
                    }
                    else                     // Bottom Line
                    {
                        lineHeight = endRect.Bottom - endRect.Top;
                    }
                    lineHeights.Add(lineHeight);
                    yaxis += (float)lineHeight;
                }

                ((ISpatialElement)span).Region = Region.FromLines(lineHeights.ToArray(), finalSize.Width, startRect.X, endRect.X, startRect.Top).Inflate(10);

                // update current location
                currentLocation += length;
            }
        }
Exemplo n.º 6
0
        static CGRect GetCharacterBounds(NSRange characterRange, NSLayoutManager layoutManager, NSTextContainer textContainer)
        {
            var glyphRange = new NSRange();

#if __MOBILE__
            layoutManager.CharacterRangeForGlyphRange(characterRange, ref glyphRange);
#else
            layoutManager.CharacterRangeForGlyphRange(characterRange, out glyphRange);
#endif
            return(layoutManager.BoundingRectForGlyphRange(glyphRange, textContainer));
        }
Exemplo n.º 7
0
        public CGRect [] GetRectArray(NSRange glyphRange, NSRange selectedGlyphRange, NSTextContainer textContainer)
        {
            if (textContainer == null)
            {
                throw new ArgumentNullException("textContainer");
            }

            nuint rectCount;
            var   retHandle   = GetRectArray(glyphRange, selectedGlyphRange, textContainer.Handle, out rectCount);
            var   returnArray = new CGRect [rectCount];

            unsafe {
                float *ptr = (float *)retHandle;
                for (nuint i = 0; i < rectCount; ++i)
                {
                    returnArray [i] = new CGRect(ptr [0], ptr [1], ptr [2], ptr [3]);
                    ptr            += 4;
                }
            }
            return(returnArray);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AppKit.TextKit.Formatter.SourceTextView"/> class.
 /// </summary>
 /// <param name="frameRect">Frame rect.</param>
 /// <param name="container">Container.</param>
 public SourceTextView(CGRect frameRect, NSTextContainer container) : base(frameRect, container)
 {
     // Init
     Initialize();
 }
Exemplo n.º 9
0
        public override void DrawRect(RectangleF dirtyRect)
        {
            var height = base.Bounds.Height;
            var calcValue = base.Bounds.Width * (Value * 0.01f);

            var context = NSGraphicsContext.CurrentContext.GraphicsPort;
            context.SetStrokeColor("#999999".ToCGColor());
            context.SetLineWidth (1.0F);

            context.StrokeRect(new RectangleF(0, 0, base.Bounds.Width, height));

            context.SetFillColor("#999999".ToCGColor());
            context.FillRect(new RectangleF(0, 0, base.Bounds.Width, height));

            context.SetFillColor(BackgroundColor.ToCGColor());
            context.FillRect(new RectangleF(0, 0, calcValue, height));

            var attrStr = "{0}".ToFormat(Label).CreateString("#ffffff".ToNSColor(), Font);

            var storage   = new NSTextStorage();
            storage.SetString(attrStr);

            var layout    = new NSLayoutManager();
            var container = new NSTextContainer();

            layout.AddTextContainer(container);
            storage.AddLayoutManager(layout);

            // Get size
            //			var size = layout.GetUsedRectForTextContainer (container).Size;
            //			var width = size.Width / 2.0f;
            var width = (base.Bounds.Width / 2.0f) - (TextOffset.X);

            storage.DrawString(new PointF(width, TextOffset.Y));
        }
Exemplo n.º 10
0
 public CustomUITextView(CGRect frame, NSTextContainer textContainer, string placeholder) : base(frame, textContainer)
 {
     InitializeComponents(placeholder);
 }
Exemplo n.º 11
0
 public CustomUITextView(CGRect frame, NSTextContainer textContainer) : this(frame, textContainer, "")
 {
 }
        private string DetectTappedUrl(UIGestureRecognizer tap, UILabel label, IEnumerable <LinkData> linkList)
        {
            var layoutManager = new NSLayoutManager();
            var textContainer = new NSTextContainer();
            var textStorage   = new NSTextStorage();

            textStorage.SetString(label.AttributedText);

            layoutManager.AddTextContainer(textContainer);
            textStorage.AddLayoutManager(layoutManager);

            textContainer.LineFragmentPadding  = 0;
            textContainer.LineBreakMode        = label.LineBreakMode;
            textContainer.MaximumNumberOfLines = (nuint)label.Lines;
            var labelSize = label.Bounds.Size;

            textContainer.Size = labelSize;

            var locationOfTouchInLabel = tap.LocationInView(label);
            var textBoundingBox        = layoutManager.GetUsedRectForTextContainer(textContainer);
            var textContainerOffset    = new CGPoint(
                (labelSize.Width - textBoundingBox.Size.Width) * 0.0 - textBoundingBox.Location.X,
                (labelSize.Height - textBoundingBox.Size.Height) * 0.0 - textBoundingBox.Location.Y);

            nfloat labelX;

            switch (Element.HorizontalTextAlignment)
            {
            case TextAlignment.End:
                labelX = locationOfTouchInLabel.X - (labelSize.Width - textBoundingBox.Size.Width);
                break;

            case TextAlignment.Center:
                labelX = locationOfTouchInLabel.X - (labelSize.Width - textBoundingBox.Size.Width) / 2;
                break;

            default:
                labelX = locationOfTouchInLabel.X;
                break;
            }

            var locationOfTouchInTextContainer = new CGPoint(labelX - textContainerOffset.X,
                                                             locationOfTouchInLabel.Y - textContainerOffset.Y);

            nfloat partialFraction  = 0;
            var    indexOfCharacter = (nint)layoutManager.CharacterIndexForPoint(locationOfTouchInTextContainer,
                                                                                 textContainer, ref partialFraction);

            nint scaledIndexOfCharacter = 0;

            if (label.Font.Name == "NeoSans-Light")
            {
                scaledIndexOfCharacter = (nint)(indexOfCharacter * 1.13);
            }

            if (label.Font.Name.StartsWith("HelveticaNeue", StringComparison.InvariantCulture))
            {
                scaledIndexOfCharacter = (nint)(indexOfCharacter * 1.02);
            }

            foreach (var link in linkList)
            {
                var rangeLength = link.Range.Length;
                var tolerance   = 0;
                if (label.Font.Name == "NeoSans-Light")
                {
                    rangeLength      = (nint)(rangeLength * 1.13);
                    tolerance        = 25;
                    indexOfCharacter = scaledIndexOfCharacter;
                }

                if (label.Font.Name.StartsWith("HelveticaNeue", StringComparison.InvariantCulture))
                {
                    if (link.Range.Location > 2000)
                    {
                        indexOfCharacter = scaledIndexOfCharacter;
                    }
                }

                // Xamarin version of NSLocationInRange?
                if ((indexOfCharacter >= (link.Range.Location - tolerance)) &&
                    (indexOfCharacter < (link.Range.Location + rangeLength + tolerance)))
                {
                    return(link.Url);
                }
            }

            return(null);
        }
Exemplo n.º 13
0
		/// <summary>
		/// Initializes a new instance of the <see cref="AppKit.TextKit.Formatter.SourceTextView"/> class.
		/// </summary>
		/// <param name="frameRect">Frame rect.</param>
		/// <param name="container">Container.</param>
		public SourceTextView (CGRect frameRect, NSTextContainer container) : base (frameRect, container)
		{
			// Init
			Initialize();
		}
Exemplo n.º 14
0
 public Editor(CoreGraphics.CGRect frame, NSTextContainer container)
     : base(frame, container)
 {
 }
Exemplo n.º 15
0
        static CGRect GetCharacterBounds(NSRange characterRange, NSLayoutManager layoutManager, NSTextContainer textContainer)
        {
            var glyphRange = new NSRange();

#if __MOBILE__
            layoutManager.CharacterRangeForGlyphRange(characterRange, ref glyphRange);
#else
#pragma warning disable CS0618 // Type or member is obsolete
            layoutManager.CharacterRangeForGlyphRange(characterRange, out glyphRange);
#pragma warning restore CS0618 // Type or member is obsolete
#endif
            return(layoutManager.BoundingRectForGlyphRange(glyphRange, textContainer));
        }
 public PlaceholderEnabledUITextView(CGRect frame, NSTextContainer container)
     : base(frame, container)
 {
     this.CommonInit();
 }
Exemplo n.º 17
0
        private string DetectTappedUrl(UIGestureRecognizer tap, UILabel label, IEnumerable <LinkData> linkList)
        {
            // Create instances of NSLayoutManager, NSTextContainer and NSTextStorage
            var layoutManager = new NSLayoutManager();
            var textContainer = new NSTextContainer();
            var textStorage   = new NSTextStorage();

            textStorage.SetString(label.AttributedText);

            // Configure layoutManager and textStorage
            layoutManager.AddTextContainer(textContainer);
            textStorage.AddLayoutManager(layoutManager);

            // Configure textContainer
            textContainer.LineFragmentPadding  = 0;
            textContainer.LineBreakMode        = label.LineBreakMode;
            textContainer.MaximumNumberOfLines = (nuint)label.Lines;
            var labelSize = label.Bounds.Size;

            textContainer.Size = labelSize;

            // Find the tapped character location and compare it to the specified range
            var locationOfTouchInLabel = tap.LocationInView(label);
            var textBoundingBox        = layoutManager.GetUsedRectForTextContainer(textContainer);
            var textContainerOffset    = new CGPoint((labelSize.Width - textBoundingBox.Size.Width) * 0.0 - textBoundingBox.Location.X,
                                                     (labelSize.Height - textBoundingBox.Size.Height) * 0.0 - textBoundingBox.Location.Y);

            nfloat labelX;

            switch (Element.HorizontalTextAlignment)
            {
            case TextAlignment.End:
                labelX = locationOfTouchInLabel.X - (labelSize.Width - textBoundingBox.Size.Width);
                break;

            case TextAlignment.Center:
                labelX = locationOfTouchInLabel.X - (labelSize.Width - textBoundingBox.Size.Width) / 2;
                break;

            default:
                labelX = locationOfTouchInLabel.X;
                break;
            }
            var locationOfTouchInTextContainer = new CGPoint(labelX - textContainerOffset.X, locationOfTouchInLabel.Y - textContainerOffset.Y);

            nfloat partialFraction  = 0;
            var    indexOfCharacter = (nint)layoutManager.CharacterIndexForPoint(locationOfTouchInTextContainer, textContainer, ref partialFraction);

            nint scaledIndexOfCharacter = 0;

            // Problem is that method CharacterIndexForPoint always returns index based on UILabel font
            // ".SFUIText" which is the default Helvetica iOS font
            // HACK is to scale indexOfCharacter for 13% because NeoSans-Light is narrower font than ".SFUIText"
            if (label.Font.Name == "NeoSans-Light")
            {
                scaledIndexOfCharacter = (nint)((double)indexOfCharacter * 1.13);
            }

            // HelveticaNeue font family works perfect until character position in the string is more than 2000 chars
            // some uncosnsistent behaviour
            // if string has <b> tag than label.Font.Name from HelveticaNeue-Thin goes to HelveticaNeue-Bold
            if (label.Font.Name.StartsWith("HelveticaNeue", StringComparison.InvariantCulture))
            {
                scaledIndexOfCharacter = (nint)((double)indexOfCharacter * 1.02);
            }

            foreach (var link in linkList)
            {
                var rangeLength = link.Range.Length;
                var tolerance   = 0;
                if (label.Font.Name == "NeoSans-Light")
                {
                    rangeLength      = (nint)((double)rangeLength * 1.13);
                    tolerance        = 25;
                    indexOfCharacter = scaledIndexOfCharacter;
                }

                if (label.Font.Name.StartsWith("HelveticaNeue", StringComparison.InvariantCulture))
                {
                    if (link.Range.Location > 2000)
                    {
                        indexOfCharacter = scaledIndexOfCharacter;
                    }
                }

                // Xamarin version of NSLocationInRange?
                if ((indexOfCharacter >= (link.Range.Location - tolerance)) && (indexOfCharacter < (link.Range.Location + rangeLength + tolerance)))
                {
                    return(link.Url);
                }
            }
            return(null);
        }
Exemplo n.º 18
0
 public PlaceholderEnabledUITextView(CGRect frame, NSTextContainer container)
     : base(frame, container)
 {
     this.CommonInit();
 }