mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: cleanup
This commit is contained in:
@@ -7,9 +7,10 @@ import { TextBaseCommon, textProperty, formattedTextProperty, textAlignmentPrope
|
||||
import { Color } from '../../color';
|
||||
import { FormattedString } from './formatted-string';
|
||||
import { Span } from './span';
|
||||
import { colorProperty, fontInternalProperty, VerticalAlignment } from '../styling/style-properties';
|
||||
import { colorProperty, fontInternalProperty, Length, VerticalAlignment } from '../styling/style-properties';
|
||||
import { isString, isDefined, isNullOrUndefined } from '../../utils/types';
|
||||
import { iOSNativeHelper } from '../../utils';
|
||||
import { Trace } from '../../trace';
|
||||
|
||||
export * from './text-base-common';
|
||||
|
||||
@@ -348,12 +349,10 @@ export class TextBase extends TextBaseCommon {
|
||||
}
|
||||
|
||||
_setShadow(value: TextShadow): void {
|
||||
let layer;
|
||||
|
||||
if (this.nativeTextViewProtected instanceof UITextView) {
|
||||
layer = this.nativeTextViewProtected.layer.sublayers.objectAtIndex(1);
|
||||
} else {
|
||||
layer = this.nativeTextViewProtected.layer;
|
||||
const layer = getShadowLayer(this);
|
||||
if (!layer) {
|
||||
Trace.write('text-shadow not applied, no layer.', Trace.categories.Style, Trace.messageType.info);
|
||||
return;
|
||||
}
|
||||
|
||||
if (isNullOrUndefined(value)) {
|
||||
@@ -365,12 +364,24 @@ export class TextBase extends TextBaseCommon {
|
||||
return;
|
||||
}
|
||||
|
||||
layer.shadowOpacity = 1;
|
||||
layer.shadowRadius = value.blurRadius;
|
||||
layer.shadowColor = value.color.ios.CGColor;
|
||||
layer.shadowOffset = CGSizeMake(value.offsetX, value.offsetY);
|
||||
layer.shouldRasterize = true;
|
||||
if (value.color) {
|
||||
layer.shadowOpacity = value.color.a / 255;
|
||||
layer.shadowColor = value.color.ios.CGColor;
|
||||
}
|
||||
|
||||
if (value.blurRadius) {
|
||||
layer.shadowRadius = Length.toDevicePixels(value.blurRadius);
|
||||
}
|
||||
|
||||
layer.shadowOffset = CGSizeMake(Length.toDevicePixels(value.offsetX), Length.toDevicePixels(value.offsetY));
|
||||
// layer.shadowOffset = CGSizeMake(Length.toDevicePixels(value.offsetX), Length.toDevicePixels(value.offsetY));
|
||||
layer.masksToBounds = false;
|
||||
|
||||
// NOTE: generally should not need shouldRaterize
|
||||
// however for various detailed animation work which involves text-shadow applicable layers, we may want to give users the control of enabling this with text-shadow
|
||||
// if (!(this.nativeTextViewProtected instanceof UITextView)) {
|
||||
// layer.shouldRasterize = true;
|
||||
// }
|
||||
}
|
||||
|
||||
createNSMutableAttributedString(formattedString: FormattedString): NSMutableAttributedString {
|
||||
@@ -493,6 +504,41 @@ export function getTransformedText(text: string, textTransform: TextTransform):
|
||||
}
|
||||
}
|
||||
|
||||
export function getShadowLayer(view: TextBase): CALayer {
|
||||
let layer: CALayer;
|
||||
const name = 'shadow-layer';
|
||||
const nativeView = view && view.nativeTextViewProtected;
|
||||
if (nativeView) {
|
||||
if (nativeView.layer) {
|
||||
if (nativeView.layer.name === name) {
|
||||
return nativeView.layer;
|
||||
} else {
|
||||
if (nativeView.layer.sublayers && nativeView.layer.sublayers.count) {
|
||||
console.log('this.nativeTextViewProtected.layer.sublayers.count:', nativeView.layer.sublayers.count);
|
||||
for (let i = 0; i < nativeView.layer.sublayers.count; i++) {
|
||||
console.log(`layer ${i}:`, nativeView.layer.sublayers.objectAtIndex(i));
|
||||
if (nativeView.layer.sublayers.objectAtIndex(i).name === name) {
|
||||
return nativeView.layer.sublayers.objectAtIndex(i);
|
||||
}
|
||||
}
|
||||
if (nativeView instanceof UITextView) {
|
||||
layer = nativeView.layer.sublayers.objectAtIndex(1);
|
||||
} else {
|
||||
layer = nativeView.layer.sublayers.objectAtIndex(nativeView.layer.sublayers.count - 1);
|
||||
}
|
||||
} else {
|
||||
layer = nativeView.layer;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// could this occur?
|
||||
console.log('no layer!');
|
||||
}
|
||||
}
|
||||
layer.name = name;
|
||||
return layer;
|
||||
}
|
||||
|
||||
function NSStringFromNSAttributedString(source: NSAttributedString | string): NSString {
|
||||
return NSString.stringWithString((source instanceof NSAttributedString && source.string) || <string>source);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user