mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
stylers removed
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import common = require("./utils-common");
|
||||
import * as traceModule from "trace";
|
||||
import enums = require("ui/enums");
|
||||
|
||||
global.moduleMerge(common, exports);
|
||||
|
||||
@@ -56,6 +57,76 @@ export module layout {
|
||||
// We are using "ad" here to avoid namespace collision with the global android object
|
||||
export module ad {
|
||||
|
||||
export function setTextDecoration(view: android.widget.TextView, value: string) {
|
||||
var flags = 0;
|
||||
|
||||
var values = (value + "").split(" ");
|
||||
|
||||
if (values.indexOf(enums.TextDecoration.underline) !== -1) {
|
||||
flags = flags | android.graphics.Paint.UNDERLINE_TEXT_FLAG;
|
||||
}
|
||||
|
||||
if (values.indexOf(enums.TextDecoration.lineThrough) !== -1) {
|
||||
flags = flags | android.graphics.Paint.STRIKE_THRU_TEXT_FLAG;
|
||||
}
|
||||
|
||||
if (values.indexOf(enums.TextDecoration.none) === -1) {
|
||||
view.setPaintFlags(flags);
|
||||
} else {
|
||||
view.setPaintFlags(0);
|
||||
}
|
||||
}
|
||||
|
||||
export function setTextTransform(view: android.widget.TextView, value: string) {
|
||||
let str = view.getText() + "";
|
||||
let result: string;
|
||||
|
||||
switch (value) {
|
||||
case enums.TextTransform.none:
|
||||
default:
|
||||
result = view["originalString"] || str;
|
||||
if (view["transformationMethod"]) {
|
||||
view.setTransformationMethod(view["transformationMethod"]);
|
||||
}
|
||||
break;
|
||||
case enums.TextTransform.uppercase:
|
||||
view.setTransformationMethod(null);
|
||||
result = str.toUpperCase();
|
||||
break;
|
||||
case enums.TextTransform.lowercase:
|
||||
view.setTransformationMethod(null);
|
||||
result = str.toLowerCase();
|
||||
break;
|
||||
case enums.TextTransform.capitalize:
|
||||
view.setTransformationMethod(null);
|
||||
result = getCapitalizedString(str);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!view["originalString"]) {
|
||||
view["originalString"] = str;
|
||||
view["transformationMethod"] = view.getTransformationMethod();
|
||||
}
|
||||
|
||||
view.setText(result);
|
||||
}
|
||||
|
||||
function getCapitalizedString(str: string): string {
|
||||
var words = str.split(" ");
|
||||
var newWords = [];
|
||||
for (let i = 0; i < words.length; i++) {
|
||||
let word = words[i];
|
||||
newWords.push(word.substr(0, 1).toUpperCase() + word.substring(1));
|
||||
}
|
||||
|
||||
return newWords.join(" ");
|
||||
}
|
||||
|
||||
export function setWhiteSpace(view: android.widget.TextView, value: string) {
|
||||
view.setSingleLine(value === enums.WhiteSpace.nowrap);
|
||||
view.setEllipsize(value === enums.WhiteSpace.nowrap ? android.text.TextUtils.TruncateAt.END : null);
|
||||
}
|
||||
|
||||
export function getApplication() { return <android.app.Application>(<any>com.tns).NativeScriptApplication.getInstance(); }
|
||||
export function getApplicationContext() { return <android.content.Context>getApplication().getApplicationContext(); }
|
||||
|
||||
|
||||
20
utils/utils.d.ts
vendored
20
utils/utils.d.ts
vendored
@@ -68,6 +68,10 @@
|
||||
* Module with android specific utilities.
|
||||
*/
|
||||
module ad {
|
||||
export function setTextTransform(view, value: string);
|
||||
export function setWhiteSpace(view, value: string);
|
||||
export function setTextDecoration(view, value: string);
|
||||
|
||||
/**
|
||||
* Gets the native Android application instance.
|
||||
*/
|
||||
@@ -141,6 +145,22 @@
|
||||
* Module with ios specific utilities.
|
||||
*/
|
||||
module ios {
|
||||
|
||||
export function setWhiteSpace(view, value: string, parentView?: any);
|
||||
export function setTextTransform(view, value: string);
|
||||
export function setTextDecoration(view, value: string);
|
||||
export function setTextAlignment(view, value: string);
|
||||
|
||||
export interface TextUIView {
|
||||
font: any;
|
||||
textAlignment: number;
|
||||
textColor: any;
|
||||
text: string;
|
||||
attributedText: any;
|
||||
lineBreakMode: number;
|
||||
numberOfLines: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility module dealing with some iOS collections.
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import common = require("./utils-common");
|
||||
import dts = require("utils/utils");
|
||||
import common = require("./utils-common");
|
||||
import colorModule = require("color");
|
||||
import enums = require("ui/enums");
|
||||
import * as typesModule from "utils/types";
|
||||
|
||||
global.moduleMerge(common, exports);
|
||||
|
||||
@@ -29,6 +32,155 @@ export module layout {
|
||||
}
|
||||
|
||||
export module ios {
|
||||
|
||||
export function setTextAlignment(view: dts.ios.TextUIView, value: string) {
|
||||
switch (value) {
|
||||
case enums.TextAlignment.left:
|
||||
view.textAlignment = NSTextAlignment.NSTextAlignmentLeft;
|
||||
break;
|
||||
case enums.TextAlignment.center:
|
||||
view.textAlignment = NSTextAlignment.NSTextAlignmentCenter;
|
||||
break;
|
||||
case enums.TextAlignment.right:
|
||||
view.textAlignment = NSTextAlignment.NSTextAlignmentRight;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
export function setTextDecoration(view: dts.ios.TextUIView, value: string) {
|
||||
var attributes: NSMutableDictionary = NSMutableDictionary.alloc().init();
|
||||
var values = (value + "").split(" ");
|
||||
|
||||
if (values.indexOf(enums.TextDecoration.underline) !== -1) {
|
||||
attributes.setObjectForKey(NSUnderlineStyle.NSUnderlineStyleSingle, NSUnderlineStyleAttributeName);
|
||||
}
|
||||
|
||||
if (values.indexOf(enums.TextDecoration.lineThrough) !== -1) {
|
||||
attributes.setObjectForKey(NSUnderlineStyle.NSUnderlineStyleSingle, NSStrikethroughStyleAttributeName);
|
||||
}
|
||||
|
||||
if (values.indexOf(enums.TextDecoration.none) === -1) {
|
||||
setTextDecorationNative(view, view.text || view.attributedText, attributes);
|
||||
} else {
|
||||
setTextDecorationNative(view, view.text || view.attributedText, NSMutableDictionary.alloc().init());
|
||||
}
|
||||
}
|
||||
|
||||
export function setTextTransform(view: dts.ios.TextUIView, value: string) {
|
||||
let str = getNSStringFromView(view);
|
||||
let result: string;
|
||||
|
||||
switch (value) {
|
||||
case enums.TextTransform.none:
|
||||
default:
|
||||
result = view["originalString"] || str;
|
||||
break;
|
||||
case enums.TextTransform.uppercase:
|
||||
result = str.uppercaseString;
|
||||
break;
|
||||
case enums.TextTransform.lowercase:
|
||||
result = str.lowercaseString;
|
||||
break;
|
||||
case enums.TextTransform.capitalize:
|
||||
result = str.capitalizedString;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!view["originalString"]) {
|
||||
view["originalString"] = str;
|
||||
}
|
||||
|
||||
let newStr = getAttributedStringFromView(view, result);
|
||||
|
||||
if (newStr) {
|
||||
setAttributedStringToView(view, newStr);
|
||||
} else {
|
||||
setStringToView(view, result);
|
||||
}
|
||||
}
|
||||
|
||||
function getNSStringFromView(view: any): NSString {
|
||||
let result: string;
|
||||
|
||||
if (view instanceof UIButton) {
|
||||
let attrTitle = (<UIButton>view).titleLabel.attributedText;
|
||||
result = attrTitle ? attrTitle.string : (<UIButton>view).titleLabel.text;
|
||||
}
|
||||
else {
|
||||
let attrText = (<UITextView>view).attributedText;
|
||||
result = attrText ? attrText.string : (<UITextView>view).text;
|
||||
}
|
||||
|
||||
return NSString.alloc().initWithString(result || "");
|
||||
}
|
||||
|
||||
function setStringToView(view: any, str: string) {
|
||||
if (view instanceof UIButton) {
|
||||
(<UIButton>view).setTitleForState(str, UIControlState.UIControlStateNormal);
|
||||
}
|
||||
else {
|
||||
(<dts.ios.TextUIView>view).text = str;
|
||||
}
|
||||
}
|
||||
|
||||
function getAttributedStringFromView(view: any, value: string): NSMutableAttributedString {
|
||||
let result: NSMutableAttributedString;
|
||||
|
||||
if (view instanceof UIButton) {
|
||||
let attrTitle = (<UIButton>view).titleLabel.attributedText;
|
||||
if (attrTitle) {
|
||||
result = NSMutableAttributedString.alloc().initWithAttributedString(attrTitle);
|
||||
}
|
||||
} else if (view.attributedText) {
|
||||
result = NSMutableAttributedString.alloc().initWithAttributedString(view.attributedText);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
result.replaceCharactersInRangeWithString({ location: 0, length: result.length }, value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function setAttributedStringToView(view: any, str: NSMutableAttributedString) {
|
||||
if (view instanceof UIButton) {
|
||||
(<UIButton>view).setAttributedTitleForState(str, UIControlState.UIControlStateNormal);
|
||||
}
|
||||
else {
|
||||
(<dts.ios.TextUIView>view).attributedText = str;
|
||||
}
|
||||
}
|
||||
|
||||
export function setWhiteSpace(view: dts.ios.TextUIView, value: string, parentView?: UIView) {
|
||||
if (value === enums.WhiteSpace.normal) {
|
||||
view.lineBreakMode = NSLineBreakMode.NSLineBreakByWordWrapping;
|
||||
view.numberOfLines = 0;
|
||||
}
|
||||
else {
|
||||
if (parentView) {
|
||||
view.lineBreakMode = NSLineBreakMode.NSLineBreakByTruncatingMiddle;
|
||||
} else {
|
||||
view.lineBreakMode = NSLineBreakMode.NSLineBreakByTruncatingTail;
|
||||
}
|
||||
view.numberOfLines = 1;
|
||||
}
|
||||
}
|
||||
|
||||
function setTextDecorationNative(view: dts.ios.TextUIView, value: string | NSAttributedString, attributes: NSMutableDictionary) {
|
||||
var attributedString: NSMutableAttributedString;
|
||||
|
||||
if (value instanceof NSAttributedString) {
|
||||
attributedString = NSMutableAttributedString.alloc().initWithAttributedString(value);
|
||||
attributedString.addAttributesRange(attributes, NSRangeFromString(attributedString.string));
|
||||
} else {
|
||||
var types: typeof typesModule = require("utils/types");
|
||||
|
||||
view.attributedText = NSAttributedString.alloc().initWithStringAttributes(types.isString(value) ? <string>value : "", attributes);
|
||||
}
|
||||
}
|
||||
|
||||
export module collections {
|
||||
export function jsArrayToNSArray(str: string[]): NSArray {
|
||||
return NSArray.arrayWithArray(<any>str);
|
||||
|
||||
Reference in New Issue
Block a user