ios - NSAttributedString 仅适用于 Helvetica Neue

标签 ios objective-c uitextview nsattributedstring

我正在尝试使用 NSAttributedString 加载 UITextView。我正在从富文本文件“.rtf”加载 NSAttributedString

我的[NSBundle mainBundle]中的文件名是My Text-HelveticaNeue.rtf, My Text-TimesNewRomanPSMT.rtf,和 My Text-MarkerFelt-Thin.rtf 这些文件都有正确的字体集。他们也有一些粗体文本。

这是我的代码:

NSString *resourseName = [NSString stringWithFormat:@"My Text-%@", self.currentFontName];
NSURL *rtfUrl = [[NSBundle mainBundle] URLForResource:resourseName withExtension:@".rtf"];
NSError *error;

NSAttributedString *myAttributedTextString = [[NSAttributedString alloc] initWithFileURL:rtfUrl options:nil documentAttributes:nil error:&error];

myTextView.attributedText = myAttributedTextString;

NSLog(@"%@", myAttributedTextString);

Helvetica Neue 文件保留所有格式,包括粗体文本。但是其他文件只保留它们的字体;他们不保留加粗的文本。我可能做错了什么?

编辑:

我听从了@Rob 的建议,这是我得到的输出。

Name:HelveticaNeue
Font:<UICTFont: 0xc1aab30> font-family: "Helvetica Neue"; font-weight: normal; font-style: normal; font-size: 13.00pt
Bold:<UICTFont: 0xc1af770> font-family: "Helvetica Neue"; font-weight: bold; font-style: normal; font-size: 13.00pt

Name:MarkerFelt-Thin
Font:<UICTFont: 0xfb16170> font-family: "MarkerFelt-Thin"; font-weight: normal; font-style: normal; font-size: 13.00pt
Bold:<UICTFont: 0xfb18c60> font-family: "MarkerFelt-Thin"; font-weight: normal; font-style: normal; font-size: 13.00pt

Name:TimesNewRomanPSMT
Font:<UICTFont: 0xc1cb730> font-family: "Times New Roman"; font-weight: normal; font-style: normal; font-size: 13.00pt
Bold:<UICTFont: 0xc1c9b30> font-family: "Times New Roman"; font-weight: normal; font-style: normal; font-size: 13.00pt

最佳答案

iOS 不提供粗体或斜体版本的 MarkerFelt-Thin,因此您无法显示它。

iOS 确实发布了 TimesNewRomanPS-BoldMT,但它似乎并不认为它是 TimesNewRomanPSMT 的粗体版本。 (我没有测试过,但根据你的评论,我假设 TimesNewRomanPS-ItalicMT 也是如此;它存在但不被视为斜体。)我可能会认为这是一个错误并打开雷达。

这里的一个关键点是您不要使用“粗体”或“斜体”字体。粗体和斜体本身就是完整的字体(由字体设计师单独设计并打包到自己的字体定义中)。他们只是与基本(“罗马”)版本具有象征性关系。如果没有安装粗体版本的字体,则请求粗体只会返回相同的字体。

您可以使用如下程序探索哪些字体可用,以及 iOS 将哪些字体视为另一种字体的粗体版本:

void LogFontNamed(NSString *name) {
  UIFont *font = [UIFont fontWithName:name size:13];
  UIFontDescriptor *boldFontDescriptor = [[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
  UIFont *boldFont = [UIFont fontWithDescriptor:boldFontDescriptor size:13];

  NSLog(@"=================");
  NSLog(@"Name:%@", name);
  NSLog(@"Font:%@", font);
  NSLog(@"Bold:%@", boldFont);
  NSLog(@"=================");
}

void printFonts() {
// uncomment if you want to explore the font lists
//  NSLog(@"%@",[UIFont familyNames]);
//  NSLog(@"%@", [UIFont fontNamesForFamilyName:@"Times New Roman"]);

  LogFontNamed(@"HelveticaNeue");
  LogFontNamed(@"MarkerFelt-Thin");
  LogFontNamed(@"TimesNewRomanPSMT");
  LogFontNamed(@"TimesNewRomanPS-BoldMT");
}

关于ios - NSAttributedString 仅适用于 Helvetica Neue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19099621/

相关文章:

iphone - UITextView 拦截对 setSelectedRange 的调用?

ios - 触发按钮以在 swift3 xcode 8 ios 10 中更改 UITextView 中的文本

ios - 如何重定向到 Google map (如果已安装),否则重定向到 Appstore 以使用 Swift 安装 Google map

ios - 如何在连续的 repeatForever 中链接 Facebook pop 动画?

objective-c - 在 viewDidAppear 之前无法正确设置框架

objective-c - 如何识别导航堆栈中的前一个 View Controller

ios - Grand Central Dispatch EXC_BAD_ACCESS 异常

iphone - iPad Storyboard无法正常工作

ios - AFNetworking Reachability 在网络状态改变时显示 UIView

ios - 如何根据其中的 UITextView 的大小调整 UITableViewCell 的大小?