hi i'm begginer in objective-c and i want to learn how i can make UILabel in center of screen
this is my code:
XXRootViewController.h
#interface XXRootViewController : UIViewController{
UILabel *title; } #end
XXRootViewController.m
#import "XXRootViewController.h"
#implementation XXRootViewController {
NSMutableArray *_objects;
}
- (void)loadView {
[super loadView];
self.view = [[[UIView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]]
autorelease];
self.view.backgroundColor = [UIColor whiteColor];
title = [[UILabel alloc] initWithFrame:CGRectMake(300,200,400,200)];
title.text = #"This is Sasuke's first app :)";
[self.view addSubview:title];
}
#end
Try this code i make some changes in you code
self.view = [[UIView alloc]
initWithFrame:[UIScreen mainScreen].bounds];
self.view.backgroundColor = [UIColor whiteColor];
title = [[UILabel alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width - 50,200)];
title.text = #"This is Sasuke's first app :)";
title.textAlignment = NSTextAlignmentCenter;
title.center = self.view.center;
[self.view addSubview:title];
You just need to set frame in center of the screen ...
change your one line code with it -
title = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 200,self.view.frame.size.height/2 - 100,400,200)];
now your label is shown in the center of screen .. This will help you change the frame according to your use...
Has Hardik Thakkar said, the best way would be to use the center method. That way, even if your label would grow, it will always be centred.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(300,200,400,200)];
label.text = #"something";
label.center = self.view.center; (this should always be you superview)
Now, even if your label is bigger than the size you were expecting, it will always be centred
Related
myview = [[AVPlayerViewController alloc]init];
[window.rootViewController.view addSubview:controller.view];
UILabel *label = [[UILabel alloc] init];
label.text = #"TEST";
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Menlo" size:(72.0)];
[myview.contentOverlayView addSubview:label];
myview.view.frame = window.rootViewController.view.frame;
// Disable playback controls
myview.showsPlaybackControls=false;
// show the view controller
[window.rootViewController addChildViewController:myview];
myview is playing video, but the "TEST" label isn't visible. I'm not sure what I'm doing wrong.
Currently, I programatically created 7 buttons inside a UIView. I added two lines of UIlabel to the UIButton. I created another UIView as a selector and whenever the button is clicked, I will moved the selector to the location of the button.
The problem is the selector UIView is another layer on top of the buttons and the color is mixed or distorted.
What I have and partly trying to acheive
What I am trying to acheive
To programatically insert 2 lines of title to the buttons and change the background color of the button when it is selected.
Code
-(void)setupSegmentButtons{
CGFloat Y_POS_BTN = [[UIApplication sharedApplication] statusBarFrame].size.height+5;
//=== The view where the buttons sits
navigationView = [[UIView alloc]initWithFrame:CGRectMake(X_BUFFER,Y_POS_BTN,self.view.frame.size.width,HEIGHT_BTN)];
navigationView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:navigationView]; //=== Create a View called navigationView
//==== Setup the shadows around the view ===
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.navigationView.bounds];
self.navigationView.layer.masksToBounds = NO;
self.navigationView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
self.navigationView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
self.navigationView.layer.shadowOpacity = 0.8f;
self.navigationView.layer.shadowPath = shadowPath.CGPath;
//=== Get the dates and formatting of the dates
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *beginningOfThisWeek;
NSTimeInterval durationOfWeek;
[calendar rangeOfUnit:NSWeekCalendarUnit
startDate:&beginningOfThisWeek
interval:&durationOfWeek
forDate:now];
NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:now];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/YYYY"];
NSDateFormatter *datelblFormat = [[NSDateFormatter alloc] init];
[datelblFormat setDateFormat:#"dd"];
NSDateFormatter *daylblFormat= [[NSDateFormatter alloc] init];
[daylblFormat setDateFormat:#"EEE"];
//=== Loop 7 times to create the Buttons and the 2 lines Labels
for (int i = 0; i<numControllers; i++) {
button = [[UIButton alloc]initWithFrame:CGRectMake(i* (self.navigationView.frame.size.width/numControllers), Y_BUFFER, (self.navigationView.frame.size.width/numControllers),HEIGHT_BTN)];
[navigationView addSubview:button]; //=== Put the buttons into the navigation View
NSString *dateString = [dateFormatter stringFromDate:[calendar dateFromComponents:comps]];
[dtDate addObject:dateString];
NSString *lblDate = [datelblFormat stringFromDate:[calendar dateFromComponents:comps]];
firstLineButton = [[UILabel alloc] initWithFrame:CGRectMake(0,5,self.view.frame.size.width/numControllers,HEIGHT_LABEL)];
firstLineButton.text = lblDate;
firstLineButton.font = [UIFont systemFontOfSize:20];
firstLineButton.textColor = [UIColor whiteColor];
firstLineButton.textAlignment=NSTextAlignmentCenter;
[button addSubview:firstLineButton]; //=== Put the Date in the 1st line of the the button
NSString *lblDay = [daylblFormat stringFromDate:[calendar dateFromComponents:comps]];
UILabel *secondLineButton = [[UILabel alloc] initWithFrame:CGRectMake(0,28,self.view.frame.size.width/numControllers,HEIGHT_LABEL2)];
secondLineButton.text = lblDay;
secondLineButton.textColor = [UIColor whiteColor];
secondLineButton.font = [UIFont boldSystemFontOfSize:11];
secondLineButton.textAlignment=NSTextAlignmentCenter;
[button addSubview:secondLineButton]; //=== Put the Day in the 2nd line of the Button
if(i < 6){
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(button.frame.size.width - 1, 10, 1, button.frame.size.height - 25)];
separator.backgroundColor = UIColor.whiteColor;
[button addSubview:separator];
}
button.tag = i; //--- IMPORTANT: if you make your own custom buttons, you have to tag them appropriately
button.backgroundColor = [UIColor colorWithRed:236.0f/255.0f green:0/255.0f blue:140.0f/255.0f alpha:0.6];//%%% buttoncolors
[button addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
++comps.day;
}
//NSLog(#" The array %#", dtDate);
[self setupSelector]; //=== The selection bar or highligthed area
}
//=== sets up the selection bar under the buttons or the highligted buttons on the navigation bar
-(void)setupSelector {
selectionBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (self.view.frame.size.width/numControllers),HEIGHT_BTN)];
selectionBar.backgroundColor = [UIColor colorWithRed:1.0f/255.0f green:179.0/255.0f blue:242.0f/255.0f alpha:0.6]; //%%% sbcolor
[navigationView addSubview:selectionBar];
}
//=== When the top button is tapped
-(void)tapSegmentButtonAction:(UIButton *)button {
sDtDate = dtDate[button.tag];
[self LoadClasses];
__weak typeof(self) weakSelf = self;
[weakSelf updateCurrentPageIndex:button.tag];
NSInteger xCoor = selectionBar.frame.size.width*self.currentPageIndex;
selectionBar.frame = CGRectMake(xCoor, selectionBar.frame.origin.y, selectionBar.frame.size.width, selectionBar.frame.size.height);
}
//=== makes sure the nav bar is always aware of what date page you're at in reference to the array of view controllers you gave
-(void)updateCurrentPageIndex:(int)newIndex {
self.currentPageIndex = newIndex;
}
I think you do not need to use a secondary button or additional labels.
You can do all the stuff with button itself.
Following may help you by converting it to Objective-C:
Swift - UIButton with two lines of text
You can use a custom button which inherit from UIButton with two labels and rewrite it's setSelected: method,for example:
#import "MyButton.h"
#interface MyButton ()
#property (nonatomic, strong) UILabel *mainTitleLabel;
#property (nonatomic, strong) UILabel *subTitleLabel;
#end
#implementation MyButton
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.mainTitleLabel = [[UILabel alloc] init];
self.subTitleLabel = [[UILabel alloc] init];
// other setting
}
return self;
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
self.backgroundColor = [UIColor whiteColor];
self.mainTitleLabel.textColor = [UIColor redColor];
self.subTitleLabel.textColor = [UIColor redColor];
} else {
self.backgroundColor = [UIColor blueColor];
self.mainTitleLabel.textColor = [UIColor orangeColor];
self.subTitleLabel.textColor = [UIColor orangeColor];
}
}
#end
hi Im programmatically creating an UILabel like this
`
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[[UILabel alloc] init] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)];
// _titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
_titleLabel.backgroundColor =[UIColor clearColor];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
_titleLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:_titleLabel];
}
return _titleLabel;
}`
this _photoView is an UIImageView I have created already. I want to change the UILabel View Y value. But the problem is when I change this second parameter label y position is not changing. Any one can tell e the reason for this.
And this is how I created the ImageView
`
- (UIImageView *)photoView {
if (!_photoView) {
_photoView = [[UIImageView alloc] init];
_photoView.contentMode = UIViewContentModeScaleAspectFill;
_photoView.clipsToBounds = YES;
_photoView.layer.cornerRadius = 5;
_photoView.clipsToBounds = YES;
[self addSubview:_photoView];
}
return _photoView;
}`
Thanks
In this line , you have used two types init
[[[UILabel alloc] init] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)]
change this line to
[[UILabel alloc] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)]
Purpose:
I want to set right view as label in searchbar's textfield
Following Code working fine in ios 6 for doing the same thing:
UISearchBar *search = [[UISearchBar alloc] init];
[search setTintColor:[UIColor grayColor]];
search.frame = CGRectMake(0, 150, 320,50);
search.delegate = self;
UITextField *searchField=[search.subviews objectAtIndex:1];//Changed this line in ios 7
searchField.backgroundColor=[UIColor redColor];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=#"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
[self addSubView:search];
When i have Run the same code in ios 7(with xcode 7.0.2 fully updated), It was giving me error, after googling i got that hiearchy of searchbar to textfield has been changed, so i have modified my code as:
replaced commented line in above code with:
UITextField *searchField=[((UIView *)[search.subviews objectAtIndex:0]).subviews lastObject];
after doing this, it is not giving me any error(also checked the logs by printing the description of textfield and everything is working good), when i run it, it gives textfield with red background but the rightView's label is not shown to me. Now, can anyone help me how to show this rightview.
If you want to do this programatically then you have to declare UISearchBar object in your .h file.
UISearchBar *searchBar;
And in your .m file you can code like:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
searchBar = [[UISearchBar alloc] init];
[searchBar setTintColor:[UIColor grayColor]];
searchBar.frame = CGRectMake(0, 150, 320,50);
searchBar.delegate = self;
[self.view addSubview:searchBar];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITextField *searchField;
if ([[[UIDevice currentDevice] systemVersion] floatValue]<7.0)
searchField=[searchBar.subviews objectAtIndex:1];
else
searchField=[((UIView *)[searchBar.subviews objectAtIndex:0]).subviews lastObject];
searchField.backgroundColor=[UIColor redColor];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=#"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
I think this will work for both iOS7 and prior versions of iOS7.
First of all, your approach is not good and not maintainable across iOS versions.
I think that initialization of UISearchBarTextField's right view is taking place after you set your custom view to it's rightView property. You will see correct result if you set searchField.rightView in viewDidAppear method:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITextField *searchField=[((UIView *)[_searchBar.subviews objectAtIndex:0]).subviews lastObject];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=#"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
I've tested it on iOS 7 simulator and it works.
You have to get the text field first.
No need to index subviews to get text field or bar button.
You can directly get the text field and bar button
In swift
let textField = searchBar.valueForKey("searchField") as? UITextField
let cancelBarButton = searchBar.valueForKey("cancelBarButtonItem") as? UIBarButtonItem
In Objective-C
UITextField *textField = [searchBar valueForKey:#"searchField"]:
UIBarButtonItem *cancelBarButton = [searchBar valueForKey:#"cancelBarButtonItem"]:
Then you can set the left view and right view with
textField.leftView = yourView
textField.rightView = yourView
I want to set a custom view to the inputView property of a UITextField. (Basically because I want to add a UILabel to the selectionIndicator of a UIPickerView and I couldn't find an easier way...) If I set a UIPickerView as the inputView, everything works perfectly normal. But if I set a UIView as the inputView, it just doesn't show up when the UITextField becomes the firstResponder. Has anybody got an explanation for me here?
This works:
UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
// configuration of field
UIPickerView* pickerView = [[UIPickerView alloc] initWithFrame:field.inputView.frame];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
field.inputView = pickerView;
This doesn't:
UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
// configuration of field
UIView* view = [[UIView alloc] initWithFrame:field.inputView.frame];
view.backgroundColor = [UIColor redColor];
field.inputView = view;