Skip to content

Commit

Permalink
feat(android): text alignment justify (#12464)
Browse files Browse the repository at this point in the history
Fixes TIMOB-28348
  • Loading branch information
m1ga committed Feb 16, 2021
1 parent 6762beb commit 851b4f6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ public class UIModule extends KrollModule
@Kroll.constant
public static final String TEXT_ALIGNMENT_RIGHT = "right";
@Kroll.constant
public static final String TEXT_ALIGNMENT_JUSTIFY = "justify";
@Kroll.constant
public static final String TEXT_VERTICAL_ALIGNMENT_BOTTOM = "bottom";
@Kroll.constant
public static final String TEXT_VERTICAL_ALIGNMENT_CENTER = "middle";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@
import android.graphics.drawable.StateListDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.text.Spanned;
import android.text.Layout;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.DisplayMetrics;
Expand Down Expand Up @@ -481,12 +483,20 @@ public static void setAlignment(TextView tv, String textAlign, String verticalAl
int gravity = Gravity.NO_GRAVITY;

if (textAlign != null) {

if (!"justify".equals(textAlign) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// reset justification
tv.setJustificationMode(Layout.JUSTIFICATION_MODE_NONE);
}

if ("left".equals(textAlign)) {
gravity |= Gravity.LEFT;
} else if ("center".equals(textAlign)) {
gravity |= Gravity.CENTER_HORIZONTAL;
} else if ("right".equals(textAlign)) {
gravity |= Gravity.RIGHT;
} else if ("justify".equals(textAlign) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
tv.setJustificationMode(Layout.JUSTIFICATION_MODE_INTER_WORD);
} else {
Log.w(TAG, "Unsupported horizontal alignment: " + textAlign);
}
Expand Down
13 changes: 7 additions & 6 deletions apidoc/Titanium/UI/UI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ description: |
fullscreen or modal and can be customized, such as changing their opacity or background color.
Windows themselves are views and also inherit a views properties, functions and events. There
are a few specialization of Windows such as a [Tab Group](Titanium.UI.TabGroup) which offer
additional behavior beyond the basic Window. It is considered a best practice to use a
additional behavior beyond the basic Window. It is considered a best practice to use a
[NavigationWindow](Titanium.UI.NavigationWindow) as the root of your application.
Titanium uses the [Factory Pattern](http://en.wikipedia.org/wiki/Factory_method_pattern) for
Expand All @@ -53,7 +53,7 @@ description: |
#### Global Context
Prior to the release of Titanium SDK `9.0.0.GA` any variable declared in `app.js` or `alloy.js`
Prior to the release of Titanium SDK `9.0.0.GA` any variable declared in `app.js` or `alloy.js`
was added to a global scope. This however is no longer the case since `9.0.0.GA`. However
it is still possible to add variables to a global scope by adding `global.` in front of any
variabled declared in `app.js` or `alloy.js`. However you should be careful with adding variables
Expand Down Expand Up @@ -578,7 +578,7 @@ properties:
- name: ATTRIBUTE_LINE_BREAK
deprecated:
since: "7.5.0"
notes: Use [ParagraphAttribute.lineBreakMode](ParagraphAttribute.lineBreakMode) instead.
notes: Use [ParagraphAttribute.lineBreakMode](ParagraphAttribute.lineBreakMode) instead.
summary: |
Use with <Attribute.type> to wrap and truncate the text.
description: |
Expand Down Expand Up @@ -2357,11 +2357,12 @@ properties:
Use with the <Titanium.UI.TextField.textAlign>, <Titanium.UI.Button.textAlign> and
<Titanium.UI.TextArea.textAlign> properties.
This constant is a number and only available on iOS.
This constant is a number on iOS and a string on Android.
type: [Number, String]
permission: read-only
platforms: [iphone, ipad, macos]
since: "6.1.0"
platforms: [android, iphone, ipad, macos]
osver: {android: {min: "8.0"}}
since: { android: "10.0.0", iphone: "6.1.0", ipad: "6.1.0" }

- name: TEXT_ALIGNMENT_LEFT
summary: Left align text.
Expand Down
6 changes: 2 additions & 4 deletions tests/Resources/ti.ui.label.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,8 @@ describe('Titanium.UI.Label', function () {
should(label.textAlign).eql(Ti.UI.TEXT_ALIGNMENT_RIGHT);

// TIMOB-3408
if (utilities.isIOS()) {
label.textAlign = Ti.UI.TEXT_ALIGNMENT_JUSTIFY;
should(label.textAlign).eql(Ti.UI.TEXT_ALIGNMENT_JUSTIFY);
}
label.textAlign = Ti.UI.TEXT_ALIGNMENT_JUSTIFY;
should(label.textAlign).eql(Ti.UI.TEXT_ALIGNMENT_JUSTIFY);
});

it('has no accessors', () => {
Expand Down

0 comments on commit 851b4f6

Please sign in to comment.