UiLabel text assignment in custom view - ios

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

Related

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

Create a CAShapeLayer around UIButton?

I have a UIButton, I want to create a BORDER outside its content.
I want to create it using CAShapeLayer or any other proper way.
How can I do so.
The property of borderColor and BorderWidth of UIButton creates a border inside the BUTTON. As more width, as more space is consumed from inside.
Thanks.
// This is the *.m file
#import "ViewController.h"
#interface ViewController ()
// Comment A. Another label and button added to the class without adding them to the
// storyboard
#property (nonatomic, strong) UIButton *firstButton;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.firstButton setTitle:#"Just a Button" forState:UIControlStateNormal];
self.firstButton.frame = CGRectMake(50, 50, 300, 40);
UIGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(theAction:)];
[self.firstButton addGestureRecognizer:tapGesture];
//The screen width and height
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
//The CALayer definition
CALayer *graphic = nil;
graphic = [CALayer layer];
graphic.bounds = CGRectMake(0, 0, screenHeight, screenWidth);
graphic.position = CGPointMake(screenHeight/2, screenWidth/2);
graphic.backgroundColor = [UIColor whiteColor].CGColor;
graphic.opacity = 1.0f;
[graphic addSublayer:self.firstButton.layer];
[self.view.layer addSublayer:graphic];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)theAction:(id)sender {
NSLog(#"Button Tapped");
}
#end

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

TabViewController setting UINavigationBar

I am trying to programatically setup my UINavigationBar. I have setup the NavBar for each tab in interface builder and it inherits from my GenericTabUiNavBar in interface builder.
However when I run the app, the GenericTabUiNavBar is not loaded with its elements into the UINavBar.
Here is my GenericTabUiNavBar code:
.h
#import <UIKit/UIKit.h>
#interface GenericTabUiNavBar : UINavigationBar
#property (nonatomic, strong) UILabel *firstLetter;
#property (nonatomic, strong) UILabel *secondLetter; //need two labels
#property (nonatomic,strong) UIButton *leftSideButton;
#property (nonatomic,strong) UIButton *rightSideButton;
#end
.m
#implementation GenericTabUiNavBar
#synthesize firstLetter;
#synthesize secondLetter;
#synthesize leftSideButton;
#synthesize rightSideButton;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
//r g b
//Yellow Color = 255,219,17
UIColor *yellowColor = [[UIColor alloc] initWithRed:255 green:219 blue:17 alpha:1];
UIColor *blueColor = [[UIColor alloc] initWithRed:1 green:129 blue:200 alpha:1];
UIFont *fontFaceAndSize = [UIFont fontWithName:#"HelveticaNeue" size:36];
//self
self.frame = CGRectMake(0, 20, 320, 44);
self.backgroundColor = blueColor;
self.barTintColor = blueColor;
//x.108 y.31
//w.15 / y.21
CGRect firstLetterRect = CGRectMake(108, 31, 15, 21);
self.firstLetter = [[UILabel alloc] initWithFrame:firstLetterRect];
firstLetter.text = #"An";
firstLetter.font = fontFaceAndSize;
firstLetter.textColor = [UIColor whiteColor];
//128 31
//22 21
CGRect secLetterRect = CGRectMake(108, 31, 15, 21);
self.secondLetter = [[UILabel alloc] initWithFrame:secLetterRect];
secondLetter.text = #"APP";
secondLetter.font = fontFaceAndSize;
secondLetter.textColor = yellowColor;
//tex pos =
self.leftSideButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 29, 30, 26)];
self.rightSideButton = [[UIButton alloc] initWithFrame:CGRectMake(207, 29, 30, 26)];
[leftSideButton setImage:[UIImage imageNamed:#"anImg.png"] forState:UIControlStateNormal];
[rightSideButton setImage:[UIImage imageNamed:#"otherImg.png"] forState:UIControlStateNormal];
[self addSubview:leftSideButton];
[self addSubview:firstLetter];
[self addSubview:secondLetter];
[self addSubview:rightSideButton];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
In my TabViewController I call it by hooking it up in interface builder. Why does this not load?
When controllers are loaded from storyboard or xib. The init method
- (id)initWithCoder:(NSCoder *)decoder
is called instead of the
- (instancetype)initWithNibName:(NSString *)nibName
bundle:(NSBundle *)nibBundle
as for views the reason might be the same

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