无法为UITableViewCell中的Label设置Century Gothic字体求助
Hey there, let’s work through this frustrating font issue together! It’s so annoying when you’ve tried both Storyboard and code to set a custom font, but it still sticks to the default Avenir. Here are the key fixes to get Century Gothic working properly:
1. Double-Check Font Import & Info.plist Setup
First off, make sure the font is actually properly added to your project:
- Confirm the Century Gothic font file (
.ttfor.otf) is in your Xcode project, and that its Target Membership is checked for your main app target (you can find this in the File Inspector on the right when selecting the font file). - Open your
Info.plistand verify you have theFonts provided by applicationkey (add it if missing). Add the exact filename of your Century Gothic font(s) to the array under this key—pay close attention to capitalization and spacing (e.g.,CenturyGothic.ttforCenturyGothic-Bold.ttf). Typos here are one of the most common causes of font failures.
2. Use the Correct Font Name (Not Just the Filename)
The system’s internal name for the font might not match the filename or the display name you see in Storyboard. To find the exact name Century Gothic uses:
Add this quick debug code to your app’s launch cycle (like application(_:didFinishLaunchingWithOptions:) or a view controller’s viewDidLoad()):
for family in UIFont.familyNames.sorted() { let fontNames = UIFont.fontNames(forFamilyName: family) print("Family: \(family) → Fonts: \(fontNames)") }
Run your app, look for "Century Gothic" in the family list, and use the exact font name from the output (it might be CenturyGothic without spaces). Update your code to use this name, e.g.:
cell.foodTitle.font = UIFont(name: "CenturyGothic", size: 58)
3. Troubleshoot Storyboard Attributed Text Settings
If you’re using Attributed Text in Storyboard:
- Double-check that you’ve selected the correct Century Gothic variant (not a similar-named font) in the font picker.
- Clean your project (press
Cmd+Shift+K) and rebuild—sometimes Storyboard caches old font settings. - Ensure there are no Font Overrides in the Size Inspector that might be overriding your custom font selection.
4. Make Sure Code Settings Aren’t Being Overridden
Check your custom UITableViewCell1 class:
- Look for any code in
awakeFromNib()orlayoutSubviews()that sets thefoodTitlefont—this could be overwriting the font you set incellForRowAt. - Verify that your
foodTitleIBOutlet is connected to the correct label in the cell, not a different one.
5. Test Font Loading Success
Add a quick check to confirm if the font is loading properly in your cellForRowAt method:
if let customFont = UIFont(name: "CenturyGothic", size: 58) { cell.foodTitle.font = customFont } else { print("⚠️ Failed to load Century Gothic font!") // Fallback to a system font if needed cell.foodTitle.font = UIFont.systemFont(ofSize: 58) }
If you see the error print statement, go back to steps 1 and 2—your font isn’t being recognized by the app.
内容的提问来源于stack exchange,提问作者kitchen800

