I’m using setTitleTextAttributes to change the font on a UISegmentedControl as follows.
UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
[self setTitleTextAttributes:attributes forState:UIControlStateNormal];</pre></code>
This works fine in the simulator, but crashed when running on an iPad. The bad access fault occurs on creation of the dictionary and I used an NSLog statement to verify that UITextAttributeFont is generating the fault.
Any ideas what the problem is here or if there is an alternative method to set the font?
EDITED
I just realized the iPad is running iOS 4 (I believe this is supported on iOS >= 5). What is the best way to test for version support?
Based on Dirty Henry’s suggestion, the correct implementation is as follows.
if ([self respondsToSelector:@selector(setTitleTextAttributes: forState:)]) {
UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: font, UITextAttributeFont, nil];
[self setTitleTextAttributes:attributes forState:UIControlStateNormal];
}
Is your iPad running iOS 5.0 or later? (UITextAttributeFont has been introduced in this version). (i don’t see anything wrong with your code either)