iphone - 如何设置 UILabel 以使用 Arial Narrow(以编程方式),就像我们在 Storyboard上所做的那样?

标签 iphone ios objective-c fonts uilabel

当我们在 Storyboards 上设计 UILabel 时,我们可以将它们设置为 Attributed 文本,并且可以设置 Arial Narrow 字体。但是当我们使用上面的代码时以编程方式:

   NSRange auxRange = NSMakeRange(0, [myString length]);
   NSMutableAttributedString *attrStr = [myLabel.attributedText mutableCopy];
   UIFont *newFont = [UIFont fontWithName:@"Arial Narrow" size:17];
   [attrStr addAttributes:@{ NSFontAttributeName : newFont } range:auxRange];
   myLabel.attributedText = attrStr;

我们有一个指向 newFont 的 nil 指针,这是因为编译器找不到 Arial Narrow 作为字体名称。

我的问题是,为什么我们可以在 Storyboard 上设置 Arial Narrow 而我们不能在代码上做同样的事情? 如果可能的话,我做错了什么?

最佳答案

打印手机字体列表:

for (NSString *familyName in [UIFont familyNames]) {
        for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
            NSLog(@"%@", fontName);
        }
    }

然后检查要添加的字体的名称,它必须在那里。

使用您看到的打印名称。

关于iphone - 如何设置 UILabel 以使用 Arial Narrow(以编程方式),就像我们在 Storyboard上所做的那样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19250390/

相关文章:

iphone - 在 iPhone 上通过手指触摸画一条线

iphone - '-[NSURL initFileURLWithPath :]: nil string parameter' when using CoreData

ios - 即使设备在 objective-c 中水平旋转,如何始终以纵向模式加载 UIViewController?

html - 在 Swift 中从本地文件编辑和加载 HTML

ios - 如何自定义 UIActivityViewController 分享菜单的例子?

objective-c - 更改 block 内 NSString 的值

iOS ARC 应用程序和一行或两行对象初始化

iphone - 在数组中存储 cvMat 对象 - OpenCV - iPhone

iphone - 如何在 viewController 中使用 UITabbar?

iphone - sizeThatFits 的正确使用方法 : and sizeToFit with UILabel?