Can I define the bold in Settings> Availability?

With iOS 7, you can program your application to match your user preference for Dynamic Type — a larger or smaller font size. You use the method preferredFontForTextStyle:, and then listen for notifications to update the user interface if the user changes settings while your application is running. I'm wondering if you can do the same with the Bold Text accessibility setting, which is located in Settings> Accessibility. I realized that the Bold Text option actually requires a reboot of the device, so there is no need to listen to notifications, because your application will be killed and overwritten anyway.

This is what I ultimately want to accomplish: . I would like to change the title bar text of the navigation bar to a lighter font. It cannot be the default system font - it can be any font that iOS can display, but I will probably use HelveticaNeue-Light. I would also like to respect user preferences for bold text. If it is turned on, I want to change the title text to a heavier weight of the same font - exactly the same as the default iOS, although the default value is already quite heavy - Helvetica Neue Medium. Indeed, when turned on, it is a little heavier. I want to do the same with a different font.

Here is what I am doing to change it, but this will obviously be fixed no matter what the bold is:

[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"HelveticaNeue-Light" size:17],  [NSFontAttributeName, nil]];

I may have a solution, but it seems like a bad approach. I am creating a new font with a fixed size from preferredFontto subheadline. This is almost what I want - it automatically takes care of the font based on the Bold Text value (HelveticaNeueRegular [I really want Light] when disabled, HelveticaNeueMedium when it is turned on), but will not work for another font. Perhaps there is a better approach?

UIFont *subtitleFont = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
UIFont *titleFont = [subtitleFont fontWithSize:17];
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:titleFont, NSFontAttributeName, nil]];
+4
source share
4 answers

As in iOS 8, you can determine whether the user has enabled Bold Text in the settings using UIAccessibilityIsBoldTextEnabled()( docs ) and UIAccessibilityBoldTextStatusDidChangeNotification( docs ).

, iOS 7, Ive , iOS 7 8 Helvetica Neue iOS 9 San Francisco, , "" , :

Objective-C:

BOOL hasBoldText = ![[UIFont preferredFontForTextStyle:UIFontTextStyleBody].fontName hasSuffix:@"-Regular"];

Swift:

let hasBoldText = !UIFont.preferredFontForTextStyle(UIFontTextStyleBody).fontName.hasSuffix("-Regular")
+4

UIFontDescriptor :

UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleSubheadline];
UIFont *font = [UIFont fontWithDescriptor:fontDescriptor size:17]; // better to use a constant

, UIApplicationContentSizeDidChangeNotification. , , applicationWillEnterForeground:. 99% , , , .

+1

. , , 'bold', , , Bold Text , . , , Apple . , , Medium Bold. Apple , , . .

UIFont *currentTitleFont = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
//if Bold Text is disabled
if ([currentTitleFont.fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location == NSNotFound) {
    UIFont *titleFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:17];
    [self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:titleFont, NSFontAttributeName, nil]];
}
else {
    //put custom font here for when Bold Text is enabled, or do nothing to get the default
}
0

iOS 7 , UIFontTextStyleHeadline: HelveticaNeueInterface-Heavy.

op :

UIFont *currentTitleFont = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
//if Bold Text is disabled
if ([currentTitleFont.fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location == NSNotFound && [currentTitleFont.fontName rangeOfString:@"heavy" options:NSCaseInsensitiveSearch].location == NSNotFound) {
    UIFont *titleFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:17];
    [self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:titleFont, NSFontAttributeName, nil]];
}
else {
    //put custom font here for when Bold Text is enabled, or do nothing to get the default
}
0

Source: https://habr.com/ru/post/1538318/


All Articles