UINavigationItem#titleView alignment problems - iphone

I am finding little information about using UINavigationItem#titleView with a custom font.
When I have done so, the font is misaligned vertically in the navigation bar.
This entry is partly to document a hack, and also hoping someone has a succinct answer to this problem, as I feel I am missing something simple.
First the hack, using my own UILabel derived class:
#interface NavigationItemLabel : UILabel
- (void)setFrame:(CGRect)frame;
#end
#implementation NavigationItemLabel
- (void)setFrame:(CGRect)frame {
// Called by UINavigationBar layoutSubviews.
frame.origin.y -= self.font.descender;
}
#end
For some reason, frame.origin.y == -11, no matter what font I use.
Does anyone have any intuition as to why this is?
Adding my font's descender (custom font called Gabriola) seems to help. Without this hack, the text is aligned with the bottom of the descenders on the center of the navigation bar.
This doesn't work for all fonts.
Does anyone have a better solution?
Thanks.

If you are deploying to iOS 5+, you could check out the titleVerticalPositionAdjustmentForBarMetrics property the in the UINavigationBar documentation:
- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics
Returns the title’s vertical position adjustment for given bar metrics.
Also, you should check out titleTextAttributes (also iOS5+), which you can use to set a custom font:
NSDictionary *attributes = #{
UITextAttributeFont: yourFont
};
[navBar setTitleTextAttributes:attributes];
If you're NOT on iOS5, I'd suggest wrapping the UILabel inside a UIView and setting your UIView as titleView instead (this allows you to further adjust the wrapped label's position by changing its frame).
If you have access to the WWDC 2012 videos (i.e. if you've got a developer account), I'd strongly recommend watching the talk on Advanced Appearance Customization on iOS (this stuff is included there).
Hope it helps somehow.

I had the same problem using Grotesque and there was a great SO solution shown by #kolyuchiy here. (It involves downloading the Apple Font Tool Suite command line utilities and adjusting the ascender attribute for your custom font).

Related

How to set views frame without auto layout

With the release of iOS 6 I changed my applications storyboard to use the new auto layout feature.
My problem is that I set a tableview frame in one view controller - but when I use the auto layout feature the table.frame = CGRectMake(..) code seems to get ignored.
My app should change the tableview frame if there is iAd or no iAd.
I dont know if i can solve this problem with auto layout - maybe you can help me.
greetings from germany :)
I know this is old but with the move to iOS7 I felt it was time to activate auto-layout for my own apps. I ran across this and found that I could simply turn auto-layout back on and put your table.frame = CGRectMake(..) code into:
- (void) viewDidAppear:(BOOL)animated
{
table.frame = CGRectMake(..)
}
Don't use the auto layout and remove the UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleheight in the xib. Let me know if you didn't get it.
And use the set frame property in the code.

UIBarButtonItem - Invalid tap glow size

I have a strange problem with glow that appears when pressing on a UIBarButtonItem in the UIToolbar control. When I set ImageInsets property to 4,0,0,0 the glow gets smaller every time I tap on it. Here is an illustration:
The problem doesn't appear if I don't set imageInsets. The problem appears for all buttons in the UIToolbar. I don't have tap handlers. Making bigger inset (e.g. 8,0,0,0) produces the same result faster.
I appreciate any suggestions on how to solve the problem.
EDIT: Changed the code to Objective-C since the problem reproduces without MonoTouch as well.
It's default single view project. I added a toolbar and a UIBarButtonItem into it using storyboard designer. Created an outlet for the button.
#import "ViewController.h"
#implementation ViewController
#synthesize testBtn;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UIEdgeInsets insets = UIEdgeInsetsMake(8, 0, 0, 0);
[testBtn setImageInsets:insets];
}
- (void)viewDidUnload
{
[self setTestBtn:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
It is hard to see what the problem is without code. Would you mind posting the relevant parts of your code up? I am thinking it has something to do with you repeatedly incrementing the top inset by 4... I will edit my answer once I see the relevant code.
EDIT: So I am not familiar with Monotouch... and from the code you pasted I am assuming the constructor does indeed get called once. I am thinking there may be a bug in the Monotouch framework which is causing the imageinsets to be shifted by the specified amount (down 4) every time the button is tapped. I would check in the following order:
That the delegate method tied to the button is not pointing to the constructor.
Change the inset to (8,0,0,0) and in 5 taps do you see the same image as above (10x). If so it may be an issue with the monotouch framework or with how you hooked up your delegate method.
Sorry but I am unfamiliar with monotouch so I can't provide much more assistance. Unless you are locked into using Monotouch, I would strongly recommend you learn native Objective-C and program natively to avoid small pitfalls and headaches such as this. Objective-C and the iOS SDK is pretty elegant.
Finally, I asked designer to adjust image size and thus get rid of the imageSize property use.

UILabel text moved

I have a UITextView in which it has a place holder text in it, however, when I tried to type in something to the UITextView it moved my UILabel text to the right a bit. Why is this? I can't seem to figure out how this could happen. Here's a video showing the issue.
I am using SSTextView from Sam Soffle's SSToolkit here for the UITextView.
All the other is pretty much standard. Can anyone tell me why is this?
Not sure if it helps, but can it be due to:
- (void)textViewDidChange:(UITextView *)textView
{
[self.textCount setText:[NSString stringWithFormat:#"%3d/142", textView.text.length]];
}
[self.textCount setText:[NSString stringWithFormat:#"%3d/142", textView.text.length]];
When you first init your TextField do you have the same formatting?
If you have set in in IB, remove it from there and set it in code with that formatting.
But maybe a simpler solution would be to justify your text to the right and place your TextField accordingly, so you wouldn't event have to deal with the place holder.
Have you log out the frame information of that UILabel to see if it's really moving?
I'm not sure the problem is that the UILabel is moving.
The moving occur when you first modify it's text. This make me think that the problem maybe something else.
How, where and when are set the text property of that UILabel may hold the key to that mystery.

Cocoa Touch UITextView setText: method useless

I have a UITextView which I create in Interface Builder for an iOS app. I checked (and double checked, and triple checked) that it is properly connected to the outlet in my code. I create a property and synthesize it, and when I run NSLog(#"%#", myTextView);, it returns the UITextView's properties, not null. However, when I try calling [myTextView setText:#"My Text."];, it doesn't change anything on the view. I have found many people with similar problems like this on Stackoverflow and other places on the internet, however none of the solutions there helped me. I am using Xcode 4.0.2 on Snow Leopard. I am attempting to do this in the -viewDidLoad method. What could be causing this issue?
If you need any more information about my code setup, post a comment and I will update this post.
Are you sure the whole text view is visible on screen, and that the font color is not the same as your background?
It is very likely that the view that is displayed on the screen is not the same view as myTextView points to. Check [myTextView superview] and make sure it's onscreen. Then look and see if you've accidentally covered it with something. Change other aspects like the backgroundColor. Also, check the frame and make sure it's not CGRectZero.

Custom font in iOS

Ok,
using a custom font since 3.2 is pretty straightforward:
Load the font in resources bundle, specify it's name in plist and there you go, you can use is in a text field with [textField setFont...].
The thing is, i want to replace the font in every part of my app, say the navigation bar so it gets complicated, because it seems i have to replace every component with a custom one in order to set the font.
Is there any way to let the bloody thing know that i want the default font replaced with a custom one, in one step? Not in every bar item, and so...
Thanks!
Maybe a category on UIFont which redefines
+ (UIFont *)systemFontOfSize:(CGFloat)size {
return [self fontWithName:#"YourFontName" size:size];
}
+ (UIFont *)boldSystemFontOfSize:(CGFloat)size {
return [self fontWithName:#"YourBoldFontName" size:size];
}
to return your custom font.
As pointed by Jonathan in the comments, it may also change the status bar font (I can't verify at the moment). I don't know if Apple would accept this behavior (redefining methods is OK but modifying the default UI isn't IMHO).
Subclassing UIFont wouldn't be sufficient because labels and others would still use the UIFont class by default.