Customize an existing button in Objective C - ios

I'd like to be able to customize any buttons I'll create within a specific style of my app. I was trying to create a new class which will consist of all my settings, so every time I add a new button I can use methods of the class to customize the new button.
I'm a very new to the iOS programming so I quess I might've been doing something wrong, but that's what I've got so far:
I created the class with settings
#import "CustomButton.h"
#implementation CustomButton
-(UIButton *) setButtonWithType{
self.layer.cornerRadius = 25;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(1.5f, 1.5f);
self.layer.shadowOpacity = 1.0f;
self.layer.shadowRadius = 0.0f;
self.layer.masksToBounds = NO;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.startPoint = CGPointMake(0.5, 0.5);
gradient.endPoint = CGPointMake(0.0, 0.5);
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,(id)[UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0].CGColor, nil];
gradient.cornerRadius = self.layer.cornerRadius;
[self.layer insertSublayer:gradient atIndex:0];
return self;
}
#end
ViewController.h
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *button;
#end
ViewController.m
#import "ViewController.h"
#import "CustomButton.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CustomButton *btn = [[CustomButton alloc] init];
self.button = [btn setButtonWithType];
//rest of the code
Any ideas how to make it works?
Or do I need to use a very different approach?
Thank you!!

You may find success doing this as a Category (lots of examples out there)...
UIButton+ShadowRoundedGradient.h
#import <UIKit/UIKit.h>
#interface UIButton (ShadowRoundedGradient)
- (void) makeMe;
#end
UIButton+ShadowRoundedGradient.m
#import "UIButton+ShadowRoundedGradient.h"
#implementation UIButton (ShadowRoundedGradient)
- (void) makeMe {
self.layer.cornerRadius = 25;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(1.5f, 1.5f);
self.layer.shadowOpacity = 1.0f;
self.layer.shadowRadius = 0.0f;
self.layer.masksToBounds = NO;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.startPoint = CGPointMake(0.5, 0.5);
gradient.endPoint = CGPointMake(0.0, 0.5);
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,(id)[UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0].CGColor, nil];
gradient.cornerRadius = self.layer.cornerRadius;
[self.layer insertSublayer:gradient atIndex:0];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
#property (weak, nonatomic) IBOutlet UIButton *button;
}
#end
ViewController.m
#import "ViewController.h"
#import "UIButton+ShadowRoundedGradient"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.button makeMe];
//rest of the code
}

Related

How to create a custom UISlider

Hi How can i create a custom UISlider like the image below.
I googled a lot still couldn't able to find a way to do that. Can anybody suggest me how to achieve this. Any help would be greatly appreciated.
Custom UISlider
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController :UIViewController
{
}
#property (strong, nonatomic) IBOutlet UILabel *LineLbl;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollviewObj;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
{
}
#end
#implementation ViewController
#synthesize LineLbl;
#synthesize scrollviewObj;
- (void)viewDidLoad
{
[super viewDidLoad];
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(10,LineLbl.frame.origin.y + LineLbl.frame.size.height +20, self.view.frame.size.width -20, 20)];
slider.maximumValue = 1;
slider.value = 0.6;
[slider setMinimumTrackTintColor:[UIColor clearColor]];
[slider setMaximumTrackTintColor:[UIColor clearColor]];
[slider setThumbImage:[UIImage imageNamed:#"sliderImg"] forState:UIControlStateNormal];
CAGradientLayer *red_gradient =[CAGradientLayer layer];
UIColor *startColor=[UIColor yellowColor];
UIColor *midColor=[UIColor colorWithRed:(155/255.f) green:(250/255.f) blue:(90/255.f) alpha:1];
UIColor *endColor= [UIColor colorWithRed:(96/255.f) green:(255/255.f) blue:(59/255.f) alpha:1];
red_gradient.frame = slider.bounds;
red_gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[midColor CGColor],(id)[endColor CGColor], nil];
[red_gradient setStartPoint:CGPointMake(0.0, 0.5)];
[red_gradient setEndPoint:CGPointMake(1.0, 0.5)];
[slider.layer insertSublayer:red_gradient atIndex:2];
[scrollviewObj addSubview: slider];
}
I followed this.

how to create bottom line border in multiple textfield

I'm just beginner in Objective C.
I want to create multiple textfield with only bottom line border.
for this i crete a new class customTextField
look at my code;-
CustomTextField.m
- (id)init {
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, self.frame.size.height);
border.borderWidth = borderWidth;
[self.layer addSublayer:border];
self.layer.masksToBounds = YES;
return self;
}
in my loginViewController.h
#import <UIKit/UIKit.h>
#import "CustomTextField.h"
#interface LoginViewController : UIViewController
#property (strong, nonatomic) IBOutlet CustomTextField *txtFirstName;
#property (strong, nonatomic) IBOutlet CustomTextField *txtLastName;
#property (strong, nonatomic) IBOutlet CustomTextField *txtEmail;
#property (strong, nonatomic) IBOutlet CustomTextField *txtPassword;
#end
loginViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
_txtFirstName = [[CustomTextField alloc]init];
_txtLastName = [[CustomTextField alloc]init];
_txtEmail = [[CustomTextField alloc]init];
_txtPassword = [[CustomTextField alloc]init];
}
I don't know where I do mistakes. this code is doing nothing. Can any one tell me where I do wrong ?
any help or any suggestion.
Try with this:
self.layer.masksToBounds = YES;
self.layer.borderColor = [UIColor grayColor].CGColor;
self.layer.borderWidth = borderWidth;
But you're using IBOulet, which means your objects are initialised from the storyboard, so then you should move your code to awakeFromNib
- (void)awakeFromNib
{
self.layer.masksToBounds = YES;
self.layer.borderColor = [UIColor grayColor].CGColor;
self.layer.borderWidth = borderWidth;
}
Add a private border layer property in your .m file
#property (strong, nonatomic) IBOutlet CALayer *borderLayer;
Override layoutSubviews to set up the border.
- (void)layoutSubViews {
[super layoutSubViews];
if (self.borderLayer) {
self.borderLayer = [CALayer layer];
[self.layer addSublayer:border];
}
CGFloat borderWidth = 5.0f;
self.borderLayer.backgroundColor = color.CGColor;
self.borderLayer.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth);
}
Update:
Don't initialize the CustomTextField IBOutlets in viewDidLoad. Simply set their class to CustomTextField in the storyboard!
you are using subclass with storyboard your code will fine if you create all button programatically but if you want that your code will work fine with story board and programatically then you have to use this code in your CustomTextField.m
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
[self initialize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
// Initialization code
}
return self;
}
- (void) awakeFromNib
{
[super awakeFromNib];
}
- (void) layoutSubviews
{
for (UIView *view in self.subviews) {
[view removeFromSuperview];
}
[self initialize];
}
- (instancetype)init
{
self = [super init];
if (self) {
self = [self initWithFrame:self.frame];
}
return self;
}
-(void)initialize
{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, self.frame.size.height);
border.borderWidth = borderWidth;
[self.layer addSublayer:border];
self.layer.masksToBounds = YES;
}

UiLabel text assignment in custom view

I have created a custom view (custom view) inside other custom view(bage). Then in main view I add custom view as a subview. After that I am trying to set text via property and it does not work.
Bage.h:
#import <UIKit/UIKit.h>
#interface Bage : UIView
#property (nonatomic,strong) UILabel *title;
#end
Bage.m:
#import "Bage.h"
#implementation Bage
#synthesize title;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:CGRectMake(0, 0, 45, 25)];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
//// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();
//// Shadow Declarations
UIColor* shadow = UIColor.blackColor;
CGSize shadowOffset = CGSizeMake(3.1, 3.1);
CGFloat shadowBlurRadius = 3;
//// Variable Declarations
CGFloat buttonWidth = rect.size.width - 2 - 4;
CGFloat buttonHeigh = rect.size.height - 2 - 4;
//// Frames
// CGRect frame = CGRectMake(frSize.origin.x, frSize.origin.y, frSize.size.width, frSize.size.height);
//// Rectangle Drawing
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(1, 1, buttonWidth, buttonHeigh) cornerRadius: 8];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, [shadow CGColor]);
[UIColor.whiteColor setFill];
[rectanglePath fill];
CGContextRestoreGState(context);
[[UIColor greenColor] setStroke];
rectanglePath.lineWidth = 1;
[rectanglePath stroke];
title = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(rect) + 1, CGRectGetMinY(rect) + 1, CGRectGetWidth(rect) - 6, CGRectGetHeight(rect) - 6)];
title.textColor = [UIColor blackColor];
title.textAlignment = NSTextAlignmentCenter;
[self addSubview:title];
}
#end
CustomView.h
#import <UIKit/UIKit.h>
#import "Bage.h"
#interface CustomView : UIView
#property (nonatomic,strong) Bage *myBage;
#end
CustomView.m
#import "CustomView.h"
#implementation CustomView
#synthesize myBage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
myBage = [[Bage alloc] init];
self.backgroundColor = [UIColor greenColor];
myBage.title.text = #"Class text";
myBage.center = self.center;
[self addSubview:myBage];
}
return self;
}
#end
ViewController.m
#import "ViewController.h"
#import "CustomView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor blueColor];
CustomView *cView = [[CustomView alloc] initWithFrame:CGRectMake(30, 30, 200, 200)];
[self.view addSubview:cView];
cView.myBage.backgroundColor = [UIColor clearColor];
cView.myBage.title.text = #"Text";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Please help me to get text in my bage.
Do not add the title as a subview in drawRect:. You should do this when the Bage view is initialized. Also, you are using the rect given to you in drawRect:, which will not always be the bounds of the view. So, add the title in your initializer

Issue adding CAGradientLayer sublayer to UIView

I've created a CAGradientLayer, which I'd like to add as a sublayer to a UIView. I can add it to self.view.layer with no problems, but cannot for the life of me get it to appear when added to the UIView.
Here's the simplified code.
- (CAGradientLayer*) makeGradient
{
//method returns the gradient layer
CAGradientLayer *gradeLayer = [CAGradientLayer layer];
gradeLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
gradeLayer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.2], [NSNumber numberWithFloat:0.5], nil];
return gradeLayer;
}
-(void)addGradient
{
//method creates UIView, then creates Gradient, and tries to add it to the UIView
UIView *myView = [[UIView alloc] initWithFrame:self.view.frame];
myView.backgroundColor = [UIColor clearColor];
myView.opaque = NO;
CAGradientLayer *bg = [self makeGradient];
CGRect myRect = myView.bounds;
myRect.size.height = myRect.size.height * 5;
myRect.origin.y = myView.bounds.size.height-myRect.size.height;
bg.frame = myRect;
//Adding gradient to self.view.layer works like a charm...
//[self.view.layer insertSublayer:bg atIndex:0];
//...however, adding it to my custom view doesn't work at all.
[myView.layer insertSublayer:bg atIndex:0];
}
What am I missing? Thanks in advance for any insight.
Your problem is that you don't seem to be setting a frame on the sub layer. Instead a cleaner option is to subclass UIView and use layerClass.
CustomGradientView.h
// For CALayers, make sure you also add the QuartzCore framework
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#interface CustomGradientView : UIView
// Redeclare the layer property but as the new CALayer class so it can receive the correct
// messages without compiler warnings.
#property (nonatomic, strong, readonly) CAGradientLayer *layer;
#end
CustomGradientView.m
#import "CustomGradientView.h"
#interface CustomGradientView ()
#end
#implementation CustomGradientView
+(Class)layerClass
{
return [CAGradientLayer class];
}
-(instancetype)init
{
self = [super init];
if (self) {
[self customInit];
}
return self;
}
-(instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
[self customInit];
}
return self;
}
-(void)customInit
{
self.layer.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
self.layer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.2], [NSNumber numberWithFloat:0.5], nil];
}
#end

Attempting to set title for views in xcode iOS project

What do I need to do to be able to set the title of each main view in my iOS project. Below is what I've got so far and what I'm attempting to do...
Created Files That I'm attempting to get to work together.
PageTitleView.h
PageTitleView.m
PageBaseViewController.h
PageBaseViewController.m
HomeViewController.h
HomeViewController.m
ActivityFeedViewController.h
ActivityFeedViewController.m
Essentially setup the PageTitleView.h * .m along with PageBaseViewController.h & .m so that I can then make a call into the HomeViewController&ActivityFeedViewController and set the title of the current view.
Here is my code from those files
PageTitleView.h
#import "ContainerView.h"
#import "BaseLabel.h"
#interface PageTitleView : ContainerView
#property (nonatomic, strong) BaseLabel* label;
#property (nonatomic, strong) NSString* title;
#end
PageTitleView.m
#import "PageTitleView.h"
#implementation PageTitleView
#synthesize label;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 35)];
label = [[BaseLabel alloc] initWithFrame:CGRectMake(18, 10, frame.size.width , 25)];
label.font=[UIFont fontWithName:#"Helvetica" size:15.0];
label.textColor = (UIColor *) COLOR_DARK_BLUE;
label.text =[[NSString alloc] initWithFormat: #"text"];
self.backgroundColor = (UIColor *)COLOR_WHITE;
self.layer.shadowOpacity = 0.05;
self.layer.shadowOffset = CGSizeMake(.25, .25);
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowRadius = 5;
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.layer.bounds].CGPath;
self.layer.cornerRadius = 10.0;
self.layer.borderWidth = 0.25;
self.layer.borderColor = [UIColor grayColor].CGColor;
[self addSubview:label];
return self;
}
#end
PageBaseViewController.h
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#import "Settings.h"
#import "CustomButton.h"
#import "PageTitleView.h"
#interface PageBaseViewController : UIViewController
#property (nonatomic, strong) UIScrollView *scrollView;
#property (nonatomic, strong) CustomButton *backButton;
#end
PageBaseViewController.m
#import "PageBaseViewController.h"
#interface PageBaseViewController ()
#end
#implementation PageBaseViewController
synthesize scrollView;
- (void)viewDidLoad {
[super viewDidLoad];
// GRADIENT
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = [NSArray arrayWithObjects:
(id)BACKGROUND_GRADIENT_COLOR_1.CGColor,
(id)BACKGROUND_GRADIENT_COLOR_2.CGColor,
(id)BACKGROUND_GRADIENT_COLOR_3.CGColor, nil];
gradient.locations = [NSArray arrayWithObjects:
(id)[NSNumber numberWithFloat:0.0],
(id)[NSNumber numberWithFloat:0.4],
(id)[NSNumber numberWithFloat:1.0], nil];
[self.view.layer insertSublayer:gradient atIndex:0];
float height = self.view.frame.size.height - TITLEBAR_HEIGHT - self.tabBarController.tabBar.frame.size.height;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, TITLEBAR_HEIGHT, self.view.frame.size.width, height)];
[self.view addSubview:scrollView];
PageTitleView *pageTitle = [[PageTitleView alloc] initWithFrame:CGRectMake(105, -10, 100 , 35)];
[self.view addSubview:pageTitle];
/* This button is currently added to the view and will show in the TITLEBAR_HEIGHT.
These will be used throughout the entire project, we will be working next to have
correct buttons turn on and off for each view.*/
CustomButton *buttonLeft = [[CustomButton alloc] initWithFrame:CGRectMake(5, 5, 60, 25) andText:#"Left"];
[buttonLeft addTarget:self action:#selector(buttonLeft:) forControlEvents:UIControlEventTouchUpInside];
[buttonLeft setBackgroundColor:BUTTON_COLOR_1];
[self.view addSubview:buttonLeft];
// Remove this button from homeview and have a logo show up.
CustomButton *buttonRight = [[CustomButton alloc] initWithFrame:CGRectMake(235, 5, 80, 25) andText:#"Right"];
[buttonRight addTarget:self action:#selector(buttonRight:) forControlEvents:UIControlEventTouchUpInside];
[buttonRight setBackgroundColor:BUTTON_COLOR_1];
[self.view addSubview:buttonRight];
}
- (void)buttonLeft:(UIButton *)buttonLeft {
NSLog(#"Ping");
}
- (void)buttonRight:(UIButton *)buttonRight {
NSLog(#"Ping");
}
#end
Solved this with the following steps.
Imported the PageTitleView.h into the PageBaseViewController.h
In the PageBaseViewController.h declared the following property
#property (nonatomic, strong) PageTitleView *pageTitle;
Then in the PageBaseViewController adjusted the import of PageTitleView
From:
PageTitleView *pageTitle = [[PageTitleView alloc] initWithFrame:CGRectMake(105, -10, 100 , 35)];
To:
pageTitle = [[PageTitleView alloc] initWithFrame:CGRectMake(105, -10, 100 , 35)];
Then in one of my views for example HomeViewController I added the following code.
[self.pageTitle setTitle:#"Home"];
Now I'll add the following code to my other views and fill it in with the appropriate name.
[self.pageTitle setTitle:#""];

Resources