I have a similar problem to this post (UIButton: Add gradient layer and title is not shown anymore - how to fix?) but his solution did not work for me. First things first, I'm somewhat new to MonoTouch, but have never gone as far as customizing the out of the box controls.
I am trying to add a gradient to my button, but when I do so, the gradient looks exactly as I want, but the text is covered (I set the opacity of my gradient to .5 to test). I have a storyboard in which I have visually designed the interface, but want to tweak a few things.
Here is my code:
var gradient = new CAGradientLayer();
gradient.Frame = getStartedButton.Layer.Bounds;
gradient.Colors = new MonoTouch.CoreGraphics.CGColor[]
{
UIColor.FromRGB (23, 55, 94).CGColor,
UIColor.FromRGB (33, 81, 141).CGColor
};
getStartedButton.Layer.AddSublayer(gradient);
getStartedButton.Layer.CornerRadius = 10;
getStartedButton.Layer.BorderColor = UIColor.White.CGColor;
getStartedButton.Layer.BorderWidth = 1;
getStartedButton.VerticalAlignment = UIControlContentVerticalAlignment.Center;
getStartedButton.Font = UIFont.FromName("Helvetica", 12);
getStartedButton.SetTitleColor(UIColor.White, UIControlState.Normal);
getStartedButton.SetTitle("Get Started", UIControlState.Normal);
Embarrassing, but it was simply a matter of changing the type of button to custom in XCode.
Related
My ImageView is matching screen size on x-axis and is using remaining space on y-axis in my layout. I want to create bitmap into this ImageView with exactly the same size as the ImageView is. How to make it please? Can it be done by some automatic setting, should I call some measure function?
I tried SetAdjustViewBounds() but it didn't work for me.
Creating Bitmap big enough (I don't like much such a memory wasting) and setting SetScaleType(ImageView.ScaleType.Matrix) works, but still when I'm making drawing operations on canvas, I don't know real size of area I should paint into, both canvas and bitmap height are equal to yScreen while imgWeekView height is pretending to be 0, even though it paints whole desired area with gray color.
imgWeekView = new ImageView(context);
//imgWeekView.SetAdjustViewBounds(true);
imgWeekView.SetScaleType(ImageView.ScaleType.Matrix);
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent,1f);
layoutParams.Height = 0;
imgWeekView.LayoutParameters = layoutParams;
Bitmap bitmap = Bitmap.CreateBitmap((int)xScreen, (int)yScreen, Bitmap.Config.Argb8888);
cnvWeekView = new Canvas(bitmap);
imgWeekView.SetImageBitmap(bitmap);
linearLayout.AddView(imgWeekView); //whole activity layout
//Test
cnvWeekView.DrawColor(new Color(128, 128, 128));
Paint paint = new Paint(PaintFlags.AntiAlias);
paint.Color = new Color(255, 255,0);
cnvWeekView.DrawCircle(50, 50, 40, paint);
Finally I found a way how to measure my ImageView and here I will post my answer.
I believed that there should be much easier solution, but maybe there isn't. From this question I took most of the important data:
How to get the width and height of an android.widget.ImageView?
Things look however a little different in my android application and I'm not experienced enough to tell why. I had to change things a little. I had to learn a bit about interfaces and this question helped too.
Implementing the View.IOnTouchListener interface
Here is how I combined things. First I created class that will do the measure.
public class MyPredrawListener : Java.Lang.Object, ViewTreeObserver.IOnPreDrawListener
{
ImageView imageView;
public MyPredrawListener(ImageView img)
{
imageView = img;
}
public bool OnPreDraw()
{
imageView.ViewTreeObserver.RemoveOnPreDrawListener(this);
int finalHeight = imageView.MeasuredHeight;
int finalWidth = imageView.MeasuredWidth;
Bitmap bitmap = Bitmap.CreateBitmap(finalWidth, finalHeight, Bitmap.Config.Argb8888);
imageView.SetImageBitmap(bitmap);
//Test to see result
Canvas cnv = new Canvas(bitmap);
Paint paint = new Paint();
paint.Color = new Color(255, 255, 0);
cnv.DrawColor(new Color(128, 128, 128));
cnv.DrawCircle(finalWidth-50, finalHeight-50, 50, paint);
return true;
}
}
And in code where I create my imageView I set the listener like this.
imgWeekView = new ImageView(context);
MyPredrawListener listener=new MyPredrawListener(imgWeekView);
imgWeekView.ViewTreeObserver.AddOnPreDrawListener(listener);
In OnPreDraw function I put test code to see the result graphically, clearing bitmap to gray color and painting yellow circle to bottom right of a view.
Below is my code I used to create a custom UITextField in code behind.
UITextField usernameField;
usernameField = new UITextField
{
Placeholder = "Enter your username",
BorderStyle = UITextBorderStyle.RoundedRect,
Frame = new RectangleF(10, 32, 50, 30)
};
View.AddSubview(usernameField);
But when I run my app, I dont see this anywhere. Not only this control but all the controls that I create in code behind.
If I drag the controls from toolbox onto my View it's working very fine.
What might be the cause?
Make sure that Container View is not transparent so Alpha = 1 and set background colors for appropriate controlls. Because by default its set to ClearYou can use this method to easly set background and rounded corners like in older versions of iOS
public static void MakeViewRounded(UIView view,float cornerRadius, MonoTouch.CoreGraphics.CGColor backgroundColor)
{
view.Layer.CornerRadius = cornerRadius;
view.Layer.BackgroundColor = backgroundColor;
}
Also make sure that you adding your custom controll to ViewController/View
I have a custom ViewCell with a StackLayout like this:
var viewLayout = new StackLayout () {
Padding = new Thickness (10, 0, 10, 0),
Orientation = StackOrientation.Horizontal,
Children = { nameLabel, button1, button2 }
};
When I use Xamarin.Forms standard ViewCells, i.e "TextCell", the label will get a blueish color on Android. My questions is: Where can I find this property? I want to set it to my custom nameLabel like this:
nameLabel.TextColor = TextCell.StandardTextColorForCurrentPlatform;
For TextCells:-
On Android you can get this value via Color.Accent.
On WindowsPhone use the system-defined PhoneForegroundBrush.
iOS appears to be hard coded to black.
You can write a simple DependencyService to return the appropriate color per the platform your executing on.
I am having problems drawing a terrain with XNA, specifically with colors (It happens with VertexPositionColorNormal and with VertexPositionTextureNormal). My code is following:
public BasicEffect GetEffectForColoredTerrain()
{
this.coloredTerrainEffect.EnableDefaultLighting();
this.coloredTerrainEffect.SpecularPower = 0.01f; //Power of the light
this.coloredTerrainEffect.AmbientLightColor = new Vector3(0.1f, 0.1f, 0.1f); //Color of the light when it reflects on a surface
this.coloredTerrainEffect.EmissiveColor = new Vector3(1, 0, 0);
this.coloredTerrainEffect.DirectionalLight0.Enabled = true; //Enable directional light
this.coloredTerrainEffect.DirectionalLight0.DiffuseColor = (new Vector3(0.2f, 0.2f, 0.2f)); //Diffuse color
this.coloredTerrainEffect.DirectionalLight0.SpecularColor = (new Vector3(0.2f, 0.2f, 0.2f)); //Specular color
this.coloredTerrainEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(1, -1f, 1)); //Direction where the light comes from.
this.coloredTerrainEffect.View = Camera.GetInstance().GetViewMatrix();
this.coloredTerrainEffect.Projection = Camera.GetInstance().GetProjectionMatrix();
this.coloredTerrainEffect.Alpha = (float)((float)Configuration.GetInstance().TerrainOpacity / (float)100);
this.coloredTerrainEffect.VertexColorEnabled = true;
return this.coloredTerrainEffect;
}
And this code for drawing terrain:
RasterizerState rs = new RasterizerState();
rs.CullMode = CullMode.None;
WorldContent.CommonGraphicsDevice.RasterizerState = rs;
//Restore things that SpriteBatch can have overriden
WorldContent.CommonGraphicsDevice.BlendState = BlendState.AlphaBlend;
WorldContent.CommonGraphicsDevice.DepthStencilState = DepthStencilState.Default;
WorldContent.CommonGraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
BasicEffect shader = ShadersHandler.GetInstance().GetEffectForColoredTerrain();
foreach (EffectPass pass in shader.CurrentTechnique.Passes)
{
pass.Apply();
WorldContent.CommonGraphicsDevice.Indices = indexBufferVertices;
WorldContent.CommonGraphicsDevice.SetVertexBuffer(vertexBufferVertices);
WorldContent.CommonGraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexPositionColorNormalList.Length, 0, indicesVertices.Length / 3);
}
However, the result is really weird, as you can see in the following images:
Terrain laterally
Terrain bottom
Color is a gradient from Yellow to white (yellow up, white down). However, if I use the effects file from Riemers tutorial (effects.fx from 3D Series 1), everything is correct, as you can see here:
Terrain good laterally
Terrain good bottom
If you wish, you can see the effect code here: Effects file
So QUESTION: Does anyone knows what is happening here with BasicEffect? I would like to use the Riemers file (everything seems correct with it), but I need to use transparency and the BasicEffect object provides me alpha property, which is perfect for what I am looking for.
PD: The same problem happens with textured terrain, using VertexPositionNormalTexture
Looking at the images, the discoloration appears to be a very nice circle, as from a directional light. I would try removing the DirectionalLight0 property settings you are using in the BasicEffect example and see if that corrects the discoloration.
I want to make speech bubbles but there is a problem adjusting size of the bubbles in UITextView.
is there a way to increase the size of the Textview automatically depending on the length of the
text by using UITextView?
Okay I found a better way of doing what I was suggesting. Rather then deal with the UIView and resizing depending on the text inside it. I just made the UITextView have a rounded edge which now looks like a panel. Perfect. Hope this helps someone!
If your interested in the code
- (void)drawRect:(CGRect)rect
{
CGRect frame = self.frame;
frame.size.height = self.contentSize.height;
self.frame = frame; // Drawing code
self.clipsToBounds = YES;
self.layer.cornerRadius = 10.0f;
}
I think I understand what your getting at. I had this problem the other day. This bit of code will adjust the UITextView contents. Create a new class and call this into it via a new class. If you want to adjust the View well I'm still working on that :-P
Hope this helps
- (void)drawRect:(CGRect)rect
{
CGRect frame = self.frame;
frame.size.height = self.contentSize.height;
self.frame = frame; // Drawing code
}