Change the titleLabel of UIButton which title type is Attributed in Storyboard / Xib file - nsattributedstring

Issue :
I have use below code to change Title and set underline of UIButton at run time.
[btnTemp setTitle:#"Test" forState:UIControlStateNormal];
I have got below issues:
Underline is not showing
Title not change
Title color is display default black.
To overcome this issue I have implement below code:
NSDictionary *attribs = #{NSForegroundColorAttributeName: _btnTemp.titleLabel.textColor,
NSFontAttributeName: _btnTemp.titleLabel.font,
NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)
};
NSAttributedString *strAttributed = [[NSAttributedString alloc] initWithString:#"Test" attributes:attribs];
[btnTemp setAttributedTitle:strAttributed forState:UIControlStateNormal];

Related

TitleView of UITableViewController's UINavigationItem image not visible

I am using Swift 2.3 in Xcode 7.3. I have a UINavigationController with a UITableViewController. I am trying to set the titleView of the navigation item in the navigationBar to create a button centered in the navigation bar.
The indexTextBut is a UIButton hooked up from the story board. I have tried just creating a new button and it made no perceivable difference. The way I am currently doing it the button is there as it has the behavior it should but no image. I have tried just setting the title field as a string and even then I see nothing. I set the right and left barButtonItems using:
navItem.setRightBarButtonItems(barButtonItems, animated: false)
navItemArray.append(navItemCatDet)
self.navigationController?.navigationBar.setItems(navItemArray, animated: false)
Here is my relevant code. My big question is why can't I see the image of the button being set to the titleView. It has preset behavior and when I click where it should be I get that behavior. I've tried adjusting the frame of the UIButton and its UIImageView, and the size of the UIImageView's image but have had no luck. I included the line aobut tinting the background just incase it is related even though I have tried it with and without.
class MyTableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var indexTextBut: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
var indexNavItemArray = [UINavigationItem]()
var indexNavItem = UINavigationItem()
indexNavItem.titleView = newButton
indexNavItemArray.append(indexNavItem)
self.navigationController?.navigationBar.barTintColor = UIColor(netHex:0xe63246)
self.navigationController?.navigationBar.setItems(indexNavItemArray, animated: false)
swift 3.0
var indexNavItemArray = [UINavigationItem]()
var indexNavItem = UINavigationItem()
indexNavItem.titleView = indexTextBut
indexTextBut.setTitle("button", for: .normal)
indexNavItemArray.append(indexNavItem)
self.navigationController?.navigationBar.barTintColor = UIColor.lightGray
self.navigationController?.navigationBar.setItems(indexNavItemArray, animated: false)
replace your code to above.

iOS7 UIImagePickerController cancel button disappear

The Cancel button is miss?! How can I fix this? Thank you very much.
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
{
if(buttonIndex == 1)
{
self.ctr = [[UIImagePickerController alloc] init];
self.ctr.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.ctr.delegate = self;
self.ctr.allowsEditing = YES;
[self presentModalViewController:self.ctr animated:YES];
}
}
Just change the UIImagePickerController navigationBar.tintColor, it should be OK.
self.ctr.navigationBar.tintColor = [UIColor redColor];//Cancel button text color
[self.ctr.navigationBar setTitleTextAttributes:#{UITextAttributeTextColor: [UIColor blackColor]}];// title color
Looks like apple made some mistake with it (iOS 10, Xcode 8) because just changing tint color of UIImagePickerController could not be done, cause, before controller isn't have topItem property, or navigationController property. So have done the changes in UIImagePickerController extension. But I checked navigationController and topItem in those overrided methods: viewDidLoad, viewWillAppear, viewDidAppear. but it still was nil. So i decide to check it in viewWillLayoutSubviews, and voila! It's wasn't nil, so we can set bar tint color of exact rightBarButtomItem here!
Here is example:
extension UIImagePickerController {
open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.black
self.navigationBar.topItem?.rightBarButtonItem?.isEnabled = true
}
}
And don't forget to call super.viewWillLayoutSubviews, it's very important ;-)
EDIT: But it still has problems when return to the albums screen..
Change the tintColor
self.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.black
If that doesn't work run through your view controllers to see if there isn't a place where you changed the appearance of the navigation bar and reset the change.

Set Different Navation bar title for different View controller in iphone

I have navigation bar i am setting title on image which is used in navigation bar it works fine but when i use same code for other controller it shows same previous title on navigation bar not the new one.
here is the code
UIImage *image = [UIImage imageNamed:#"Nav.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(50,2,250,36)];
titleLabel.text=#"Activity";
titleLabel.textColor=[UIColor whiteColor];
titleLabel.backgroundColor=[UIColor clearColor];
titleLabel.font=[UIFont fontWithName:#"Helvetica-Bold" size :18];
[self.navigationController.navigationBar addSubview:titleLabel];

UINavigationItem multiple line prompt text

Can any body give me the solution for displaying UINavigationItem prompt text in 2 lines?
There is not a built-in way to do this. Below is a work-around that seems to work pretty well that I put together from stackOverflow post UINavigationItem with prompt and activity indicator
Here is a simulator screen shot of what it creates:
Note that since the text is a UILabel you can modify its color, font, or anything else too.
// I have this code in viewDidLoad
UIView *viewContainingPrompt;
UIBarButtonItem *promptButtonItem;
// Configuring the prompt title of the navigation bar so it is present but empty
[self.navigationItem setPrompt: #""];
// We will create a UIBarButtonItem that has a custom view (viewContainingPrompt).
// A subview of viewContainingPrompt will be a UILabel (headerLabel)
// We need to have this "intermediate" view to position the label at the right position
// (the UIBarButtonItem ignores the origin and height of its custom view)
viewContainingPrompt = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 0, 85)];
viewContainingPrompt.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Choose a width that puts 10 points on either end...
CGFloat labelWidth = self.navigationController.navigationBar.bounds.size.width - 20.0;
// Note that the '-60' below is determined by the width of the back button
// If someone can figure out how to determine this width at runtime this code
// would be much more robust.
UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(-60,-8,labelWidth,36)];
headerLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
headerLabel.text = #"A quite long prompt string that will wrap to a second line to demonstrate multiline prompt.";
headerLabel.font = [UIFont systemFontOfSize: 14];
headerLabel.numberOfLines = 0; // Zero gives as many lines as will fit, could be 2
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor colorWithRed: .1 green: .1 blue: .2 alpha: 0.8f];
headerLabel.shadowColor = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 0.5f];
headerLabel.shadowOffset = CGSizeMake( 0, 1 );
headerLabel.textAlignment = UITextAlignmentCenter;
[viewContainingPrompt addSubview: headerLabel];
//[headerLabel release]; // Uncomment if not using ARC
promptButtonItem = [[UIBarButtonItem alloc] initWithCustomView: viewContainingPrompt];
self.navigationItem.leftBarButtonItem = promptButtonItem;
self.navigationItem.leftItemsSupplementBackButton = YES;
//[viewContainingPrompt release]; // Uncomment if not using ARC
//[promptButtonItem release]; // Uncomment if not using ARC
I would appreciate anyone's feedback on how to figure out the width of the back button during execution so that width did not have to be hard coded.
As it is I do not think there are any private APIs or other illegal code contained.

Use custom background with UINavigationController default UIToolbar

I want to use my self background for my UIToolbar.
To do that with the UINavigationBar I've just used this category with the override of the drawRect method:
#implementation UIToolbar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"nm010400.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
This piece of code works perfectly for the UINavigationBar but this does not work for the UIToolbar that is inncluded in the UINavigationController and enabled with this line o code:
self.navController.toolbarHidden = NO;
Can anyone help me with the problem? I' m sure that the UINavigationController use a standard UIToolbar so why this not works?!? thanks
To create custom UI elements there is no need to create categories anymore. With IOS 5 you can change the appearance of any UI element through the UIApperance protocol. You can change individual UI elements as normal but the real power comes when you are creating a themed application. Using this new protocol you can change the style of all instances of a particular UI element or even a UI element in a particular setting such as a UIBarButtonItem in a UINavigationController.
To change the appearance of all of the UINavigationBars in your application you can set the background like so,
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"myNavBar.png"] forBarMetrics:UIBarMetricsDefault];
you change different attributes depending on which element you are using to change the title attributes of the UINavigationBar you can use,
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont fontWithName:#"MarkerFelt-Thin" size:24], UITextAttributeFont, nil]];
you can also use this on other UI elements so to change the background on all instances of the UIToolbar you can call
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"myToolBar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Hope this helps.
In your custom UINavigationController's viewDidLoad use the following code:
[self.toolbar setBackgroundImage:[UIImage imageNamed:#"mytoolbarbg.png"]
forToolbarPosition:1
barMetrics:UIBarMetricsDefault];

Resources