How can I set custom font in UINavigationBar? - iphone

How can I set custom font in UINavigationBar ? I need the tahoma font.
- (void)viewDidLoad{
self.title =#"My text";
}

Totally possible, if a little tricky to do. Once you've found the font you need (either one of the alternatives already shipped with iOS, or a TTF file that you've got the correct licensing for), just create a UILabel with the size, formatting, font etc and then add it to the navigation items for that bar (or, if you're doing this in a view controller, set the .navigationItem.titleView of that controller to your label).
For example, I have a view controller inside a UINavigationController. To change the label in the top to a custom font, I simply do:
//...I have already setup a UILabel called navLabel that has the same style as a
// default navigation bar title, but I've changed the .font property to my custom font
self.navigationItem.titleView = navLabel;
[navLabel release];

This code should work. In uiviewcontroller which presents your main ui:
- (void)viewDidLoad
{
[super viewDidLoad];
int height = navigationController.navigationBar.frame.size.height;
int width = navigationController.navigationBar.frame.size.width;
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
navLabel.font = [UIFont boldSystemFontOfSize:15];
navLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = navLabel;
[navLabel release];
}
Note that resulting custom view has transparent background, so that you can add something more to your navigation bar with [navigationController.navigationBar addSubview:view]. This may be spinner in left corner of the bar or something else.
If you use custom view, you will not be able set the title with uiviewcontroller title anymore. You need to use the way available by your custom view.
Example:
((UILabel *)self.navigationItem.titleView).text = title;

Related

Setting custom UINavigationBar

Hello everyone I have a problem with my custom navigation bar.
I needed to create a custom navigation bar and this was to be used in several view controllers so i created it as a category for UIViewController and Used the following code for creating the customisation i needed.
- (void)setCustomLabel:(NSString *)labelText
{
UILabel *navigationLabel = [[UILabel alloc]initWithFrame:CGRectMake(60,10,40,40)];
[navigationLabel setBackgroundColor:[UIColor clearColor]];
navigationLabel.font = [UIFont fontWithName:#"Humanist 521 BT-Bold" size:15.0];
navigationLabel.font = [UIFont boldSystemFontOfSize:18.0];
navigationLabel.textColor = [UIColor whiteColor];
navigationLabel.text = labelText;
navigationLabel.shadowColor = [UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0];
navigationLabel.shadowOffset = CGSizeMake(0.0, -1.0);
[navigationLabel sizeToFit];
[self.navigationController.navigationBar addSubview:navigationLabel];
[navigationLabel release];
}
On first view there are 2 buttons signIn and Register when i click on signIn button it will take me to signIn View and when i click on register button it will take me to Register view.
I created 2 ViewControllers and set the navigationBarLabel in both views as Register and SignIn using the code :
[self setCustomLabel:#"REGISTER"];
and
[self setCustomLabel:#"SIGN IN"];
The views would have a title displayed as
and
and it does look this way when i first run the application and click on either register or signIn Button but if i click on any of the 2 buttons navigate to the register or signIn view and then i go back and click on the second button the navigation bar changes to
Please help me out i have been at this for a very long time i set the navigation bar in viewDidAppear and i have also tried setting it to nil
[self setCustomLabel:nil];
in viewWillDisappear and in viewDidDisappear . I am new to iPhone development help me ou
This is because you are adding the label to the navigation bar. Since it is the same navigation bar no matter how many views you go to, it just keeps adding new labels to the bar and leaves them there.
The way that I see it you have two options to fix this:
You can create a singular label one time and have it always on the navigation bar and just set its text in the method so that it always has the correct text and set it to hidden when you do not want it to be visible.
You can do the same thing you are doing, but add a tag to the label and before creating the new label, you can iterate through the NavigationBar's subviews and remove the old label by checking the tags.
You're not removing the label from the navigationbar when you're addign a new one.
Perhaps you should try calling [navigationLabel removeFromSuperview] on it when you wish to set a new one. (This means you probably have to store it in an #property)
example:
header file:
#property (assign, nonatomic) UILabel *navigationLabel;
Implementation file:
#synthesize navigationLabel
- (void)setCustomLabel:(NSString *)labelText {
if (self.navigationLabel) [self.navigationLabel removeFromSuperview];
self.navigationLabel = [[UILabel alloc]initWithFrame:CGRectMake(60,10,40,40)];
[self.navigationLabel setBackgroundColor:[UIColor clearColor]];
self.navigationLabel.font = [UIFont fontWithName:#"Humanist 521 BT-Bold" size:15.0];
self.navigationLabel.font = [UIFont boldSystemFontOfSize:18.0];
self.navigationLabel.textColor = [UIColor whiteColor];
self.navigationLabel.text = labelText;
self.navigationLabel.shadowColor = [UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0];
self.navigationLabel.shadowOffset = CGSizeMake(0.0, -1.0);
[self.navigationLabel sizeToFit];
[self.navigationController.navigationBar addSubview:self.navigationLabel];
[self.navigationLabel release];
}

How to achieve UIPickerView as shown in image, with titles for components?

How to achieve picker view like this? I have implemented all necessary delegate and dataSource methods to populate the data, but the thing I am not able to understand is how to add this titles adults, children and infants?
They are static and does not spin with the component!
Add the 3 labels to your view as subviews when you showing the picker view and then hiding them when the picker is dismissed.
You will have to position the labels on the band.
I got this done just using Interface Builder.
I created a container view and then put picker view inside it.
To be sure that my container size is the same as picker view I set space constraints: leading, trailing, top and bottom.
Then I put 3 labels above picker view (but they're still the subviews of the container) and set their frames to center it.
Also to achieve the same label visual effect as on the screenshot (it seems to be under selection bar) decrease label's alpha to about 0.7.
You could put the labels on a particular frame position and then make the labels background color as clearColor.
You need to add labels as subviews of the picker view. There is no functionality built into UIPickerView to make this easy.
Create your picker, create a label with a shadow, and push it to a picker's subview below the selectionIndicator view.
It would look something like this
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(135, 93, 80, 30)] autorelease];
label.text = #"Label";
label.font = [UIFont boldSystemFontOfSize:20];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake (0,1);
[picker insertSubview:label aboveSubview:[picker.subviews objectAtIndex:5]];
//When you have multiple components (sections)...
//you will need to find which subview you need to actually get under
//so experiment with that 'objectAtIndex:5'
//
//you can do something like the following to find the view to get on top of
// define #class UIPickerTable;
// NSMutableArray *tables = [[NSMutableArray alloc] init];
// for (id i in picker.subviews) if([i isKindOfClass:[UIPickerTable class]]) [tables addObject:i];
// etc...

Add UILabel to UITableView - iphone

I am an iOS development newbie. I have a settings screen which is a UITableView. I want to add some explanation to it. I am using the following code to do it, but it skews up the text completely. Any idea what I am doing wrong?
UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 175)];
subjectLabel.font = [UIFont systemFontOfSize:16.0];
subjectLabel.numberOfLines = 0;
subjectLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(10.0)];
subjectLabel.backgroundColor = [UIColor clearColor];
//bodyLabel.textAlignment = UITextAlignmentLeft;
subjectLabel.text = #"mytext";
settingTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0,0, 320, 370) style:UITableViewStyleGrouped] autorelease];
settingTableView.dataSource = self;
settingTableView.delegate = self;
[settingTableView addSubview:subjectLabel];
[self.view addSubview:settingTableView];
A tableViewHeader is a UIView which is set as the tableViewHeader property of a tableView. If you want to have a UILabel in a header view, make a separate UIView (either in code, or in a nib), and set it as the tableView.tableHeaderView property. More information can be found here: TableView Reference. Hope that helps!
create a view in your view controller and add your lable to that and bind it ...
IBOutlet UIView *headerView1;
and add this code
settingTableView.tableHeaderView = headerView1;
Suggestion1 : You could have create a separate view which contains your UILabel and place above the UITableView and place your tableView y position would be from the height of the UIView.
Remark : This is useful because when you scroll the tableView the default header will be stick to top.
Suggestion2 : you can use viewForHeaderInSection delegate method. where you can create a view and add the UILabel. viewForHeaderInSection returns the UIView, which you can return your view which contains the UILabel
Remark : when you scroll the tableView the default header will move along with your tableView

How to customize the UISearchBar style

Does anyone know how to customize the UISearchBar style? There are 3 default styles of the UISearchBar, but I want to customize other aspects of the search bar, such as its image, background. If there is no way to do that can I use the UITextField to replace the search bar's function?
Thanks All
As you discovered, you can customize a bar such as a search bar with the barStyle property and these two values:
UIBarStyleDefault
UIBarStyleBlack
Alternatively, you can choose a custom color for the bar using the tintColor property:
// use RGB values between 0.0 and 1.0
UIColor *barColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
searchBar.tintColor = barColor;
In either case, you can make the bar translucent by setting the translucent property to YES.
UISearchBar is a subclass of UIView, therefor you can simply add an UIView on top. (You just have to keep the area of the actual searchbar transparent.)
// Edit
It's easier if you add the image to the background (between the background and the search field, that is). To do this just add it and iterate over the subview to send the original background to the back (behind your image view).
searchBarOverlay = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SearchBarBack"]];
searchBarOverlay.frame = CGRectMake(-8, -2, 320, 48);
[searchBar addSubview:searchBarOverlay];
[searchBar sendSubviewToBack:searchBarOverlay];
for (UIView *v in [searchBar subviews]) {
if ([NSStringFromClass([v class]) isEqualToString:#"UISearchBarBackground"])
{
[searchBar sendSubviewToBack:v];
}
if ([NSStringFromClass([v class]) isEqualToString:#"UIImageView"] && v != searchBarOverlay)
{
[searchBar sendSubviewToBack:v];
}
}
For UISearchBarStyleProminent:
1) Be sure to check the "Translucent" box for the search bar in the Attributes Inspector.
2) Add the following to viewDidLoad:
self.navigationController.navigationBar.translucent = NO; // If navBar
self.searchDisplayController.searchBar.translucent = NO;
In order to get the minimal search bar to not be translucent I have put together a workaround.
1) Be sure to check the "Translucent" box for the search bar in the Attributes Inspector.
2) Add the following code to viewDidLoad:
self.navigationController.navigationBar.translucent = NO;
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.backgroundColor = [UIColor desiredColor];
3) A UIView needs to be added to the viewController. This view needs to be 20px heigh and should have the same color as the
searchBar.barTintColor.

iPhone + add controls dynamically

I want to add UILabel controls to my application dynamically from code (programmatically), please help me
You can do it the following way in one of the view controller methods:
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(...)]; // Create UILabel
myLabel.tag = someTag; // set label's tag to access this label in future via [view viewWithTag:someTag];
... // set label's properties like text, background and text color, font size etc (e.g. myLabel.textColor = [UIColor redColor];)
[view addSubView:myLabel]; // add label to your view
[myLabel release]; // view owns the label now so we can release it