[iOS] UIFont 设置字体

本文介绍了一种在iOS应用中设置字体的方法,并列举了一系列可用的字体名称及其样式变化,包括常规、粗体、斜体等,适用于不同场景的需求。
label.font  =  [UIFont fontWithName: @" Arial-BoldItalicMT "  size: 24 ];

 

字体名如下:


Font Family: American Typewriter
Font: AmericanTypewriter
Font: AmericanTypewriter-Bold

Font Family: AppleGothic
Font: AppleGothic

Font Family: Arial
Font: ArialMT
Font: Arial-BoldMT
Font: Arial-BoldItalicMT
Font: Arial-ItalicMT

Font Family: Arial Rounded MT Bold
Font: ArialRoundedMTBold

Font Family: Arial Unicode MS
Font: ArialUnicodeMS

Font Family: Courier
Font: Courier
Font: Courier-BoldOblique
Font: Courier-Oblique
Font: Courier-Bold

Font Family: Courier New
Font: CourierNewPS-BoldMT
Font: CourierNewPS-ItalicMT
Font: CourierNewPS-BoldItalicMT
Font: CourierNewPSMT

Font Family: DB LCD Temp
Font: DBLCDTempBlack

Font Family: Georgia
Font: Georgia-Bold
Font: Georgia
Font: Georgia-BoldItalic
Font: Georgia-Italic

Font Family: Helvetica
Font: Helvetica-Oblique
Font: Helvetica-BoldOblique
Font: Helvetica
Font: Helvetica-Bold

Font Family: Helvetica Neue
Font: HelveticaNeue
Font: HelveticaNeue-Bold

Font Family: Hiragino Kaku Gothic **** W3
Font: HiraKakuProN-W3

Font Family: Hiragino Kaku Gothic **** W6
Font: HiraKakuProN-W6

Font Family: Marker Felt
Font: MarkerFelt-Thin

Font Family: STHeiti J
Font: STHeitiJ-Medium
Font: STHeitiJ-Light

Font Family: STHeiti K
Font: STHeitiK-Medium
Font: STHeitiK-Light

Font Family: STHeiti SC
Font: STHeitiSC-Medium
Font: STHeitiSC-Light

Font Family: STHeiti TC
Font: STHeitiTC-Light
Font: STHeitiTC-Medium

Font Family: Times New Roman
Font: TimesNewRomanPSMT
Font: TimesNewRomanPS-BoldMT
Font: TimesNewRomanPS-BoldItalicMT
Font: TimesNewRomanPS-ItalicMT

Font Family: Trebuchet MS
Font: TrebuchetMS-Italic
Font: TrebuchetMS
Font: Trebuchet-BoldItalic
Font: TrebuchetMS-Bold

Font Family: Verdana
Font: Verdana-Bold
Font: Verdana-BoldItalic
Font: Verdana
Font: Verdana-Italic

Font Family: Zapfino
Font: Zapfino

UIFont fontWithName 后不知道字体的名字,看了下面的全解决!

<think> 我们正在讨论的是如何在iOS中使用Objective-C设置UIButton的字体。根据用户的需求,他之前可能已经了解了在Storyboard中设置固定字体的方法,现在想知道如何通过Objective-C代码来实现。 参考引用: - 引用[1]展示了如何创建一个自定义类型的UIButton。 - 引用[2]展示了如何创建一个自定义的UIButton并设置其图片,以及如何将它用作导航栏按钮。 但是,用户现在关心的是设置字体,因此我们需要关注如何通过代码设置UIButton的字体。 在Objective-C中,设置UIButton的字体通常是通过设置其titleLabel的font属性来实现的。例如: ```objective-c UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setTitle:@"Button" forState:UIControlStateNormal]; button.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0]; ``` 这样设置后,字体就不会随系统动态类型变化(前提是该字体不是系统动态字体,而是固定大小的字体)。如果要使用系统字体但固定大小,可以使用`systemFontOfSize:`方法: ```objective-c button.titleLabel.font = [UIFont systemFontOfSize:17.0]; ``` 注意:为了避免Storyboard设置被代码覆盖(如引用[2]提到的,Storyboard中的设置可能会在视图加载后覆盖代码设置),我们可以在`viewDidLoad`方法中进行设置,或者确保在Storyboard中已经将字体设置为自定义(Custom)并关闭了自动调整。 另外,如果按钮是在Storyboard中创建的,我们可以通过IBOutlet连接,然后在代码中设置字体。 步骤: 1. 在Storyboard中创建按钮并连接到ViewController的IBOutlet属性。 2. 在ViewController的`viewDidLoad`方法中设置按钮的字体。 例如: ```objective-c // ViewController.h @property (weak, nonatomic) IBOutlet UIButton *myButton; // ViewController.m - (void)viewDidLoad { [super viewDidLoad]; self.myButton.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0]; } ``` 如果希望设置多个按钮的字体,可以将这些按钮放在一个数组里,然后循环设置。 此外,为了防止字体随系统动态类型变化,我们需要避免使用`preferredFontForTextStyle:`方法,而是使用固定字体和大小。 总结:设置UIButton字体的核心是操作其titleLabel的font属性,使用固定字体和大小。 现在,根据用户要求,我们需要生成相关问题。 相关问题: 1. 如何设置UIButton的字体颜色和背景颜色? 2. 如何为UIButton设置不同状态(如高亮、选中)下的字体? 3. 如何设置UIButton的字体为系统动态字体(即随系统设置变化)? 4. 如何通过代码创建UIButton并设置字体? 注意:回答中如果引用到之前的引用,需要添加引用标识。 </think> ### iOS Objective-C 设置UIButton字体的方法 在Objective-C中设置UIButton字体主要通过操作`titleLabel.font`属性实现。以下是具体方法和注意事项: #### 1. **基础设置方法** ```objective-c // 创建自定义按钮(推荐方式) UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; // [^1] // 设置固定字体(不随系统变化) button.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0]; // 设置系统字体但固定大小 button.titleLabel.font = [UIFont systemFontOfSize:17.0]; // 设置加粗字体 button.titleLabel.font = [UIFont boldSystemFontOfSize:18.0]; ``` **关键点**: - 使用`UIButtonTypeCustom`类型创建按钮可完全自定义样式[^1] - `fontWithName:size:`指定固定字体名称和大小 - `systemFontOfSize:`使用系统字体但固定大小 #### 2. **防止字体随系统动态类型变化** ```objective-c // 方案1:直接设置固定字体(推荐) button.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:16.0]; // 方案2:关闭自动调整 if (@available(iOS 10.0, *)) { button.titleLabel.adjustsFontForContentSizeCategory = NO; } ``` **注意**:iOS 10+需显式关闭`adjustsFontForContentSizeCategory`属性 #### 3. **为不同状态设置字体** ```objective-c // 设置默认状态字体 [button setTitle:@"Normal" forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:16]; // 设置高亮状态字体 [button setTitle:@"Highlighted" forState:UIControlStateHighlighted]; button.titleLabel.highlightedFont = [UIFont boldSystemFontOfSize:16]; ``` #### 4. **导航栏按钮字体设置** ```objective-c // 创建自定义导航栏按钮[^2] UIButton* navButton = [UIButton buttonWithType:UIButtonTypeCustom]; navButton.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; [navButton setTitle:@"Back" forState:UIControlStateNormal]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navButton]; ``` #### 5. **高级技巧:渐变字体** 结合引用[3]的文字渐变色技术: ```objective-c CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = button.titleLabel.bounds; gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor]; // [^3] [button.titleLabel.layer addSublayer:gradient]; button.titleLabel.layer.mask = button.titleLabel.layer; // 文字蒙版 ``` #### ⚠️ **注意事项** 1. **优先级问题**: - Storyboard设置会覆盖`viewDidLoad`中的代码设置[^2] - 建议在`viewDidLayoutSubviews`中进行字体设置 2. **字体名称**: - 使用精确字体名(如`Helvetica-Bold`而非`Helvetica`) - 可通过`[UIFont familyNames]`查看可用字体 3. **动态类型兼容**: ```objective-c // 若需支持动态类型(不推荐固定字体时) button.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; ```
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值