How set custom fonts for iOS13 context menu actions?
Asked Answered
L

2

6

I want to customize the action buttons on context menu for iOS 13+

Menus are something like this:

enter image description here

I thought I could set the custom font like UIAlertController actions like this:

let action = UIAction(title: "asdasd") { _ in}
let attributeString = NSMutableAttributedString(string: action.title)
attributeString.addAttributes([NSAttributedString.Key.font : font], range: NSMakeRange(0, action.title.count))
action.setValue(attributeString, forKey: "attributedMessage")

But its not work like that, so how is it?

Logwood answered 8/4, 2020 at 8:49 Comment(2)
No. although I wanted to use a custom font...Logwood
So, Apple will not allow any UIMenu/UIAlert customization?Shortcake
G
5

You can't force it to use a specific font and or text size. It actually supports dynamic text. So its size will depend on the user device settings. Settings > Display & Brightness > Text Size and Settings > Accessibility > Larger Text (iOS 14). So yon can't use a custom font because context menu supports dynamic text. The font used by dynamic text depends on the system therefore San Francisco has been the font used by Apple since iOS 9. For more info about Dynamic Type in iOS you can read this article.

Gynecic answered 11/7, 2020 at 16:48 Comment(0)
E
0

In iOS 17 it works if you set _attributedTitle as the key and using UIFont preferredFontForTextStyle as the font size:

CGFloat pointSize = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline].pointSize;
UIFont *font = [UIFont fontWithName:@"font name" size:pointSize];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[action title]];
[string addAttributes:@{NSFontAttributeName:font} range:NSMakeRange(0, [[action title] length])];
[action setValue:string forKey:@"_attributedTitle"];
Edgar answered 13/8, 2023 at 10:22 Comment(1)
It's never a good idea to use setValue to set undocumented private properties. Such code can crash or fail to function in any future iOS update.Kidron

© 2022 - 2025 — McMap. All rights reserved.