diff --git a/src/label-common.ts b/src/label-common.ts index b634b17..0e0a721 100644 --- a/src/label-common.ts +++ b/src/label-common.ts @@ -57,6 +57,7 @@ export abstract class LabelBase extends TNLabel implements LabelViewDefinition { @cssProperty lineBreak: LineBreak; @cssProperty linkColor: Color; @cssProperty linkUnderline: boolean; + @cssProperty selectable: boolean; html: string; //@ts-ignore formattedText: FormattedString; @@ -103,6 +104,13 @@ export const linkUnderlineProperty = new CssProperty({ }); linkUnderlineProperty.register(Style); +export const selectableProperty = new CssProperty({ + name: 'selectable', + cssName: 'selectable', + valueConverter: booleanConverter, +}); +selectableProperty.register(Style); + export const autoFontSizeProperty = new CssProperty({ name: 'autoFontSize', cssName: 'auto-font-size', diff --git a/src/label.android.ts b/src/label.android.ts index a916cdf..348bd84 100644 --- a/src/label.android.ts +++ b/src/label.android.ts @@ -44,7 +44,7 @@ import { import { lineHeightProperty } from '@nativescript/core/ui/text-base/text-base-common'; import { layout } from '@nativescript/core/utils/utils'; import { Label as LabelViewDefinition, LineBreak, TextShadow } from './label'; -import { autoFontSizeProperty, lineBreakProperty, maxLinesProperty, needFormattedStringComputation, textShadowProperty } from './label-common'; +import { autoFontSizeProperty, lineBreakProperty, maxLinesProperty, needFormattedStringComputation, selectableProperty, textShadowProperty } from './label-common'; export { enableIOSDTCoreText, createNativeAttributedString } from '@nativescript-community/text'; @@ -201,6 +201,7 @@ abstract class LabelBase extends View implements LabelViewDefinition { @cssProperty linkColor: Color; @cssProperty linkUnderline: boolean; public html: string; + @cssProperty selectable: boolean; public _isSingleLine: boolean; public text: string; @@ -507,6 +508,10 @@ export class Label extends LabelBase { } } + [selectableProperty.setNative](value: boolean) { + this.nativeTextViewProtected.setTextIsSelectable(value); + } + @profile createHTMLString() { const result = createNativeAttributedString({ text: this.html }) as android.text.SpannableStringBuilder; diff --git a/src/label.ios.ts b/src/label.ios.ts index c211f9d..20d4b62 100644 --- a/src/label.ios.ts +++ b/src/label.ios.ts @@ -36,6 +36,7 @@ import { linkUnderlineProperty, maxLinesProperty, needFormattedStringComputation, + selectableProperty, textShadowProperty, } from './label-common'; @@ -413,7 +414,7 @@ export class Label extends LabelBase { updateHTMLString() { if (!this.html) { - this.nativeTextViewProtected.selectable = false; + this.nativeTextViewProtected.selectable = this.selectable === true; this.attributedString = null; } else { const font = this.nativeViewProtected.font; @@ -434,10 +435,13 @@ export class Label extends LabelBase { // this.nativeTextViewProtected.linkTextAttributes = null; // const color =this.linkColor.ios; let hasLink = false; - result.enumerateAttributeInRangeOptionsUsingBlock(NSLinkAttributeName, { location: 0, length: result.length }, 0, (attributes: NSDictionary, range, stop) => { - hasLink = true; + result.enumerateAttributeInRangeOptionsUsingBlock(NSLinkAttributeName, { location: 0, length: result.length }, 0, (value, range: NSRange, stop) => { + hasLink = hasLink || (!!value && range.length > 0); + if (hasLink) { + stop[0] = true; + } }); - this.nativeTextViewProtected.selectable = hasLink; + this.nativeTextViewProtected.selectable = this.selectable === true || hasLink; // } this.attributedString = result; @@ -472,6 +476,9 @@ export class Label extends LabelBase { } nativeView.linkTextAttributes = attributes; } + [selectableProperty.setNative](value: boolean) { + this.nativeTextViewProtected.selectable = value; + } [linkUnderlineProperty.setNative](value: boolean) { const nativeView = this.nativeTextViewProtected; let attributes = nativeView.linkTextAttributes as NSMutableDictionary;