Skip to content

feat(TextField): support css white-space and text-overflow #10737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/toolbox/src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,10 @@ Button {

.no-shadow {
box-shadow: none;
}

.truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
4 changes: 4 additions & 0 deletions apps/toolbox/src/pages/forms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<TextView hint="Type text..." style.placeholderColor="silver" textChange="{{textChangeArea}}" color="black" width="80%" borderColor="silver" borderWidth="1" height="200" borderRadius="4" fontSize="14">
</TextView>

<Label text="TextField white-space + text-overflow" fontWeight="bold" marginTop="12" />
<TextField text="https://a.very.long.url.that.needs.to.be.truncated.com/" class="truncate" marginTop="6" backgroundColor="#efefef" padding="8" fontSize="18" height="60">
</TextField>

</StackLayout>
</ScrollView>
</Page>
2 changes: 1 addition & 1 deletion packages/core/core-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export namespace CoreTypes {
export const lowercase = 'lowercase';
}

export type WhiteSpaceType = 'normal' | 'nowrap' | CSSWideKeywords;
export type WhiteSpaceType = 'normal' | 'nowrap' | 'wrap' | CSSWideKeywords;
export namespace WhiteSpace {
export const normal = 'normal';
export const nowrap = 'nowrap';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/ui/text-base/text-base-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export const textStrokeProperty = new InheritedCssProperty<Style, string | Strok
});
textStrokeProperty.register(Style);

const whiteSpaceConverter = makeParser<CoreTypes.WhiteSpaceType>(makeValidator<CoreTypes.WhiteSpaceType>('normal', 'nowrap'));
const whiteSpaceConverter = makeParser<CoreTypes.WhiteSpaceType>(makeValidator<CoreTypes.WhiteSpaceType>('normal', 'nowrap', 'wrap'));
export const whiteSpaceProperty = new InheritedCssProperty<Style, CoreTypes.WhiteSpaceType>({
name: 'whiteSpace',
cssName: 'white-space',
Expand Down
9 changes: 0 additions & 9 deletions packages/core/ui/text-field/index.android.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { TextFieldBase, secureProperty } from './text-field-common';
import { whiteSpaceProperty } from '../text-base';
import { keyboardTypeProperty } from '../editable-text-base';
import { CoreTypes } from '../../core-types';

export * from './text-field-common';

Expand Down Expand Up @@ -96,13 +94,6 @@ export class TextField extends TextFieldBase {

this._setInputType(inputType);
}

[whiteSpaceProperty.getDefault](): CoreTypes.WhiteSpaceType {
return 'nowrap';
}
[whiteSpaceProperty.setNative](value: CoreTypes.WhiteSpaceType) {
// Don't change it otherwise TextField will go to multiline mode.
}
}

TextField.prototype._isSingleLine = true;
41 changes: 40 additions & 1 deletion packages/core/ui/text-field/index.ios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TextFieldBase, secureProperty } from './text-field-common';
import { textProperty } from '../text-base';
import { textOverflowProperty, textProperty, whiteSpaceProperty } from '../text-base';
import { hintProperty, placeholderColorProperty, _updateCharactersInRangeReplacementString } from '../editable-text-base';
import { CoreTypes } from '../../core-types';
import { Color } from '../../color';
Expand Down Expand Up @@ -317,4 +317,43 @@ export class TextField extends TextFieldBase {
[paddingLeftProperty.setNative](value: CoreTypes.LengthType) {
// Padding is realized via UITextFieldImpl.textRectForBounds method
}

[whiteSpaceProperty.setNative](value: CoreTypes.WhiteSpaceType) {
this.adjustLineBreak();
}

[textOverflowProperty.setNative](value: CoreTypes.TextOverflowType) {
this.adjustLineBreak();
}

private adjustLineBreak() {
let paragraphStyle: NSMutableParagraphStyle;

switch (this.whiteSpace) {
case 'nowrap':
switch (this.textOverflow) {
case 'clip':
paragraphStyle = NSMutableParagraphStyle.new();
paragraphStyle.lineBreakMode = NSLineBreakMode.ByClipping;
break;
default:
// ellipsis
paragraphStyle = NSMutableParagraphStyle.new();
paragraphStyle.lineBreakMode = NSLineBreakMode.ByTruncatingTail;
break;
}
break;
case 'wrap':
paragraphStyle = NSMutableParagraphStyle.new();
paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping;
break;
}

if (paragraphStyle) {
let attributedString = NSMutableAttributedString.alloc().initWithString(this.nativeViewProtected.text);
attributedString.addAttributeValueRange(NSParagraphStyleAttributeName, paragraphStyle, NSRangeFromString(`{0,${attributedString.length}}`));

this.nativeViewProtected.attributedText = attributedString;
}
}
}
Loading