/// <summary> /// Initialize this instance. /// </summary> private void Initialize() { this.WantsLayer = true; this.LayerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay; // -------------------------------------------------------------------------------- // Handle mouse with Gesture Recognizers. // NOTE: Use either this method or the Override Methods, NOT both! // -------------------------------------------------------------------------------- var click = new NSClickGestureRecognizer (() => { FlipSwitchState(); }); AddGestureRecognizer (click); }
void OnClick(object annotationObject, NSClickGestureRecognizer recognizer) #endif { // https://bugzilla.xamarin.com/show_bug.cgi?id=26416 NSObject annotation = Runtime.GetNSObject(((IMKAnnotation)annotationObject).Handle); if (annotation == null) { return; } // lookup pin Pin targetPin = null; for (var i = 0; i < _map.Pins.Count; i++) { Pin pin = _map.Pins[i]; object target = pin.Id; if (target != annotation) { continue; } targetPin = pin; break; } // pin not found. Must have been activated outside of forms if (targetPin == null) { return; } // if the tap happened on the annotation view itself, skip because this is what happens when the callout is showing // when the callout is already visible the tap comes in on a different view if (_lastTouchedView is MKAnnotationView) { return; } targetPin.SendTap(); }
private void CreateAttributedString(NSTextField control, string html) { var attr = new NSAttributedString(html); var links = new List <LinkData>(); control.AttributedStringValue = attr; // Sets up a Gesture recognizer: if (links.Count <= 0) { return; } var tapGesture = new NSClickGestureRecognizer((tap) => { var url = DetectTappedUrl(tap, (NSTextField)tap.View, links); if (url == null) { return; } var label = (HtmlLabel)Element; var args = new WebNavigatingEventArgs(WebNavigationEvent.NewPage, new UrlWebViewSource { Url = url }, url); label.SendNavigating(args); if (args.Cancel) { return; } Device.OpenUri(new Uri(url)); label.SendNavigated(args); }); control.AddGestureRecognizer(tapGesture); }
public TabButton(IHostResourceProvider hostResource, string imageName) { if (hostResource == null) { throw new ArgumentNullException(nameof(hostResource)); } if (imageName == null) { throw new ArgumentNullException(nameof(imageName)); } Bordered = false; Action = new ObjCRuntime.Selector(ClickedName); var clicked = new NSClickGestureRecognizer(OnClicked); AddGestureRecognizer(clicked); this.hostResource = hostResource; this.imageName = imageName; ViewDidChangeEffectiveAppearance(); }
private string DetectTappedUrl(NSClickGestureRecognizer tap, NSTextField label, IEnumerable <LinkData> linkList) { // Creates instances of NSLayoutManager, NSTextContainer and NSTextStorage var layoutManager = new NSLayoutManager(); var textContainer = new NSTextContainer(); var textStorage = new NSTextStorage(); textStorage.SetString(label.AttributedStringValue); // Configures layoutManager and textStorage layoutManager.AddTextContainer(textContainer); textStorage.AddLayoutManager(layoutManager); // Configures textContainer textContainer.LineFragmentPadding = 0; textContainer.LineBreakMode = label.LineBreakMode; textContainer.MaximumNumberOfLines = (nuint)label.MaximumNumberOfLines; var labelSize = label.Bounds.Size; textContainer.Size = labelSize; // Finds 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.FontName == "NeoSans-Light") { scaledIndexOfCharacter = (nint)(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.FontName.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.FontName == "NeoSans-Light") { rangeLength = (nint)(rangeLength * 1.13); tolerance = 25; indexOfCharacter = scaledIndexOfCharacter; } if (label.Font.FontName.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); }
private void OnBackgroundTap(NSClickGestureRecognizer e) { CurrentElement.SendBackgroundClick(); }