当文本设置为"归因于"时,自定义字体的UITextView无法正常工作

时间:2015-06-28 17:00:10

标签: xcode swift nsattributedstring custom-font

我有一个UITextView,其中包含一些来自.rtf的文本(直接粘贴到Xcode上)

上下文只包含一种自定义字体(Futura Book BT 11.0)

如果我设置"文本(属性)"属性为" plain" =自定义字体从故事板和应用程序中正确显示

如果我设置"文字"属于"属于" =。自定义字体从故事板正确显示但不是从应用程序。

由于我的目标是使用多种字体工作的文本,如何使属性属性与自定义字体一起使用? (SWIFT)

谢谢!

11 个答案:

答案 0 :(得分:8)

  

自定义字体从故事板中正确显示,但不是从应用程序显示。

应用程序根本不显示字体,还是恢复为系统字体?如果它恢复到Helvetica,看起来这可能是iOS 8.1中的已知错误  http://openradar.appspot.com/radar?id=5117089870249984

可能的解决方案: 你在模拟器和设备上测试了吗?它可能只是在模拟器上不起作用,这意味着您可以通过在系统上安装字体来修复它。

答案 1 :(得分:4)

我的所作所为:

private var _mutableString: NSMutableAttributedString!

if !isTitle {
    var titleFont: UIFont = UIFont(name: StyleGlobals.TitleFontName, size: StyleGlobals.TitleFontSize)!
    self._mutableString.addAttribute(NSFontAttributeName, value: titleFont, range: NSMakeRange(0, count(self._mutableString.string)))
    self._mutableString.addAttribute(NSForegroundColorAttributeName, value: StyleGlobals.FontColor, range: NSMakeRange(0, count(self._mutableString.string)))
}

self.Label.attributedText = self._mutableString

这既适用于新字体颜色,也适用于字体本身。我有一个必须能够包含不同字体的UILabel。它可以是标题或副标题。因此,当我确定Label将要包含的内容时,我必须应用该字体。

答案 2 :(得分:3)

当我在归属UILabel中使用多个自定义字体时,我遇到了同样的问题。故事板显示正确的结果,但设备没有!!

我发现如果字体大小与具有不同自定义字体的文本不同。它工作正常!!

所以现在我使用的字体大小差别很小。就像一个人14岁,我用另一个14.01。它不可能在内部故事板中,所以我打开故事板作为源代码并手动设置字体大小如:

<attributedString key="attributedTitle">    
  <fragment content="Do not have account? ">
      <attributes>
        <color key="NSColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="calibratedRGB"/>
        <font key="NSFont" size="14" name="Roboto-Light"/>
        <paragraphStyle key="NSParagraphStyle" alignment="center" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
      </attributes>
    </fragment>
    <fragment content="Login">
      <attributes>
        <color key="NSColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="calibratedRGB"/>
        <font key="NSFont" size="14.01" name="Roboto-Bold"/>
        <paragraphStyle key="NSParagraphStyle" alignment="center" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
      </attributes>
    </fragment>
<attributedString key="attributedTitle">

但我仍然无法找到原因!

答案 3 :(得分:2)

您确定“Futura Book BT”是否已在您的应用中正确添加为自定义字体?

答案 4 :(得分:1)

在我的情况下问题是我没有在我的系统上安装字体,所以我用Font Book做了。 然后,字体出现在属性文本标签的列表中。

编辑:现在我在显示当前文本方面遇到了一些问题并且这样做了(我使用UILabel,未在UITextView上测试):

我的解决方案是从html创建一个属性字符串(在SO中的某处看到此扩展名):

extension String {
    var html2AttStr: NSAttributedString? {
        guard let data = dataUsingEncoding(NSUTF8StringEncoding) else { return nil }
        do {
            return try NSAttributedString(data: data,
                                          options: [
                                            NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                            NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding],
                                          documentAttributes: nil)
        } catch let error as NSError {
            //print(error.code)
            return nil
        }
    }
}

然后我做的是:

let htmlStyle = "<meta charset=\"UTF-8\"><style> body { font-family: 'SourceSansPro-Light'; font-size: 18px; text-align: center; } b {font-family: 'SourceSansPro-Light'; }</style>"
let myString = "<font size=\"+2\">Slightly bigger font</font><br><br>Some message with normal text <b>a bold text</b> and some more text"
var attributedString = htmlStyle
attributedString.appendContentsOf(myString)
informationLabel.attributedText = attributedString.html2AttStr

这样做的一个好处是我现在可以放一些&#39;%s&#39;字符串中的文本用于使用单词/数字/等进行解析。并且翻译和解析更容易。使用格式化文本,我必须制作一堆代码来查找文本,应用格式而不触及任何内容。这对我很有效:)

答案 5 :(得分:1)

请使用 viewDidLayoutSubviews 方法调用您的所有代码! 比工作。 我在使用字体大小,样式i viewDidLoad或viewWillApperar方法时遇到很多问题。

所以只需在 viewDidLayoutSubviews 方法中移动代码即可。您可以使用flag仅更改一次字体,因为可以多次调用此方法:]

答案 6 :(得分:0)

制作UITextview&#34;可选择&#34;从故事板开始工作。

答案 7 :(得分:0)

✅已修复:

通过将我的UITextView子类化为KMPlaceholderTextView来解决,这对于在文本视图中添加占位符也很有用,并且可以与UITextField一样在Interface Builder中使用。

现在,我可以成功使用自定义字体并添加占位符文本。

答案 8 :(得分:0)

您可以使用此扩展程序

extension NSMutableAttributedString {

    func createAttributedString(word : NSArray, attributeCustomizeFont:UIFont, defaultFont : UIFont, defaultColor : UIColor, allowSpecing : Bool , allowUnderLine : Bool = false) -> NSAttributedString {

        self.addAttribute(NSAttributedString.Key.font, value: defaultFont, range: (self.string as NSString).range(of: self.string))
        if allowSpecing {
            self.addAttribute(NSAttributedString.Key.kern, value: NSNumber(value: 5), range: (self.string as NSString).range(of: self.string))
        }else {
            self.addAttribute(NSAttributedString.Key.kern, value: NSNumber(value: 0), range: (self.string as NSString).range(of: self.string))
        }

        self.addAttribute(NSAttributedString.Key.foregroundColor, value: defaultColor, range: (self.string as NSString).range(of: self.string))

        for i in 0..<word.count {
            self.addAttribute(NSAttributedString.Key.font, value: attributeCustomizeFont, range: (self.string as NSString).range(of: word.object(at: i) as! String))

        }
        if allowUnderLine {
            self.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: (self.string as NSString).range(of: self.string))
        }
        return self
    }
}

如何使用扩展程序

var attributedString = NSMutableAttributedString(string:"Test for set custom font")
        attributedString = attributedString.createAttributedString(word: ["custom font"], attributeCustomizeFont: UIFont(name: "Arial", size: 18)! , defaultFont: UIFont(name: "Helvetica", size: 15)!, defaultColor: UIColor.black, allowSpecing: false) as! NSMutableAttributedString

因此,整个字符串字体设置为大小为18的'Helvetica',而对于'自定义字体'文本设置为大小为15的'Arial'字体。

答案 9 :(得分:-1)

有一种解决方法。只需双击字体文件(在Finder中)并将其安装到系统中。

答案 10 :(得分:-1)

我使用了不同的字体大小但仍面临问题。然后我将文本对齐方式更改为左侧而不是对齐方式。然后它工作正常。

相关问题