Border stroke issue with Custom UITextField - ios

I'm customizing a UITextField to be taller and have different border colors and shadows than the default:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
textField.layer.shadowOpacity = 0.f;
textField.layer.cornerRadius = 6.f;
textField.layer.borderColor = [[UIColor colorWithRed:196/255.0 green:111/255.0 blue:3/255.0 alpha:1] CGColor];
textField.layer.borderWidth = 1.f;
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
textField.layer.shadowColor = [[UIColor blackColor] CGColor];
textField.layer.shadowOpacity = .2f;
textField.layer.shadowOffset = CGSizeMake(0,3);
textField.layer.shadowRadius = 3.f;
textField.clipsToBounds = NO;
textField.layer.cornerRadius = 6.0f;
textField.layer.borderColor = [[UIColor colorWithRed:196/255.0 green:111/255.0 blue:3/255.0 alpha:1] CGColor];
textField.layer.borderWidth = 3.f;
return YES;
}
However, it seems that something is screwing up the rendering of the border in the 'unfocused' state in the bottom left hand:

Related

iOS - View background is black after adding multiple sublayers

I am creating a ring with multiple color segments. Here's my code:
CGFloat viewWidth = rect.size.width;
CGFloat viewHeight = rect.size.height;
CGFloat lineWidth = viewWidth * 0.15f;
CGFloat radius = viewWidth/2 - lineWidth;
CGPoint center = CGPointMake(viewWidth/2, viewHeight/2);
UIColor *lavender = [UIColor colorWithRed:0.5f green:0.0f blue:1.0f alpha:1.0f];
UIColor *purple = [UIColor purpleColor];
UIColor *fuchsia = [UIColor colorWithRed:1.0 green:0.0f blue:0.5f alpha:1.0f];
UIColor *red = [UIColor redColor];
UIColor *orange = [UIColor orangeColor];
UIColor *yellow = [UIColor yellowColor];
UIColor *yellowGreen = [UIColor colorWithRed:0.5f green:1.0f blue:0.0f alpha:1.0f];
UIColor *green = [UIColor greenColor];
UIColor *blueGreen = [UIColor colorWithRed:0.0f green:1.0f blue:0.5f alpha:1.0f];
UIColor *cyan = [UIColor cyanColor];
UIColor *white = [UIColor whiteColor];
UIColor *lightBlue = [UIColor colorWithRed:0.0f green:0.5f blue:1.0f alpha:1.0f];
UIColor *blue = [UIColor blueColor];
NSArray *colors = #[lavender, purple, fuchsia,
red, orange, yellow,
yellowGreen, green, blueGreen,
cyan, white, lightBlue, blue];
for(int i = 0; i < 13; i++)
{
CGFloat startAngle = 0.1538f * M_PI * i;
CGFloat endAngle = 0.1538f * M_PI * (i + 1);
UIColor *strokeColor = [colors objectAtIndex:i];
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
CAShapeLayer *colorPieceLayer = [[CAShapeLayer alloc] init];
[colorPieceLayer setPath:bezierPath.CGPath];
[colorPieceLayer setStrokeColor:strokeColor.CGColor];
[colorPieceLayer setFillColor:[UIColor clearColor].CGColor];
[colorPieceLayer setLineWidth:lineWidth];
[self.layer addSublayer:colorPieceLayer];
}
I tried:
setting the background color of the view self.backgroundColor = [UIColor clearColor];
setting the background color of the layer self.layer.backgroundColor = [UIColor clearColor].CGColor
setting the background color of the sublayers colorPieceLayer.backgroundColor = [UIColor clearColor].CGColor
setting the opacity (But this will make the view disappear).
Here's my screenshot:
Here's my view layers:
So how am I going to make it clear color????
PLEASE TAKE NOTE:
The purple colored is just a UIView that holds my color ring view.
So when I change my view's background color, this color changed.
You could try it.

How to Draw and animate the wifi signal pulse

i want to draw an wifi signal pulse and want to animate with that in launch screen. i tried with this code. the signal is showing in the down words but i need to show in up words. Starting from a point ti increase [like our phone wifi signal]
- (void)viewDidLoad
{
[super viewDidLoad];
[self addWavesToView];
[self addAnimationsToLayers];
}
- (void)addWavesToView
{
CGRect rect = CGRectMake(0.0f, 0.0f, 300.0f, 300.0f);
UIBezierPath *circlePath = [UIBezierPath
bezierPathWithOvalInRect:rect];
for (NSInteger i = 0; i < 10; ++i) {
CAShapeLayer *waveLayer = [CAShapeLayer layer];
waveLayer.bounds = rect;
waveLayer.position = CGPointMake(100, 100);
waveLayer.strokeColor = [[UIColor lightGrayColor] CGColor];
waveLayer.fillColor = [[UIColor clearColor] CGColor];
waveLayer.lineWidth = 5.0f;
waveLayer.path = circlePath.CGPath;
waveLayer.transform = CATransform3DMakeTranslation(0.0f, i*25, 0.0f);
waveLayer.strokeStart = 0.25- ((i+1) * 0.01f);
waveLayer.strokeEnd = 0.25+((i+1) * 0.01f);
[self.view.layer addSublayer:waveLayer];
}
}
- (void)addAnimationsToLayers
{
NSInteger timeOffset = 10;
for (CALayer *layer in self.view.layer.sublayers) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"strokeColor"];
animation.duration = 10.0f;
// Stagger the animations so they don't begin until the
// desired time in each
animation.timeOffset = timeOffset--;
animation.toValue = (id)[[UIColor lightGrayColor] CGColor];
animation.fromValue = (id)[[UIColor darkGrayColor] CGColor];
// Repeat forever
animation.repeatCount = HUGE_VALF;
// Run it at 10x. You can adjust this to taste.
animation.speed = 10;
// Add to the layer to start the animation
[layer addAnimation:animation forKey:#"strokeColor"];
}
"show in up words", it'll take more time so we can rotate base view to 180 degree by this code inside of viewDidLoad() Function
CGFloat degreesOfRotation = 180.0;
self.view.transform = CGAffineTransformRotate(self.view.transform,
degreesOfRotation * M_PI/180.0);

How to change color of CALayer while animating

I have an arc that goes around in a circle and changes colors three times. I used an example on SO to get me started: Change CALayer color while animating
This example works, however the colors are red, then green then blue. I would like them to be Green, Orange, then Red. I'm trying to figure out how in the code he changed colors, and it's quite confusing to me:
for (NSUInteger i = 0; i < 3; i++)
{
CAShapeLayer* strokePart = [[CAShapeLayer alloc] init];
strokePart.fillColor = [[UIColor clearColor] CGColor];
strokePart.frame = tutorialCircle.bounds;
strokePart.path = tutorialCircle.path;
strokePart.lineCap = tutorialCircle.lineCap;
strokePart.lineWidth = tutorialCircle.lineWidth;
// These could come from an array or whatever, this is just easy...
strokePart.strokeColor = [[UIColor colorWithHue: i * portion saturation:1 brightness:1 alpha:1] CGColor];
So I believe this line is doing the color change:
strokePart.strokeColor = [[UIColor colorWithHue: i * portion saturation:1 brightness:1 alpha:1] CGColor];
My goal is to manipulate those colors to Green Orange Red instead of Red Green and Blue.
Thanks
EDIT:
This is the value of portion:
const double portion = 1.0 / ((double)3);
The HUE-Color system defines the color in a an angle of 360 degrees, where 0 is red, 108 (360*0,3) is a green and is a blue. This is how the colors get generated:
strokePart.strokeColor = [[UIColor colorWithHue: 0 saturation:1 brightness:1 alpha:1] CGColor]; // RED
strokePart.strokeColor = [[UIColor colorWithHue: 0.3 saturation:1 brightness:1 alpha:1] CGColor]; // GREEN
strokePart.strokeColor = [[UIColor colorWithHue: 0.6 saturation:1 brightness:1 alpha:1] CGColor]; // BLUE
If you want to change to colors, you could shift the portion by some other value:
strokePart.strokeColor = [[UIColor colorWithHue: 0.3 + portion saturation:1 brightness:1 alpha:1] CGColor];
Or you could define yourself an array, with your colors you want to go through:
NSArray* colors = #[ UIColor.green, UIColor.orange, UIColor.red ];
for (id color in colors)
{
CAShapeLayer* strokePart = [[CAShapeLayer alloc] init];
strokePart.fillColor = [[UIColor clearColor] CGColor];
strokePart.frame = tutorialCircle.bounds;
strokePart.path = tutorialCircle.path;
strokePart.lineCap = tutorialCircle.lineCap;
strokePart.lineWidth = tutorialCircle.lineWidth;
// These could come from an array or whatever, this is just easy...
strokePart.strokeColor = [color CGColor];
Ah I just figured it out. Here is the answer below:
if (i == 0) {
strokePart.strokeColor = [UIColor greenColor].CGColor;
}
else if (i == 1) {
strokePart.strokeColor = [UIColor orangeColor].CGColor;
}
else if (i == 2) {
strokePart.strokeColor = [UIColor redColor].CGColor;
}

How to get shadow in UIButton inside

Using below code for shadow in outside for UIButton. But how can i get shadow for inside the button
button.imageView.layer.cornerRadius = 7.0f;
button.layer.shadowRadius = 3.0f;
button.layer.shadowColor = [UIColor blackColor].CGColor;
button.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
button.layer.shadowOpacity = 0.5f;
button.layer.masksToBounds = NO;
#define kDEFAULT_SHADOW_COLOR [UIColor lightGrayColor]
UIColor *color = [kDEFAULT_SHADOW_COLOR colorWithAlphaComponent:0.3f];
NSArray *colorsArray = #[(id)[color CGColor], (id)[[UIColor clearColor] CGColor]];
CGFloat yOffset = 0.0f;
CGFloat leftHeight = button.bounds.size.height;
CAGradientLayer *shadow;
shadow = [CAGradientLayer layer];
shadow.colors = colorsArray;
shadow.frame = CGRectMake(0, yOffset, 5.0, leftHeight);
shadow.startPoint = CGPointMake(0.0, 0.5);
shadow.endPoint = CGPointMake(1.0, 0.5);
[button.layer insertSublayer:shadow atIndex:0];
You have not implemented the button shadows property. Add the following lines of code and show shadow. The Code is...
self.submitBtn.layer.masksToBounds = YES;
self.submitBtn.layer.clipsToBounds = YES;
self.submitBtn.backgroundColor = [UIColor colorWithRed:(200.0f/255.0f) green:0.0 blue:0.0 alpha:1.0];
self.submitBtn.layer.cornerRadius = 3.0;
self.submitBtn.layer.borderWidth = 2.0;
self.submitBtn.layer.borderColor = [[UIColor clearColor] CGColor];
self.submitBtn.layer.shadowColor = [UIColor colorWithRed:(100.0f/255.0f) green:0.0 blue:0.0 alpha:1.0].CGColor;
self.submitBtn.layer.shadowOpacity = 1.0f;
self.submitBtn.layer.shadowRadius = 1.0f;
self.submitBtn.layer.shadowOffset = CGSizeMake(0, 3);

How to create custom textfield and textview

How to create the textfield has show in below figure
in below figure Address is an custom textview.
txt_Username = [[UITextField alloc] initWithFrame:CGRectMake(0,40,self.view.frame.size.width/2,32)];
txt_Username.borderStyle = UITextBorderStyleRoundedRect;
txt_Username.font = [UIFont systemFontOfSize:15];
txt_Username.placeholder = #"PlacceHolder";
txt_Username.autocorrectionType = UITextAutocorrectionTypeNo;
txt_Username.autocapitalizationType = UITextAutocapitalizationTypeNone;
txt_Username.keyboardType = UIKeyboardTypeDefault;
txt_Username.returnKeyType = UIReturnKeyNext;
txt_Username.clearButtonMode = UITextFieldViewModeWhileEditing;
txt_Username.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[txt_Username setLeftViewMode:UITextFieldViewModeAlways];
txt_Username.leftView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"user"]];
txt_Username.borderStyle = UITextBorderStyleNone;
[txt_Username setBackgroundColor:[UIColor clearColor]];
UIView *demoLine = [[UIView alloc] initWithFrame:CGRectMake(txt_Username.frame.origin.x
, txt_Username.frame.origin.y+txt_Username.frame.size.height
, txt_Username.frame.size.width, 1)];
[demoLine setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:demoLine];
Try this It will helpful.
Change your textfield border style to Transparent one (most left in Attribute Inspector). Use a image for the line in UIImageView. Above this image place your textfield. Or instead of using image for the line, draw the line. Line drawing code is here :
-(void)drawLine:(CGFloat)fromX fromY:(CGFloat)fromY toX:(CGFloat)toX toY:(CGFloat)toY width:(CGFloat)width color:(UIColor *)color
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(fromX, fromY)];
[path addLineToPoint:CGPointMake(toX, toY)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [path CGPath];
shapeLayer.strokeColor = [color CGColor];
shapeLayer.lineWidth = width;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
[self.view.layer addSublayer:shapeLayer];
}
Better if you use avoid line drawing and stick to images.

Resources