fix horizontal text-align for Button in iOS, use horizontalContentAlign of the native button

This commit is contained in:
Panayot Cankov
2017-02-21 15:34:47 +02:00
parent 137bb1e0a4
commit 709dff0258
2 changed files with 22 additions and 5 deletions

View File

@@ -1,8 +1,8 @@
import { ControlStateChangeListener } from "ui/core/control-state-change";
import {
ButtonBase, PseudoClassHandler, whiteSpaceProperty,
borderTopWidthProperty, borderRightWidthProperty, borderBottomWidthProperty, borderLeftWidthProperty,
paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, Length, WhiteSpace
borderTopWidthProperty, borderRightWidthProperty, borderBottomWidthProperty, borderLeftWidthProperty, textAlignmentProperty,
paddingTopProperty, paddingRightProperty, paddingBottomProperty, paddingLeftProperty, Length, WhiteSpace, TextAlignment
} from "./button-common";
export * from "./button-common";
@@ -160,6 +160,25 @@ export class Button extends ButtonBase {
let left = this.effectivePaddingLeft + this.effectiveBorderLeftWidth;
this.nativeView.contentEdgeInsets = { top: inset.top, left: left, bottom: inset.bottom, right: inset.right };
}
get [textAlignmentProperty.native](): TextAlignment {
return Button.nativeToJsTextAlignment[this.nativeView.contentHorizontalAlignment];
}
set [textAlignmentProperty.native](value: TextAlignment) {
this.nativeView.contentHorizontalAlignment = Button.jsToNativeTextAlignment[value];
}
private static nativeToJsTextAlignment: { [key: number]: TextAlignment } = {
[UIControlContentHorizontalAlignment.Left]: "left",
[UIControlContentHorizontalAlignment.Center]: "center",
[UIControlContentHorizontalAlignment.Right]: "right",
[UIControlContentHorizontalAlignment.Fill]: "center"
}
private static jsToNativeTextAlignment: { [key in TextAlignment]: UIControlContentHorizontalAlignment } = {
"left": UIControlContentHorizontalAlignment.Left,
"center": UIControlContentHorizontalAlignment.Center,
"right": UIControlContentHorizontalAlignment.Right
}
}
class TapHandlerImpl extends NSObject {

View File

@@ -104,9 +104,7 @@ export class TextBase extends TextBaseCommon {
}
}
set [textAlignmentProperty.native](value: TextAlignment) {
let nativeView = this.nativeView;
nativeView = nativeView instanceof UIButton ? nativeView.titleLabel : nativeView;
// NOTE: if Button textAlignment is not enough - set also btn.contentHorizontalAlignment
let nativeView = <UITextField | UITextView | UILabel>this.nativeView;
switch (value) {
case "left":
nativeView.textAlignment = NSTextAlignment.Left;