Skip to content

Commit

Permalink
feat: selectable property
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Mar 16, 2021
1 parent 114b439 commit a85f4d0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/label-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,6 +104,13 @@ export const linkUnderlineProperty = new CssProperty<Style, boolean>({
});
linkUnderlineProperty.register(Style);

export const selectableProperty = new CssProperty<Style, boolean>({
name: 'selectable',
cssName: 'selectable',
valueConverter: booleanConverter,
});
selectableProperty.register(Style);

export const autoFontSizeProperty = new CssProperty<Style, boolean>({
name: 'autoFontSize',
cssName: 'auto-font-size',
Expand Down
7 changes: 6 additions & 1 deletion src/label.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 11 additions & 4 deletions src/label.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
linkUnderlineProperty,
maxLinesProperty,
needFormattedStringComputation,
selectableProperty,
textShadowProperty,
} from './label-common';

Expand Down Expand Up @@ -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;
Expand All @@ -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<any, any>, 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;
Expand Down Expand Up @@ -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<any, any>;
Expand Down

0 comments on commit a85f4d0

Please sign in to comment.