故事板中自定义字体的属性string无法正确加载

我们在我们的项目中使用自定义字体。 它在Xcode 5中运行良好。在Xcode 6中,它以纯文本forms工作,在代码中将属性string归为一类。 但是,在故事板中设置的那些属性string在模拟器或设备上运行时都会恢复为Helvetica,尽pipe它们在故事板中看起来都是正确的。

我不确定是否是Xcode 6或iOS 8 SDK的错误,或者在Xcode 6 / iOS 8中更改使用自定义字体的方式?

对我来说修复是使用IBDesignable类:

 import UIKit @IBDesignable class TIFAttributedLabel: UILabel { @IBInspectable var fontSize: CGFloat = 13.0 @IBInspectable var fontFamily: String = "DIN Light" override func awakeFromNib() { var attrString = NSMutableAttributedString(attributedString: self.attributedText) attrString.addAttribute(NSFontAttributeName, value: UIFont(name: self.fontFamily, size: self.fontSize)!, range: NSMakeRange(0, attrString.length)) self.attributedText = attrString } } 

在Interface Builder中给你这个:

Interface Builder自定义字体与属性的字符串

你可以像往常一样设置你的属性string,但你必须在新的可用属性中再次设置你的字体大小和fontfamily。

由于界面生成器默认使用自定义字体,因此这会导致您看到的是您所获得的内容 ,而在构build应用程序时我更喜欢这一点。

注意

我使用这个而不是纯粹的版本的原因是,我正在设置属性标签上的属性,如行距,这在使用普通样式时是不可用的。

最简单的答案是将字体拖到FontBook中。 如果字体在您的项目中,但不在计算机的FontBook中,则IB有时无法find它。 奇怪,但已经有好几次为我工作了。

遇到同样的问题:故事板中为TextView设置的属性字体在XCode 6.1和iOS 8 SDK的运行时不起作用。

这是我如何解决这个问题,可能是你的解决方法:

  1. 打开你的textview的属性检查器,将文本改为“Plain”

  2. 点击十字,删除“wC hR”(红色下方)

    在这里输入图像说明

  3. 将文本更改为“已赋值”,然后可以设置文本的字体和大小。

  4. 运行来检查它是否工作

我的解决scheme是一个工作。 真正的解决scheme是苹果修复Interface Builder。

有了它,您可以使用系统字体在界面构build器中标记所有粗体和斜体文本,然后在运行时呈现您的自定义字体。 在所有情况下可能都不是最佳的。

  NSMutableAttributedString* ApplyCustomFont(NSAttributedString *attributedText, UIFont* boldFont, UIFont* italicFont, UIFont* boldItalicFont, UIFont* regularFont) { NSMutableAttributedString *attrib = [[NSMutableAttributedString alloc] initWithAttributedString:attributedText]; [attrib beginEditing]; [attrib enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrib.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) { if (value) { UIFont *oldFont = (UIFont *)value; NSLog(@"%@",oldFont.fontName); [attrib removeAttribute:NSFontAttributeName range:range]; if([oldFont.fontName rangeOfString:@"BoldItalic"].location != NSNotFound && boldItalicFont != nil) [attrib addAttribute:NSFontAttributeName value:boldItalicFont range:range]; else if([oldFont.fontName rangeOfString:@"Italic"].location != NSNotFound && italicFont != nil) [attrib addAttribute:NSFontAttributeName value:italicFont range:range]; else if([oldFont.fontName rangeOfString:@"Bold"].location != NSNotFound && boldFont != nil) [attrib addAttribute:NSFontAttributeName value:boldFont range:range]; else if(regularFont != nil) [attrib addAttribute:NSFontAttributeName value:regularFont range:range]; } }]; [attrib endEditing]; return attrib; } 

受这篇文章的启发

感谢这个线程,我来到这个解决scheme:

 private let fontMapping = [ "HelveticaNeue-Medium": "ITCAvantGardePro-Md", "HelveticaNeue": "ITCAvantGardePro-Bk", "HelveticaNeue-Bold": "ITCAvantGardePro-Demi", ] func switchFontFamily(string: NSAttributedString) -> NSAttributedString { var result = NSMutableAttributedString(attributedString: string) string.enumerateAttribute(NSFontAttributeName, inRange: NSRange(location: 0, length: string.length), options: nil) { (font, range, _) in if let font = font as? UIFont { result.removeAttribute(NSFontAttributeName, range: range) result.addAttribute(NSFontAttributeName, value: UIFont(name: fontMapping[font.fontName]!, size: font.pointSize)!, range: range) } } return result } 

遇到同样的问题:故事板中UILabel的属性字体在运行时不起作用。 使用这个UIFont + IBCustomFonts.m为我工作https://github.com/deni2s/IBCustomFonts

一样的问题。

解决:只需选中在TextView中select。 没有这个我有标准的系统字体。

我一直在努力解决这个问题:UILabel在自定义字体的IB中正确显示,但在设备或模拟器上不能正确显示(字体包含在项目中,并在普通UILabels中使用)。

最后在(Mac)App Store上findAttributed String Creator。 生成代码放置在您的应用程序在适当的地方。 太棒了。 我不是创造者,只是一个快乐的用户。

双击并将字体安装到系统中。 它会工作(Xcode 8.2)

我试图让文本有多个段落的tableView单元格。 归属的string似乎是在段落之间获得额外空间的一种方式(比在string中执行两个换行更好看一些)。 当我发现IB设置不适用于运行时,当你想在单元格中放置不同的文本时,发现了这个post和其他post。

我想出的主要事情是添加一个扩展到string(使用Swift)来创build一个具有特定属性的string。 这里的例子使用Marker Felt字体,因为它很容易与Helvetica区分开来。 该示例还显示了段落之间的一些额外的间距,以使它们彼此更加明显。

 extension String { func toMarkerFelt() -> NSAttributedString { var style = NSMutableParagraphStyle() style.paragraphSpacing = 5.0 let markerFontAttributes : [NSObject : AnyObject]? = [ NSFontAttributeName : UIFont(name: "Marker Felt", size: 14.0)!, NSParagraphStyleAttributeName: style, NSForegroundColorAttributeName : UIColor.blackColor() ] let s = NSAttributedString(string: self, attributes: markerFontAttributes) return s } } 

然后,在我的自定义tableViewCell中,你发送你想要的文本,并将其转换为UILabel上的属性string。

 // MarkerFeltCell.swift class MarkerFeltCell: UITableViewCell { @IBOutlet weak var myLabel: UILabel! func configureCellWithString(inputString : String) { myLabel.attributedText = inputString.toMarkerFelt() }} 

在视图控制器与tableView,你应该注册你的单元格viewDidLoad() – 我用了一个笔尖,所以像这样:

 let cellName = "MarkerFeltCell" tableView.registerNib(UINib(nibName: cellName, bundle: nil), forCellReuseIdentifier: cellName) 

为了让单元格弄清楚它应该有多高,build立一个用于获取大小信息的原型单元格,并且永远不会添加到tableView中。 所以,在你的视图控制器的variables中:

 var prototypeSummaryCell : MarkerFeltCell? = nil 

然后在(可能覆盖 – 取决于您的视图控制器)heightForRowAtIndexPath:

 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { // ... if xib == "MarkerFeltCell" { if prototypeCell == nil { prototypeCell = tableView.dequeueReusableCellWithIdentifier(xib) as? MarkerFeltCell } let width : CGFloat = tableView.bounds.width let height : CGFloat = prototypeCell!.bounds.height prototypeCell?.bounds = CGRect(x: 0, y: 0, width: width, height: height) configureCell(prototypeCell!, atIndexPath: indexPath) prototypeSummaryCell?.layoutIfNeeded() let size = prototypeSummaryCell!.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) let nextHeight : CGFloat = ceil(size.height + 1.0) return nextHeight } else { // ... 

在上面的代码中,prototypeCell将在第一次被需要时填充。 然后使用prototypeCell在通过自动调整过程后计算单元格的高度。 你需要使用ceil()函数来调整高度。 我还join了一些额外的模糊因素。

最后的代码是你如何configuration单元格的文本。 对于这个例子,简单地说:

 func configureCell(cell :UITableViewCell, atIndexPath indexPath: NSIndexPath) { if let realCell = cell as? MarkerFeltCell { realCell.configureCellWithString("Multi-line string.\nLine 2.\nLine 3.") // Use \n to separate lines } } 

此外,这是一个笔尖的笔尖。 将标签固定到单元格的边缘(需要边距),但使用“大于或等于”约束,底部约束的优先级小于“必需”。

单元约束

将标签的字体设置为已分配。 实际的IB字体并不重要。

LabelFont

结果在这种情况下:

CellResults

在属性string的情况下,你可以在字体列表中添加自定义字体 – 点击字体图标,这将显示下面的对话框。在下面的对话框中,你可以添加自己的类别或现有的自定义字体。 归属字体对话框

点击pipe理字体后 ,打开下面的对话框select你创build或现有的类别。 点击+符号在类别中添加字体。 pipe理字体对话框

您可以将自定义字体添加到字体书。

第一步 :点击pipe理字体。 它打开字体书。

在这里输入图像说明

第二步 :点击加号并添加你的字体。

在这里输入图像说明

下一次当你点击字体时,新增加的字体也会显示在列表中。 但请确保您的自定义字体添加到info.plist和捆绑资源。