Inherit font properties form view inside spans

This commit is contained in:
vakrilov
2015-03-26 16:14:07 +02:00
parent 6841c66de8
commit 9eb66d8a46
6 changed files with 48 additions and 6 deletions

View File

@ -21,6 +21,17 @@ export class FormattedString extends observable.Observable implements definition
private _underline: number; private _underline: number;
private _strikethrough: number; private _strikethrough: number;
private _fontAttributes: number; private _fontAttributes: number;
private _parent: view.View;
get parent(): view.View {
return this._parent;
}
set parent(value: view.View) {
if (this._parent !== value) {
this._parent = value;
}
}
get fontFamily(): string { get fontFamily(): string {
return this._fontFamily; return this._fontFamily;

View File

@ -6,6 +6,7 @@ declare module "text/formatted-string" {
import observable = require("data/observable"); import observable = require("data/observable");
import observableArray = require("data/observable-array"); import observableArray = require("data/observable-array");
import colorModule = require("color"); import colorModule = require("color");
import view = require("ui/core/view");
/** /**
* A class used to create a formatted (rich text) string. * A class used to create a formatted (rich text) string.
@ -66,5 +67,10 @@ declare module "text/formatted-string" {
* @param newBindingContext The value of the newly set binding context. * @param newBindingContext The value of the newly set binding context.
*/ */
public updateSpansBindingContext(newBindingContext: any): void public updateSpansBindingContext(newBindingContext: any): void
/**
* Gets the parent view of the formatted string.
*/
public parent: view.View;
} }
} }

View File

@ -13,18 +13,27 @@ export class Span extends spanCommon.Span {
if (realFontFamily) { if (realFontFamily) {
this.spanModifiers.push(new android.text.style.TypefaceSpan(realFontFamily)); this.spanModifiers.push(new android.text.style.TypefaceSpan(realFontFamily));
} }
var realFontSize = this.fontSize || (parent ? parent.fontSize : undefined); var realFontSize = this.fontSize ||
(parent ? parent.fontSize : undefined) ||
(parent && parent.parent ? parent.parent.style.fontSize : undefined);
if (realFontSize) { if (realFontSize) {
this.spanModifiers.push(new android.text.style.AbsoluteSizeSpan(realFontSize * utils.layout.getDisplayDensity())); this.spanModifiers.push(new android.text.style.AbsoluteSizeSpan(realFontSize * utils.layout.getDisplayDensity()));
} }
var realForegroundColor = this.foregroundColor || (parent ? parent.foregroundColor : undefined);
var realForegroundColor = this.foregroundColor ||
(parent ? parent.foregroundColor : undefined) ||
(parent && parent.parent ? parent.parent.style.color : undefined);
if (realForegroundColor) { if (realForegroundColor) {
this.spanModifiers.push(new android.text.style.ForegroundColorSpan(realForegroundColor.android)); this.spanModifiers.push(new android.text.style.ForegroundColorSpan(realForegroundColor.android));
} }
var realBackgroundColor = this.backgroundColor || (parent ? parent.backgroundColor : undefined);
var realBackgroundColor = this.backgroundColor ||
(parent ? parent.backgroundColor : undefined) ||
(parent && parent.parent ? parent.parent.style.backgroundColor : undefined);
if (realBackgroundColor) { if (realBackgroundColor) {
this.spanModifiers.push(new android.text.style.BackgroundColorSpan(realBackgroundColor.android)); this.spanModifiers.push(new android.text.style.BackgroundColorSpan(realBackgroundColor.android));
} }
var realFontAttributes = this.fontAttributes || (parent ? parent.fontAttributes : undefined); var realFontAttributes = this.fontAttributes || (parent ? parent.fontAttributes : undefined);
if (realFontAttributes) { if (realFontAttributes) {
if ((realFontAttributes & enums.FontAttributes.Bold) && (realFontAttributes & enums.FontAttributes.Italic)) { if ((realFontAttributes & enums.FontAttributes.Bold) && (realFontAttributes & enums.FontAttributes.Italic)) {

View File

@ -9,7 +9,10 @@ export class Span extends spanCommon.Span {
public updateSpanModifiers(parent: formattedString.FormattedString) { public updateSpanModifiers(parent: formattedString.FormattedString) {
super.updateSpanModifiers(parent); super.updateSpanModifiers(parent);
var realFontFamily = this.fontFamily || (parent ? parent.fontFamily : undefined); var realFontFamily = this.fontFamily || (parent ? parent.fontFamily : undefined);
var realFontSize = this.fontSize || (parent ? parent.fontSize : undefined); var realFontSize = this.fontSize ||
(parent ? parent.fontSize : undefined) ||
(parent && parent.parent ? parent.parent.style.fontSize : undefined);
var realFontAttributes = this.fontAttributes || (parent ? parent.fontAttributes : undefined); var realFontAttributes = this.fontAttributes || (parent ? parent.fontAttributes : undefined);
if (realFontAttributes || realFontFamily || realFontSize) { if (realFontAttributes || realFontFamily || realFontSize) {
var font; var font;
@ -37,20 +40,27 @@ export class Span extends spanCommon.Span {
}); });
} }
} }
var realForegroundColor = this.foregroundColor || (parent ? parent.foregroundColor : undefined);
var realForegroundColor = this.foregroundColor ||
(parent ? parent.foregroundColor : undefined) ||
(parent && parent.parent ? parent.parent.style.color : undefined);
if (realForegroundColor) { if (realForegroundColor) {
this.spanModifiers.push({ this.spanModifiers.push({
key: NSForegroundColorAttributeName, key: NSForegroundColorAttributeName,
value: realForegroundColor.ios value: realForegroundColor.ios
}); });
} }
var realBackgroundColor = this.backgroundColor || (parent ? parent.backgroundColor : undefined);
var realBackgroundColor = this.backgroundColor ||
(parent ? parent.backgroundColor : undefined) ||
(parent && parent.parent ? parent.parent.style.backgroundColor : undefined);
if (realBackgroundColor) { if (realBackgroundColor) {
this.spanModifiers.push({ this.spanModifiers.push({
key: NSBackgroundColorAttributeName, key: NSBackgroundColorAttributeName,
value: realBackgroundColor.ios value: realBackgroundColor.ios
}); });
} }
var realUnderline = this.underline || (parent ? parent.underline : undefined); var realUnderline = this.underline || (parent ? parent.underline : undefined);
if (realUnderline) { if (realUnderline) {
this.spanModifiers.push({ this.spanModifiers.push({

View File

@ -110,6 +110,9 @@ export class Button extends view.View implements definition.Button {
} }
public _onFormattedTextPropertyChanged(data: dependencyObservable.PropertyChangeData) { public _onFormattedTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
if (data.newValue) {
(<formattedString.FormattedString>data.newValue).parent = this;
}
this.setFormattedTextPropertyToNative(data.newValue); this.setFormattedTextPropertyToNative(data.newValue);
} }
} }

View File

@ -104,6 +104,9 @@ export class TextBase extends view.View implements definition.TextBase {
} }
public _onFormattedTextPropertyChanged(data: dependencyObservable.PropertyChangeData) { public _onFormattedTextPropertyChanged(data: dependencyObservable.PropertyChangeData) {
if (data.newValue) {
(<formattedString.FormattedString>data.newValue).parent = this;
}
this.setFormattedTextPropertyToNative(data.newValue); this.setFormattedTextPropertyToNative(data.newValue);
} }