how to create bottom line border in multiple textfield - ios

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;
}

Related

Reuse xib in different screens

I have 4 screens that have one almost the same view:
And one screen have the same view but with slightly different UI:
So, my question: Can I use one xib and adapt states (active, inactive) and change ui for different screen? How I can do it?
Here is an example of this kind of class
In Your .m file of custom XIB class if you are using objective-c.
- (void)foo:(NSString*)labelText andButtonText:(NSString*)buttonTitle {
//Do your code here for some screen like change labels and button text
}
- (void)bar:(NSString*)labelText andButtonText:(NSString*)buttonTitle {
//Do your code here for some another screen and change labels and button text
}
In Your .h file of custom XIB class if you are using objective-c.
- (void)foo:(NSString*)labelText andButtonText:(NSString*)buttonTitle;
- (void)bar:(NSString*)labelText andButtonText:(NSString*)buttonTitle;
In your view controller where you want to display the custom UI
Create an instance of your xib or add through interfacebuilder
And On your instance of custom class call the method as required.
Below is a class I've used in one of my project to get a clear understanding.
#import <UIKit/UIKit.h>
#import "DYRateView.h"
#interface LevelAndRankDetails : UIView
#property (nonatomic, strong) IBOutlet UIView* contentView;
#property (nonatomic, strong) IBOutlet UIView* viewContainer;
#property (nonatomic, strong) IBOutlet UILabel* lblLevel;
#property (nonatomic, strong) IBOutlet UILabel* lblRanking;
#property (weak, nonatomic) IBOutlet DYRateView *viewRate;
- (void)setLevel:(NSNumber*)level andRanking:(NSNumber*)ranking;
- (void)setupUI;
#end
.m File
#import "LevelAndRankDetails.h"
#import "AppDelegate.h"
#import "Constants.h"
#implementation LevelAndRankDetails
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]){
[self commonSetup];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self commonSetup];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
}
- (void)viewFromNibForClass {
[[NSBundle mainBundle] loadNibNamed:[[self class] description] owner:self options:nil];
[self addSubview:self.contentView];
self.contentView.frame = self.bounds;
}
- (void)commonSetup {
[self viewFromNibForClass];
//For View's Corner Radius
self.contentView.layer.cornerRadius = 12;
self.contentView.layer.masksToBounds = YES;
self.contentView.backgroundColor = kDefaultBackgroundGreyColor;
self.viewContainer.backgroundColor = kDefaultBackgroundGreyColor;//[UIColor clearColor];
self.backgroundColor = kDefaultBackgroundGreyColor;
//self.viewContainer.backgroundColor = UIColorFromRGB(0xBB9657);//[kLearnFromLightColor colorWithAlphaComponent:0.5];
self.viewRate.rate = 0;
self.viewRate.editable = NO;
self.viewRate.delegate = nil;
self.viewRate.alignment = RateViewAlignmentCenter;
self.viewRate.backgroundColor = [UIColor clearColor];
[self.viewRate setEmptyStarImage:[UIImage imageNamed:#"StarEmpty"]];
UIImage* imageFullStar = [[UIImage imageNamed:#"StarFull"] imageTintedWithColor:kSliderDarkYellowColor];
[self.viewRate setFullStarImage:imageFullStar];
self.lblLevel.textColor = [UIColor whiteColor];
self.lblRanking.textColor = [UIColor whiteColor];
//For Teacher label
}
- (void)setupUI {
self.contentView.layer.cornerRadius = 0;
self.contentView.layer.masksToBounds = YES;
self.contentView.backgroundColor = [UIColor clearColor];
self.viewContainer.backgroundColor = [UIColor clearColor];//[UIColor clearColor];
self.backgroundColor = [UIColor clearColor];
}
- (void)setRanking:(CGFloat)ranking {
self.viewRate.rate = ranking;
}
- (void)setLevel:(NSNumber*)level {
self.lblLevel.text = [NSString stringWithFormat:#"Level : %#",level];
}
- (void)setLevel:(NSNumber*)level andRanking:(NSNumber*)ranking {
if (level.integerValue > 0) {
[self setLevel:level];
}
if (ranking.doubleValue > 0) {
CGFloat rankingConverted = ranking.floatValue;
[self setRanking:rankingConverted];
}
}
#end
And this is how you use it in your view controller
LevelAndRankDetails* toolTipCustomView = [[LevelAndRankDetails alloc] initWithFrame:CGRectMake(0, 0, 250, 66)];
toolTipCustomView.backgroundColor = [UIColor blackColor];
[toolTipCustomView setLevel:#(10) andRanking:#(3)];

Customize an existing button in Objective C

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
}

How can i display string instead of image in UIView?

Interface is define like this
#interface IGLDemoCustomView : UIView
#property (nonatomic, strong) UIImage *image;
#property (nonatomic, strong) NSString *title;
#end
While Implementation file look like this
#interface IGLDemoCustomView ()
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UILabel *titleLabel
#end
#implementation IGLDemoCustomView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
[self initView];
}
- (void)initView
{
self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor;
self.layer.borderWidth = 2.0;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.alpha = 0.8;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
self.imageView = imageView;
UILabel *titleLabel;
self.titleLabel = [[UILabel alloc]init];
titleLabel.center = self.center; // set proper frame for label
[self addSubview:titleLabel];
}
- (void)setImage:(UIImage *)image
{
_image = image;
self.imageView.image = image;
}
- (void)setString:(NSString *)title
{
self.title=title;
self.titleLabel.text = title;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.imageView.frame = self.bounds;
self.layer.cornerRadius = frame.size.height / 2.0;
}
#end
When i select image from the drop down menu it shows in the menu, but when i select any text from the drop down menu it doesnt show in the drop down list view.
Any clue will be highly appreciated.
Calling and setting string in the view
IGLDropDownItem *menuButton = strongSelf.dropDownMenu.menuButton;
IGLDemoCustomView *buttonView = (IGLDemoCustomView*)menuButton.customView;
buttonView.title = device.name;
You need to set a frame for the label
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
self.imageView.frame = self.bounds;
self.titleLabel.frame = self.bounds;
self.layer.cornerRadius = frame.size.height / 2.0;
}
If you are expecting the text to wrap, you'll need to set the lineBreakMode and set numberOfLines to 0
Like you are having UIImageView you need to have a UILabel inside your UIView too.
#interface IGLDemoCustomView ()
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UILabel *titleLabel;
#end
- (void)initView
{
self.layer.borderColor = [UIColor colorWithRed:0.18 green:0.59 blue:0.69 alpha:1.0].CGColor;
self.layer.borderWidth = 2.0;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
self.alpha = 0.8;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeCenter;
[self addSubview:imageView];
self.imageView = imageView;
self.titleLabel = [[UILabel alloc]init];
titleLabel.center = self.center; // set proper frame for label
[self addSubview:titleLabel]
}
And in setString
- (void)setString:(NSString *)title
{
self.title=title;
self.titleLabel.text = title;
}

Can't switch to number keyboard iOS

My app won't allow users to switch keyboards to the number/character keyboard.
I don't have anything in my UI Inspector showing over the bottom of the screen.
I am truly stumped as to what could be causing this issue. Whatever it is blocks the number/symbol switcher, the keyboard language (or Emoji) selector, and the speech to text button. The space bar and return key on the same row continue to operate fine.
The affected UITextField's are subclassed, the keyboard works as expected on the other 5 views in which they are contained, but here is their code:
JCTextField.h
#import <UIKit/UIKit.h>
IB_DESIGNABLE
#interface JCTextField : UITextField
#property (nonatomic, retain) IBInspectable UIColor *underlineColor;
#property (nonatomic) IBInspectable CGFloat underlineWidth;
#property (nonatomic, retain) IBInspectable UIImage *sideImage;
#property (nonatomic) IBInspectable CGFloat internalOffset;
#property (nonatomic) IBInspectable CGFloat borderOffset;
#end
JCTextField.m
#import "JCTextField.h"
#implementation JCTextField
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self != nil) {
self.underlineColor = [UIColor blackColor];
self.underlineWidth = 1;
self.internalOffset = 0;
self.borderOffset = 0;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CALayer *border = [CALayer layer];
border.borderColor = _underlineColor.CGColor;
border.frame = CGRectMake(_borderOffset, self.frame.size.height - _underlineWidth, self.frame.size.width - (_borderOffset * 2), self.frame.size.height);
border.borderWidth = _underlineWidth;
[self.layer addSublayer:border];
self.layer.masksToBounds = YES;
[self setValue:[UIColor lightTextColor] forKeyPath:#"_placeholderLabel.textColor"];
[self setTextColor:[UIColor whiteColor]];
[self setFont:[UIFont fontWithName:#"Montserrat-Regular" size:16.0]];
if (self.sideImage) {
UIImageView *sideImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width - 30, 0, 30, 30)];
sideImageView.contentMode = UIViewContentModeCenter;
sideImageView.image = self.sideImage;
[self addSubview:sideImageView];
}
[self setTintColor:[UIColor whiteColor]];
[self setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
}
- (CGRect)textRectForBounds:(CGRect)bounds {
if (self.sideImage) {
UIEdgeInsets insets = UIEdgeInsetsMake(0, _internalOffset, 0, 18);
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)];
}
UIEdgeInsets insets = UIEdgeInsetsMake(0, _internalOffset, 0, 0);
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)];
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
if (self.sideImage) {
UIEdgeInsets insets = UIEdgeInsetsMake(0, _internalOffset, 0, 18);
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)];
}
UIEdgeInsets insets = UIEdgeInsetsMake(0, _internalOffset, 0, 0);
return [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, insets)];
}
#end

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

Resources