Skip to content

Commit

Permalink
feat(auto-fit-text): added (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradmartin authored Sep 10, 2020
1 parent 673a5cf commit 62656a7
Show file tree
Hide file tree
Showing 33 changed files with 347 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ Thumbs.db

*.tgz
packages/**/angular/dist
*.aar
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ npm start
```

- @nativescript/appavailability
- @nativescript/auto-fit-text
- @nativescript/background-http
- @nativescript/brightness
- @nativescript/camera
Expand Down
5 changes: 3 additions & 2 deletions apps/demo-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
"@nativescript/shared-notification-delegate": "file:../../dist/packages/shared-notification-delegate",
"@nativescript/zip": "file:../../dist/packages/zip",
"@nativescript/background-http": "file:../../dist/packages/background-http",
"@nativescript/social-share": "file:../../dist/packages/social-share"
"@nativescript/social-share": "file:../../dist/packages/social-share",
"@nativescript/auto-fit-text": "file:../../dist/packages/auto-fit-text"
},
"devDependencies": {
"@angular/compiler-cli": "file:../../node_modules/@angular/compiler-cli",
"@nativescript/android": "~7.0.0",
"@nativescript/android": "7.0.0",
"@nativescript/ios": "7.0.0",
"@nativescript/webpack": "~3.0.0",
"@ngtools/webpack": "file:../../node_modules/@ngtools/webpack",
Expand Down
1 change: 1 addition & 0 deletions apps/demo-angular/src/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'appavailability', loadChildren: () => import('./plugin-demos/appavailability.module').then((m) => m.AppavailabilityModule) },
{ path: 'auto-fit-text', loadChildren: () => import('./plugin-demos/auto-fit-text.module').then((m) => m.AutoFitTextModule) },
{ path: 'background-http', loadChildren: () => import('./plugin-demos/background-http.module').then((m) => m.BackgroundHttpModule) },
{ path: 'brightness', loadChildren: () => import('./plugin-demos/brightness.module').then((m) => m.BrightnessModule) },
{ path: 'camera', loadChildren: () => import('./plugin-demos/camera.module').then((m) => m.CameraModule) },
Expand Down
3 changes: 3 additions & 0 deletions apps/demo-angular/src/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export class HomeComponent {
{
name: 'appavailability',
},
{
name: 'auto-fit-text',
},
{
name: 'background-http',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ActionBar title="auto-fit-text" class="action-bar"> </ActionBar>
<GridLayout rows="*, *, *, *" columns="*" class="p-20">
<Label row="0" fontSize="36" text="This is a standard NS Label with textWrap enabled." textWrap="true"></Label>
<AutoFitText row="1" text="Lorem Ipsum this is a long string of text that will shrink to fit this line." textWrap="false"></AutoFitText>
<AutoFitText row="2" fontSize="48" text="Lorem Ipsum this line of text with fontSize ignored because the text is so long." textWrap="false"></AutoFitText>
<AutoFitText row="3" text="Another line of AutoFitText" textWrap="false"></AutoFitText>
</GridLayout>
17 changes: 17 additions & 0 deletions apps/demo-angular/src/plugin-demos/auto-fit-text.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component, NgZone } from '@angular/core';
import { DemoSharedAutoFitText } from '@demo/shared';
import { } from '@nativescript/auto-fit-text';

@Component({
selector: 'demo-auto-fit-text',
templateUrl: 'auto-fit-text.component.html',
})
export class AutoFitTextComponent {
demoShared: DemoSharedAutoFitText;

constructor(private _ngZone: NgZone) {}

ngOnInit() {
this.demoShared = new DemoSharedAutoFitText();
}
}
11 changes: 11 additions & 0 deletions apps/demo-angular/src/plugin-demos/auto-fit-text.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
import { NativeScriptCommonModule, NativeScriptRouterModule } from '@nativescript/angular';
import { NativeScriptAutoFitTextModule } from '@nativescript/auto-fit-text/angular';
import { AutoFitTextComponent } from './auto-fit-text.component';

@NgModule({
imports: [NativeScriptCommonModule, NativeScriptAutoFitTextModule, NativeScriptRouterModule.forChild([{ path: '', component: AutoFitTextComponent }])],
declarations: [AutoFitTextComponent],
schemas: [NO_ERRORS_SCHEMA],
})
export class AutoFitTextModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<StackLayout class="p-20">
<ScrollView class="h-full">
<StackLayout>
<Button text="Test background-http" (tap)="demoShared.testIt()" class="btn btn-primary"></Button>
<!-- <Button text="Test background-http" (tap)="demoShared.testIt()" class="btn btn-primary"></Button> -->
</StackLayout>
</ScrollView>
</StackLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<StackLayout class="p-20">
<ScrollView class="h-full">
<StackLayout>
<Button text="Test brightness" (tap)="demoShared.testIt()" class="btn btn-primary"></Button>
<!-- <Button text="Test brightness" (tap)="demoShared.testIt()" class="btn btn-primary"></Button> -->
</StackLayout>
</ScrollView>
</StackLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<StackLayout class="p-20">
<ScrollView class="h-full">
<StackLayout>
<Button text="Test social-share" (tap)="demoShared.testIt()" class="btn btn-primary"></Button>
<!-- <Button text="Test social-share" (tap)="demoShared.testIt()" class="btn btn-primary"></Button> -->
</StackLayout>
</ScrollView>
</StackLayout>
5 changes: 3 additions & 2 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
"@nativescript/shared-notification-delegate": "file:../../packages/shared-notification-delegate",
"@nativescript/zip": "file:../../packages/zip",
"@nativescript/background-http": "file:../../packages/background-http",
"@nativescript/social-share": "file:../../packages/social-share"
"@nativescript/social-share": "file:../../packages/social-share",
"@nativescript/auto-fit-text": "file:../../packages/auto-fit-text"
},
"devDependencies": {
"@nativescript/android": "~7.0.0",
"@nativescript/android": "7.0.0",
"@nativescript/ios": "7.0.0",
"@nativescript/webpack": "3.0.0",
"typescript": "file:../../node_modules/typescript"
Expand Down
5 changes: 3 additions & 2 deletions apps/demo/src/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
<Button text="local-notifications" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
<Button text="shared-notification-delegate" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
<Button text="zip" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
<Button text="background-http" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
<Button text="social-share" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
<Button text="background-http" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
<Button text="social-share" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
<Button text="auto-fit-text" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
</StackLayout>
</ScrollView>
</StackLayout>
Expand Down
10 changes: 10 additions & 0 deletions apps/demo/src/plugin-demos/auto-fit-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Observable, EventData, Page } from '@nativescript/core';
import { DemoSharedAutoFitText } from '@demo/shared';
import {} from '@nativescript/auto-fit-text';

export function navigatingTo(args: EventData) {
const page = <Page>args.object;
page.bindingContext = new DemoModel();
}

export class DemoModel extends DemoSharedAutoFitText {}
13 changes: 13 additions & 0 deletions apps/demo/src/plugin-demos/auto-fit-text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
xmlns:AFT="@nativescript/auto-fit-text" navigatingTo="navigatingTo" class="page">
<Page.actionBar>
<ActionBar title="auto-fit-text" icon="" class="action-bar">
</ActionBar>
</Page.actionBar>
<GridLayout rows="*, *, *, *" columns="*" class="p-20">
<Label row="0" fontSize="36" text="This is a standard NS Label with textWrap enabled." textWrap="true"></Label>
<AFT:AutoFitText row="1" text="Lorem Ipsum this is a long string of text that will shrink to fit this line." textWrap="false"></AFT:AutoFitText>
<AFT:AutoFitText row="2" fontSize="48" text="Lorem Ipsum this line of text with fontSize ignored because the text is so long." textWrap="false"></AFT:AutoFitText>
<AFT:AutoFitText row="3" text="Another line of AutoFitText" textWrap="false"></AFT:AutoFitText>
</GridLayout>
</Page>
3 changes: 3 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
},
"social-share": {
"tags": []
},
"auto-fit-text": {
"tags": []
}
},
"workspaceLayout": {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"license": "MIT",
"scripts": {
"setup": "npx rimraf node_modules package-lock.json dist tmp && npm i && ts-patch install",
"start": "nps",
"add": "nx workspace-schematic add-package"
"start": "nps",
"add": "nx workspace-schematic add-package"
},
"private": true,
"devDependencies": {
Expand Down
50 changes: 50 additions & 0 deletions packages/auto-fit-text/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Nativescript auto-fit-text

```bash
ns plugin add @nativescript/auto-fit-text
```

This plugin is based on the Nativescript Label implementation but with changes to adjust the font size according of the label's width

## Usage

### Nativescript

```xml
<Page
xmlns="http://schemas.nativescript.org/tns.xsd"
loaded="pageLoaded"
class="page"
xmlns:AFT="@nativescript/auto-fit-text"
>
<StackLayout class="p-20">
<AFT:AutoFitText
text="Testinggggggggggggggggg"
textWrap="false"
></AFT:AutoFitText>
</StackLayout>
</Page>
```

### Angular NativeScript

```typescript
import { NativeScriptAutoFitTextModule } from '@nativescript/auto-fit-text/angular';

// Be sure to add the plugin module to your NgModule
@NgModule({
imports: [NativeScriptAutoFitTextModule],
})
```

```xml
<AutoFitText row="2" fontSize="48" text="Lorem Ipsum this line of text with fontSize ignored because the text is so long." textWrap="false"></AutoFitText>
```

## Credits

[@grantland - android-autofittextview](https://github.com/grantland/android-autofittextview)

## License

Apache License Version 2.0, January 2004
8 changes: 8 additions & 0 deletions packages/auto-fit-text/angular/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NgModule } from '@angular/core';
import { registerElement } from '@nativescript/angular';
import { AutoFitText } from '@nativescript/auto-fit-text';

@NgModule()
export class NativeScriptAutoFitTextModule {}

registerElement('AutoFitText', () => AutoFitText);
16 changes: 16 additions & 0 deletions packages/auto-fit-text/angular/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "nativescript-auto-fit-text-angular",
"ngPackage": {
"lib": {
"entryFile": "index.ts",
"umdModuleIds": {
"@nativescript/core": "ns-core",
"@nativescript/angular": "ns-angular",
"@nativescript/auto-fit-text": "ns-auto-fit-text"
}
},
"whitelistedNonPeerDependencies": [
"."
]
}
}
10 changes: 10 additions & 0 deletions packages/auto-fit-text/angular/tsconfig.angular.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../node_modules/ng-packagr/lib/ts/conf/tsconfig.ngc.json",
"compilerOptions": {
"types": ["node"],
"baseUrl": ".",
"paths": {
"@nativescript/auto-fit-text": ["../../../dist/packages/auto-fit-text"]
}
}
}
28 changes: 28 additions & 0 deletions packages/auto-fit-text/index.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Label } from '@nativescript/core';

declare var me: any;

export class AutoFitText extends Label {
public createNativeView(): Object {
const textField: android.widget.TextView = super.createNativeView() as android.widget.TextView;
textField.setMaxLines(1);
textField.setSingleLine();
textField.setMaxHeight(200);
textField.setTextSize(200);
textField.setMinHeight(40);
textField.setTextAlignment(android.widget.TextView.TEXT_ALIGNMENT_CENTER);

me.grantland.widget.AutofitHelper.create(textField);
return textField;
}

initNativeView(): void {
this.nativeView.owner = this;
super.initNativeView();
}

disposeNativeView(): void {
this.nativeView.owner = null;
super.disposeNativeView();
}
}
6 changes: 6 additions & 0 deletions packages/auto-fit-text/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Label } from '@nativescript/core';
export declare class AutoFitText extends Label {
createNativeView(): Object;
initNativeView(): void;
disposeNativeView(): void;
}
28 changes: 28 additions & 0 deletions packages/auto-fit-text/index.ios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Label } from '@nativescript/core';

export class AutoFitText extends Label {
constructor() {
super();
}

initNativeView(): void {
this.nativeView.owner = this;

const label = this.ios as UILabel;
// label.font = label.font.fontWithSize(40);
label.adjustsFontSizeToFitWidth = true;
label.textAlignment = NSTextAlignment.Natural;

super.initNativeView();
}

disposeNativeView(): void {
// Remove reference from native view to this instance.
this.nativeView.owner = null;

// If you want to recycle nativeView and have modified the nativeView
// without using Property or CssProperty (e.g. outside our property system - 'setNative' callbacks)
// you have to reset it to its initial state here.
super.disposeNativeView();
}
}
36 changes: 36 additions & 0 deletions packages/auto-fit-text/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@nativescript/auto-fit-text",
"version": "1.0.0",
"description": "NativeScript plugin for a label that resizes on a single line to fit the screen.",
"main": "index",
"typings": "index.d.ts",
"nativescript": {
"platforms": {
"ios": "7.0.0",
"android": "7.0.0"
}
},
"repository": {
"type": "git",
"url": "https://github.com/NativeScript/plugins.git"
},
"keywords": [
"NativeScript",
"JavaScript",
"TypeScript",
"iOS",
"Android",
"auto-fit-text"
],
"author": {
"name": "NativeScript",
"email": "oss@nativescript.org"
},
"bugs": {
"url": "https://github.com/NativeScript/plugins/issues"
},
"license": "Apache-2.0",
"homepage": "https://github.com/NativeScript/plugins",
"readmeFilename": "README.md",
"bootstrapper": "@nativescript/plugin-seed"
}
9 changes: 9 additions & 0 deletions packages/auto-fit-text/platforms/android/include.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Include.gradle configuration: http://docs.nativescript.org/plugins/plugins#includegradle-specification */

android {

}

dependencies {
compile 'me.grantland:autofittextview:0.2.+'
}
1 change: 1 addition & 0 deletions packages/auto-fit-text/references.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference path="../../references.d.ts" />
9 changes: 9 additions & 0 deletions packages/auto-fit-text/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"rootDir": "."
},
"exclude": ["**/*.spec.ts", "angular"],
"include": ["**/*.ts", "references.d.ts"]
}
Loading

0 comments on commit 62656a7

Please sign in to comment.