EXC_BAD_ACCESS on UITextAttributeFont (Invalid iOS version) - iphone

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)

Related

NSMutableAttributeString not working in iOS 10.0 in UIAlertController

Hello am using NSMutableAttributedString to show different fonts but with same size in UIAlertController, the problem here is it showing two different fonts which is correct but it also showing two different sizes.Here is my code
UIFont *regularFont = [UIFont fontWithName:#"HelveticaNeue" size:11.0];
UIFont *boldFont = [UIFont fontWithName:#"HelveticaNeue-Bold" size:11.0];
UIAlertController *alertObj = [UIAlertController alertControllerWithTitle:nil message:messageStr preferredStyle:UIAlertControllerStyleAlert];
NSMutableAttributedString *attMessage = [[NSMutableAttributedString alloc] initWithString:messageStr];
[attMessage addAttribute:NSFontAttributeName value:regularFont range:NSRangeFromString(messageStr)];
[attMessage addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(25, 6)];
[attMessage addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(35, 15)];
[alertObj setValue:attMessage forKey:#"attributedMessage"];
Result can be match.Any help would be appreciated
The attributedMessage property is not public, using it is a good way to get your app rejected at the review. If you really need to style the dialogue and distribute the app on the App Store, I think you should build your own component to display the pop-up.

UIAppearance's setTitleTextAttributes causes UINavigationBar title set in storyboard to appear with ellipsized title in iOS 6 but not iOS 5

i have been using what i had thought to be some fairly standard iOS 5 code (and which is a 7x accepted answer on stackoverflow) in order to have my UINavigationBar's title appear with a customized font.]
but i have discovered that when my app built for iOS5 (so that it can run on both iOS5 and iOS6 devices) is run on the iOS6 simulator, it ellipsizes my title.
the following is the same code running on the iOS 5.1 simulator.
for clarity, the following is the pertinent code that's been working until i tried running on iOS 6.
#define COURIER_FONT_BOLD #"CourierNewPS-BoldMT"
- (UIFont*)navigationBarTitleFontPortrait
{
if (!_navigationBarTitleFontPortrait)
_navigationBarTitleFontPortrait = [UIFont fontWithName:COURIER_FONT_BOLD size:24.0];
return _navigationBarTitleFontPortrait;
}
// …
if ([[UINavigationBar class] respondsToSelector:#selector(appearance)])
{
NSDictionary* attrs = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor lightGrayColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(1, 0)],
UITextAttributeTextShadowOffset,
navigationBarTitleFontPortrait,
UITextAttributeFont,
nil];
[[UINavigationBar appearance] setTitleTextAttributes:attrs];
}
i discovered two solutions to the above problem:
set the title text to something of a different size in the
.storyboard (in my case, i entered "...", and then in viewDidLoad,
call self.navigationItem.title = #"the GRID";
in viewDidLoad, perform [self.navigationController.navigationBar
setNeedsLayout].
i prefer the latter because it feels less like a kludge. i am guessing the first solution forces the second to happen, anyway.
don't know why this behavior is different between iOS 5 and iOS 6, but this allows it to work in both.

How do you change font in UISearchBar for iOS5?

I'm trying to change the font for all of my UISearchBar objects using the new "appearance" proxy for iOS5 with something like:
[[UISearchBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont fontWithName:#"Trebuchet MS" size:0.0], UITextAttributeFont,
nil]];
Every time I run this, I'm getting this error:
"2012-05-28 03:01:52.264 DirectDx_ClientApp[30039:15503] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIAppearance setTitleTextAttributes:]: unrecognized selector sent to instance 0x8460cf0'
* First throw call stack:
(0x1ea8022 0x3a4fcd6 0x1ea9cbd 0x1e0eed0 0x1e0ecb2 0x5e695 0x5dd78 0x217aa 0x789386 0x78a274 0x799183 0x799c38 0x78d634 0x203bef5 0x1e7c195 0x1de0ff2 0x1ddf8da 0x1dded84 0x1ddec9b 0x789c65 0x78b626 0x2164d 0x2895 0x1)
terminate called throwing an exception"
The method above works perfectly well with UITabBar and UINavigationBar.
Any insights?
Thanks very much in advance.
Try this,It's Working Fine for iOS 5.0 and up (iOS 7 also):
- (void)viewDidLoad
{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"Helvetica" size:20]];
}
For iOS 8
- (void)viewDidLoad
{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:20],
}];
}
Checkout the apple reference guide for uisearchbar, under customizing appearance you can see what methods are avialable to you for a uisearchbar. SetTitleAttributes is not possible for a uisearchbar and I can not see another method where you change the font of the uisearchbar by means of appearance.
Trying to call a method that is not supported by an object, will always give you an error. In your case setTitleArtributes is supported by other classes but sadly enough not for the uisearchbar.
There is a workaround.
Try using this code
UITextField *textField = [self.searchBar valueForKey: #"_searchField"];
[textField setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17.0]];
Hope this will work for u.

Changing placeholder Font of a textfiled in iphone sdk

How to change the Font of the Placeholder of the text filed?
Is this possible to change the default font of the Placeholder of the text filed?
If anyone know it please help me.
Thanks.
Create a subclass of UITextField and overwrite the drawPlaceholderInRect:
- (void) drawPlaceholderInRect:(CGRect)rect {
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16.0]];
}
#fannheyward's answer was good. However, – drawInRect:withFont: is deprecated in iOS 7.0. In iOS 7, you should use:
- (void) drawPlaceholderInRect:(CGRect)rect {
UIFont *font = [UIFont fontWithName:#"STHeitiTC-Light" size:14.0];
[self.placeholder drawInRect:rect withAttributes:#{NSFontAttributeName: font}]
}
please also note that the "real" font name may be different from the one it shows on interface builder. You can use the following code to get the "real" name of available fonts in your project:
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"%#", fontName);
}
}

How change font of uilabel?

I want to change the font of label. And font which i am using is shown in image.
How use it in my application. I have already add in .plist as show in image. But it not working proper. How i manage it?
Thanks in advances...
Use this to set the font programmatically:
[TheLabelName setFont:[UIFont fontWithName:#"American Typewriter" size:18]];
Custom fonts in IOS
to set an label font just
yourLabel.font = [UIFont fontWithName:#"CloisterBlack" size:64.0];
If you want to use the standard font, then just as usually: you can set it in the interface builder for this label or programmatically for this label.
If you want to apply your custom font (according to the image you want that), then you can do smth. like
UIFont *font = [UIFont fontWithName:#"MyFont" size:20];
[label setFont:font];
Also you can explore this ref:
Can I embed a custom font in an iPhone application?
it might be useful in your case.
Add your custom font file .ttf in resourse and use every time when you want to display formatted font like
headLbl.font = [UIFont fontWithName:#"Museo-700" size:20.f];
Add a font in your resource directory like this image that you have already done now main thing you cann't wirte the name of the font that you have added as it is check my image font having name ROCKB.ttf but i have write the Rockwell-Bold so you have to check by what name it has been installed in your code then pick the name from there and then put that name in lable the font will reflect the label now.
label.font = [UIFont fontWithName:#"Rockwell-Bold" size:20];
and font family by this code NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
NSLog(#"Family name: %#", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
{
NSLog(#" Font name: %#", [fontNames objectAtIndex:indFont]);
}
[fontNames release];
}
[familyNames release];