You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Would be great to support linear, radial and repeating backgrounds.
Reacted by Morten Sjøgren, Hai Ren, Haythem Hedfi, Josh Jensen, Talat CAN, Sinan Ülker, Chintan Parikh, Balogun Oghenerobo Philip, Diego Julião, Jeremiah Ogbomo and 6 more
Here is an Android implementation that seems to work. The iOS code was ripped from somewhere - I don't know if it'll work.
varcoreView=require("ui/core/view"),colorModule=require("color"),Color=colorModule.Color;functionlinearGradient(root,viewId,colors,stops){var_colors=[],_view=coreView.getViewById(root,viewId),nativeView;if(_view){nativeView=_view._nativeView;}else{throwTraceableException("Cannot find view '"+view+"' in page!");}if(!nativeView){return;}colors.forEach(function(c,idx){if(!(cinstanceofColor)){colors[idx]=newColor(c);}});if(this.android){varbackgroundDrawable=nativeView.getBackground(),orientation=android.graphics.drawable.GradientDrawable.Orientation.TOP_BOTTOM,LINEAR_GRADIENT=0;// Get the android version of the colorscolors.forEach(function(c){_colors.push(c.android);});// If it isn't already gradient... make it so.if(!(backgroundDrawableinstanceofandroid.graphics.drawable.GradientDrawable)){backgroundDrawable=newandroid.graphics.drawable.GradientDrawable();backgroundDrawable.setColors(_colors);backgroundDrawable.setGradientType(LINEAR_GRADIENT);nativeView.setBackgroundDrawable(backgroundDrawable);}}elseif(this.ios){/* UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 50.0f)]; CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = view.bounds; gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil]; [view.layer insertSublayer:gradient atIndex:0]; */}}exports.linearGradient=linearGradient;
Then you use it like this:
// Set the background gradientutil.linearGradient(page,"contentArea",['#FFFFFF','#AAAAAA'],[0,1]);
You'd put this in your "pageLoaded" event and pass args.object as the first argument, the id of the view from the XML, then the gradient colors (in hex format) as the third argument. The fourth argument isn't used and can just be [0,1] as above. It was intended to be the color steps, but I never got to that. Oh yes, the XML needs to have the id property defined:
+1 for CSS gradient support. Would make it much easier to create even more polished NativeScript UI.
Even simple support for 2-step linear gradients would be a great start. Multiple steps, radial/repeating could come later. Also agree with @lscown that {N} should adopt the latest "new" CSS gradient syntax.
@bfattori - here is a version of your function with the iOS implementation. I also changed the if-statements, so checking the platform does not depend on this.
var platform = require("platform");
var coreView = require("ui/core/view");
var colorModule = require("color");
var Color = colorModule.Color;
function pageLoaded(args) {
var page = args.object;
linearGradient(page, "contentArea", ['#123456', '#345645']);
}
exports.pageLoaded = pageLoaded;
var coreView = require("ui/core/view"),
colorModule = require("color"),
Color = colorModule.Color;
function linearGradient(root, viewId, colors, stops) {
console.log(platform.device.os);
var _colors = [],
_view = coreView.getViewById(root, viewId),
nativeView;
if (_view) {
nativeView = _view._nativeView;
} else {
throw TraceableException("Cannot find view '" + view + "' in page!");
}
if (!nativeView) {
return;
}
colors.forEach(function(c, idx) {
if (!(c instanceof Color)) {
colors[idx] = new Color(c);
}
});
if (platform.device.os=== platform.platformNames.android) {
console.log("android");
var backgroundDrawable = nativeView.getBackground(),
orientation = android.graphics.drawable.GradientDrawable.Orientation.TOP_BOTTOM,
LINEAR_GRADIENT = 0;
colors.forEach(function(c) {
_colors.push(c.android);
});
if (!(backgroundDrawable instanceof android.graphics.drawable.GradientDrawable)) {
backgroundDrawable = new android.graphics.drawable.GradientDrawable();
backgroundDrawable.setColors(_colors);
backgroundDrawable.setGradientType(LINEAR_GRADIENT);
nativeView.setBackgroundDrawable(backgroundDrawable);
}
} else if (platform.device.os === platform.platformNames.ios) {
console.log("ios");
var view = root.ios.view;
var colorsArray = NSMutableArray.alloc().initWithCapacity(2);
colors.forEach(function(c) {
colorsArray.addObject(interop.types.id(c.ios.CGColor));
});
var gradientLayer = CAGradientLayer.layer();
gradientLayer.colors = colorsArray;
gradientLayer.frame = view.bounds;
view.layer.insertSublayerAtIndex(gradientLayer,0);
}
}
exports.linearGradient = linearGradient;
Thanks @bfattori and @N3ll for your solution. I just put all together into an Angular 2 directive, so it's really easy to use: <StackLayout [gradient]="'#33827D'" [endColor]="'#27758C'"></StackLayout >
Code:
import {Directive, ElementRef, Input, OnInit} from "@angular/core";
import * as application from "application";
import {Color} from "color";
import {Page} from "ui/page";
declare var android: any;
declare var NSMutableArray: any;
declare var CAGradientLayer: any;
declare var interop: any;
@Directive({
selector: "[gradient]"
})
export class GradientDirective implements OnInit {
@Input('gradient')
start: string;
@Input('endColor')
end: string;
constructor(private el: ElementRef, private page: Page) {
}
ngOnInit(): void {
let startColor: Color = new Color(this.start);
let endColor: Color = new Color(this.end);
if (application.android) {
let backgroundDrawable = this.el.nativeElement.android.getBackground();
let orientation = android.graphics.drawable.GradientDrawable.Orientation.TOP_BOTTOM;
let LINEAR_GRADIENT: number = 0;
// If it isn't already gradient... make it so.
if (!(backgroundDrawable instanceof android.graphics.drawable.GradientDrawable)) {
backgroundDrawable = new android.graphics.drawable.GradientDrawable();
backgroundDrawable.setColors([startColor.android, endColor.android]);
backgroundDrawable.setGradientType(LINEAR_GRADIENT);
backgroundDrawable.setOrientation(orientation);
this.el.nativeElement.android.setBackgroundDrawable(backgroundDrawable);
}
} else {
let view = this.page.ios.view;
let colorsArray = NSMutableArray.alloc().initWithCapacity(2);
colorsArray.addObject(interop.types.id(startColor.ios.CGColor));
colorsArray.addObject(interop.types.id(endColor.ios.CGColor));
let gradientLayer = CAGradientLayer.layer();
gradientLayer.colors = colorsArray;
gradientLayer.frame = view.bounds;
view.layer.insertSublayerAtIndex(gradientLayer,0);
}
}
}
@N3ll @csell5 this currently seems to pick the entire page and apply the gradient (atleast on IOS, havent tested on android), is there a way to limit this to the element on which the [gradient] directive has been applied
@NgSculptor The problem with the iOS implementation of the Gradient Background, is that it relies on the dimensions of the view (through the 'bounds' property), which are not available during the OnInit hook. I have also tested with AfterViewInit instead, and it seems too early as well.
That's the reason why @Cselt 's snippet uses the page instead of the view itself.
As a (dirty) workaround, you could do something like this:
This way, you're (almost) sure to catch the moment where the dimensions of the view are available. A cleaner solution would be to get notified when the dimensions of the view get available. Does anyone know if there is a way to do this?
In my app, at first module when app started (android) css background-color could load linear-gradient. However when I navigate to new module (lazy loading) CLI tell me that linear-gradient invalid. Any workaround for this?
p/s: I tried with background-image, it work correctly, but button had lost it's own behaviour when press and hold.
Activity
bfattori commentedon Aug 10, 2015
Here is an Android implementation that seems to work. The iOS code was ripped from somewhere - I don't know if it'll work.
Then you use it like this:
You'd put this in your "pageLoaded" event and pass
args.objectas the first argument, theidof the view from the XML, then the gradient colors (in hex format) as the third argument. The fourth argument isn't used and can just be[0,1]as above. It was intended to be the color steps, but I never got to that. Oh yes, the XML needs to have theidproperty defined:[-]More support for CSS colour values[/-][+]More support for CSS gradients[/+]toddanglin commentedon Nov 14, 2015
+1 for CSS gradient support. Would make it much easier to create even more polished NativeScript UI.
Even simple support for 2-step linear gradients would be a great start. Multiple steps, radial/repeating could come later. Also agree with @lscown that {N} should adopt the latest "new" CSS gradient syntax.
Reference: https://css-tricks.com/css3-gradients/
N3ll commentedon Nov 18, 2015
@bfattori - here is a version of your function with the iOS implementation. I also changed the if-statements, so checking the platform does not depend on this.
bdauria commentedon Aug 8, 2016
And also, don't forget to set the actual orientation to the GradientDrawable:
backgroundDrawable.setOrientation(orientation);Cselt commentedon Nov 5, 2016
Thanks @bfattori and @N3ll for your solution. I just put all together into an Angular 2 directive, so it's really easy to use:
<StackLayout [gradient]="'#33827D'" [endColor]="'#27758C'"></StackLayout >Code:
40x commentedon Nov 16, 2016
@N3ll @csell5 this currently seems to pick the entire page and apply the gradient (atleast on IOS, havent tested on android), is there a way to limit this to the element on which the
[gradient]directive has been appliedghbakhtiari commentedon Nov 18, 2016
+1 for css implementation
bdauria commentedon Nov 25, 2016
@NgSculptor The problem with the iOS implementation of the Gradient Background, is that it relies on the dimensions of the view (through the 'bounds' property), which are not available during the OnInit hook. I have also tested with AfterViewInit instead, and it seems too early as well.
That's the reason why @Cselt 's snippet uses the page instead of the view itself.
As a (dirty) workaround, you could do something like this:
This way, you're (almost) sure to catch the moment where the dimensions of the view are available. A cleaner solution would be to get notified when the dimensions of the view get available. Does anyone know if there is a way to do this?
m-abs commentedon Dec 13, 2016
@bdauria
It's a bit hacky, but I solved it by overriding the
_onSizeChangedfunction onthis.el.nativeElement.Note: I've made a few improvements over the original.
this.el.nativeElement._onSizeChanged, this is private inui/core/viewand only exists oniOS.this.el.nativeElementjust incase the gradient is never applied.m-abs commentedon Dec 22, 2016
@cindy-m
Do you have a code example I could look at?
10 remaining items
toddanglin commentedon Nov 3, 2017
Any update on the priority for getting CSS Gradients in to {N} CSS? This would be a big boost to making it easier to create "better looking" {N} apps.
bhavincb commentedon Jul 10, 2018
background-sizeproperty not working withlinear gradient. any workaround for this?lieutandat commentedon Jan 7, 2019
In my app, at first module when app started (android) css background-color could load linear-gradient. However when I navigate to new module (lazy loading) CLI tell me that linear-gradient invalid. Any workaround for this?
p/s: I tried with background-image, it work correctly, but button had lost it's own behaviour when press and hold.
lock commentedon Jan 7, 2020
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.