Showing posts with label setTitle. Show all posts
Showing posts with label setTitle. Show all posts

Sunday, June 9, 2013

UINavigationItem setTitle example in Objective C (iOS).


UINavigationItem setTitle

The navigation item’s title displayed in the center of the navigation bar.

@property(nonatomic, copy) NSString *title

Discussion of [UINavigationItem setTitle]
The default value is nil.

When the receiver is on the navigation item stack and is second from the top—in other words, its view controller manages the views that the user would navigate back to—the value in this property is used for the back button on the top-most navigation bar. If the value of this property is nil, the system uses the string “Back” as the text of the back button.

UINavigationItem setTitle example.
I've set the title programatically using code something like this:

navBar.topItem.title = @"title";
where navBar is declared as an IBOutlet UINavigationBar linked to the navigation bar in interface builder. This worked in my app; however, I was not using a tab bar.

Example of [UINavigationItem setTitle].
CGRect navBarFrame = CGRectMake(0, 0, self.tableView.frame.size.width, 44.0);
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:navBarFrame];

UINavigationItem *navItem = [UINavigationItem alloc];
navItem.title = @"Your Title";

[navBar pushNavigationItem:navItem animated:false];

UINavigationItem setTitle example.
@synthesize navBar;
-(void)viewWillAppear:(BOOL)animated {
[self.navBar setTitle:@"Sign In"];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}

End of UINavigationItem setTitle example article.

Thursday, May 30, 2013

UIButton setTitle example in Objective C (iOS).

UIButton setTitle

Sets the title to use for the specified state.

- (void)setTitle:(NSString *)title forState:(UIControlState)state

Parameters
title
The title to use for the specified state.
state
The state that uses the specified title. The possible values are described in UIControlState.

Discussion of [UIButton setTitle]
Use this method to set the title for the button. The title you specify derives its formatting from the button’s associated label object. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.[UIButton setTitle]

At a minimum, you should set the value for the normal state. If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state. If the value for UIControlStateNormal is not set, then the property defaults to a system value.

UIButton setTitle example.
UIButton *backbtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 68, 38)];
[backbtn setTitle:@"Your string" forState:UIControlStateNormal];
//[backbtn setImage:[UIImage imageNamed:@"BackBtn.png"] forState:UIControlStateNormal];
[backbtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backbtn];

Example of [UIButton setTitle].
MyViewcontroller *myView = [[MyViewcontroller alloc] initWithNibName:@"MyViewcontroller" bundle:nil];
[scrollView addSubview:myView.view]; // view is loaded
[myView.imageButton setTitle:@"ddd" forState:UIControlStateNormal]; // imageButton is now wired

UIButton setTitle example.
UIButton *dateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dateButton setFrame:CGRectMake(240.0, 57.0, 60.0, 30.0)];
[dateButton setTitle:@"Date" forState:UIControlStateNormal];

[dateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[dateButton setBackgroundImage:[UIImage imageNamed:@"bg_headline.png"] forState:UIControlStateNormal];
[dateButton addTarget:self action:@selector(GoToDateSettings:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dateButton];

End of UIButton setTitle example article.

Wednesday, April 17, 2013

UIButton setTitle forState: example objc


setTitle forState
Sets the title to use for the specified state.
- (void)setTitle:(NSString *)title forState:(UIControlState)state

Parameters
title
The title to use for the specified state.
state
The state that uses the specified title. The values are described in UIControlState.

Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the value for UIControlStateNormal is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

Example for ( setTitle:forState: )
{
    NSString            *title = @"My button title";
    UIColor              *titleColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
    UIImage             *bgImage = [UIImage imageNamed:@"yourImage.png"];

    UIImage             *highlightedBgImage = [UIImage imageNamed:@"yourImage.png"];
    UIButton            *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    // Create button.
    //
    button.bounds = CGRectMake0.0f0.0f, bgImage.size.width, bgImage.size.height );
    button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0f];
    button.titleLabel.adjustsFontSizeToFitWidth = YES;
    button.titleLabel.minimumFontSize = 10.0f;
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateHighlighted];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateHighlighted];
    [button setBackgroundImage:bgImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightedBgImage forState:UIControlStateHighlighted];

    // You must specify your target object and action selector here.
    // 
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

}