Skip to content

Commit 1b1ab83

Browse files
committed
fix(ios): correctly handle linkTap using UITextView methods
1 parent 86f278f commit 1b1ab83

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

Diff for: src/label.ios.ts

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { VerticalTextAlignment, createNativeAttributedString, verticalTextAlignmentProperty } from '@nativescript-community/text';
2-
import { Color, CoreTypes, Font, FormattedString, View } from '@nativescript/core';
2+
import { Color, CoreTypes, Font, FormattedString, Span, View } from '@nativescript/core';
33
import {
44
borderBottomWidthProperty,
55
borderLeftWidthProperty,
@@ -353,14 +353,40 @@ export class Label extends LabelBase {
353353
}
354354
// _htmlTappable = false;
355355
// _htmlTapGestureRecognizer;
356+
_tappable;
357+
_setTappableState(tappable) {
358+
if (this._tappable !== tappable) {
359+
this._tappable = tappable;
360+
// we dont want the label gesture recognizer for linkTap
361+
// so we override
362+
}
363+
}
356364

357365
textViewShouldInteractWithURLInRangeInteraction?(
358366
textView: UITextView,
359-
URL: NSURL,
367+
url: NSURL,
360368
characterRange: NSRange,
361369
interaction: UITextItemInteraction
362370
) {
363-
this.notify({ eventName: 'linkTap', object: this, link: URL.toString() });
371+
for (let i = 0, spanStart = 0, length = this.formattedText.spans.length; i < length; i++) {
372+
const span = this.formattedText.spans.getItem(i);
373+
const text = span.text;
374+
const textTransform = (this.formattedText.parent as View).textTransform;
375+
let spanText = isNullOrUndefined(text) ? '' : `${text}`;
376+
if (textTransform !== 'none' && textTransform !== 'initial') {
377+
spanText = getTransformedText(spanText, textTransform);
378+
}
379+
380+
spanStart += spanText.length;
381+
if (characterRange.location - 1 <= spanStart && characterRange.location - 1 + characterRange.length > spanStart) {
382+
const span: Span = this.formattedText.spans.getItem(i);
383+
if (span && span.tappable) {
384+
// if the span is found and tappable emit the linkTap event
385+
span.notify({ eventName: Span.linkTapEvent, link: url?.toString() });
386+
}
387+
break;
388+
}
389+
}
364390
return false;
365391
}
366392

@@ -392,7 +418,7 @@ export class Label extends LabelBase {
392418
this.fontSizeRatio
393419
) as NSMutableAttributedString;
394420
let hasLink = false;
395-
result &&
421+
if (result) {
396422
result.enumerateAttributeInRangeOptionsUsingBlock(
397423
NSLinkAttributeName,
398424
{ location: 0, length: result.length },
@@ -404,6 +430,8 @@ export class Label extends LabelBase {
404430
}
405431
}
406432
);
433+
}
434+
407435
this.nativeTextViewProtected.selectable = this.selectable === true || hasLink;
408436

409437
this.attributedString = result;
@@ -603,7 +631,7 @@ export class Label extends LabelBase {
603631
createParagraphStyle();
604632
paragraphStyle.minimumLineHeight = lineHeight;
605633
paragraphStyle.maximumLineHeight = lineHeight;
606-
// } else if (isTextView) {
634+
// } else if (isTextView) {
607635
// createParagraphStyle();
608636
}
609637
const source = getTransformedText(isNullOrUndefined(this.text) ? '' : `${this.text}`, this.textTransform);

0 commit comments

Comments
 (0)