Skip to content

Commit a85f4d0

Browse files
committed
feat: selectable property
1 parent 114b439 commit a85f4d0

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

Diff for: src/label-common.ts

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export abstract class LabelBase extends TNLabel implements LabelViewDefinition {
5757
@cssProperty lineBreak: LineBreak;
5858
@cssProperty linkColor: Color;
5959
@cssProperty linkUnderline: boolean;
60+
@cssProperty selectable: boolean;
6061
html: string;
6162
//@ts-ignore
6263
formattedText: FormattedString;
@@ -103,6 +104,13 @@ export const linkUnderlineProperty = new CssProperty<Style, boolean>({
103104
});
104105
linkUnderlineProperty.register(Style);
105106

107+
export const selectableProperty = new CssProperty<Style, boolean>({
108+
name: 'selectable',
109+
cssName: 'selectable',
110+
valueConverter: booleanConverter,
111+
});
112+
selectableProperty.register(Style);
113+
106114
export const autoFontSizeProperty = new CssProperty<Style, boolean>({
107115
name: 'autoFontSize',
108116
cssName: 'auto-font-size',

Diff for: src/label.android.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import {
4444
import { lineHeightProperty } from '@nativescript/core/ui/text-base/text-base-common';
4545
import { layout } from '@nativescript/core/utils/utils';
4646
import { Label as LabelViewDefinition, LineBreak, TextShadow } from './label';
47-
import { autoFontSizeProperty, lineBreakProperty, maxLinesProperty, needFormattedStringComputation, textShadowProperty } from './label-common';
47+
import { autoFontSizeProperty, lineBreakProperty, maxLinesProperty, needFormattedStringComputation, selectableProperty, textShadowProperty } from './label-common';
4848

4949
export { enableIOSDTCoreText, createNativeAttributedString } from '@nativescript-community/text';
5050

@@ -201,6 +201,7 @@ abstract class LabelBase extends View implements LabelViewDefinition {
201201
@cssProperty linkColor: Color;
202202
@cssProperty linkUnderline: boolean;
203203
public html: string;
204+
@cssProperty selectable: boolean;
204205

205206
public _isSingleLine: boolean;
206207
public text: string;
@@ -507,6 +508,10 @@ export class Label extends LabelBase {
507508
}
508509
}
509510

511+
[selectableProperty.setNative](value: boolean) {
512+
this.nativeTextViewProtected.setTextIsSelectable(value);
513+
}
514+
510515
@profile
511516
createHTMLString() {
512517
const result = createNativeAttributedString({ text: this.html }) as android.text.SpannableStringBuilder;

Diff for: src/label.ios.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
linkUnderlineProperty,
3737
maxLinesProperty,
3838
needFormattedStringComputation,
39+
selectableProperty,
3940
textShadowProperty,
4041
} from './label-common';
4142

@@ -413,7 +414,7 @@ export class Label extends LabelBase {
413414

414415
updateHTMLString() {
415416
if (!this.html) {
416-
this.nativeTextViewProtected.selectable = false;
417+
this.nativeTextViewProtected.selectable = this.selectable === true;
417418
this.attributedString = null;
418419
} else {
419420
const font = this.nativeViewProtected.font;
@@ -434,10 +435,13 @@ export class Label extends LabelBase {
434435
// this.nativeTextViewProtected.linkTextAttributes = null;
435436
// const color =this.linkColor.ios;
436437
let hasLink = false;
437-
result.enumerateAttributeInRangeOptionsUsingBlock(NSLinkAttributeName, { location: 0, length: result.length }, 0, (attributes: NSDictionary<any, any>, range, stop) => {
438-
hasLink = true;
438+
result.enumerateAttributeInRangeOptionsUsingBlock(NSLinkAttributeName, { location: 0, length: result.length }, 0, (value, range: NSRange, stop) => {
439+
hasLink = hasLink || (!!value && range.length > 0);
440+
if (hasLink) {
441+
stop[0] = true;
442+
}
439443
});
440-
this.nativeTextViewProtected.selectable = hasLink;
444+
this.nativeTextViewProtected.selectable = this.selectable === true || hasLink;
441445
// }
442446

443447
this.attributedString = result;
@@ -472,6 +476,9 @@ export class Label extends LabelBase {
472476
}
473477
nativeView.linkTextAttributes = attributes;
474478
}
479+
[selectableProperty.setNative](value: boolean) {
480+
this.nativeTextViewProtected.selectable = value;
481+
}
475482
[linkUnderlineProperty.setNative](value: boolean) {
476483
const nativeView = this.nativeTextViewProtected;
477484
let attributes = nativeView.linkTextAttributes as NSMutableDictionary<any, any>;

0 commit comments

Comments
 (0)