简体   繁体   English

UITextView - 在 XCode 5 上设置字体不适用于 iOS 6

[英]UITextView - setting font not working with iOS 6 on XCode 5

I'm using storyboards for my UI.我正在为我的用户界面使用故事板。 I was previously using XCode 4.6 and released on iOS 6. I have since updated to iOS 7 using XCode 5 and updated the Storyboard to work nicely with XCode 5. I have one issue though:我以前使用 XCode 4.6 并在 iOS 6 上发布。此后我使用 XCode 5 更新到 iOS 7 并更新了 Storyboard 以与 XCode 5 配合使用。不过我有一个问题:

UITextView doesn't want to display font changes within code. UITextView不想在代码中显示字体更改。 Text colour changes work fine.文本颜色更改工作正常。 Any other property changes are fine.任何其他属性更改都可以。 Font, not at all.字体,完全没有。 I was using a custom font, so I checked different fonts with different sizes (ie systemFontOfSize: ) but that didn't work.我使用的是自定义字体,所以我检查了不同大小的不同字体(即systemFontOfSize: ),但这没有用。 The text view only shows the font that's set in the Storyboard.文本视图显示在 Storyboard 中设置的字体。 What could I be missing here?我会在这里错过什么? Are there any auto-layout constraints that mess with this sort of thing?是否有任何自动布局约束会干扰这种事情? I had a few issues with constraints during the migration, but as I said, the fonts work fine in iOS 7.我在迁移过程中遇到了一些约束问题,但正如我所说,字体在 iOS 7 中运行良好。

I guess it's something in the Storyboard that I'm missing, as if I create a UIViewController and add a text view in code, it works fine.我想这是我缺少的 Storyboard 中的某些东西,就好像我创建了一个UIViewController并在代码中添加了一个文本视图,它工作正常。

I'd put up some code, but I'm not sure it'd help at all in this case.我会提供一些代码,但我不确定在这种情况下它会有所帮助。

Even stranger, this only happens on iPhone, not iPad.更奇怪的是,这只发生在 iPhone 上,而不是 iPad。

If you're setting the font in code and don't want an editable text view, do this:如果您在代码中设置字体并且不想要可编辑的文本视图,请执行以下操作:

textView.editable = YES;
textView.font = newFont;
textView.editable = NO;

In my case, it is matter of 'selectable' property of UITextView.就我而言,这是 UITextView 的“可选”属性的问题。

So I checked 'selectable' property of UITextView in Storyboard Editor to set it YES因此,我在 Storyboard Editor 中检查了 UITextView 的“selectable”属性以将其设置为 YES设置可选

and later in viewWillAppear set this property to NO.然后在 viewWillAppear 中将此属性设置为 NO。

textview.text = @"some text";
textview.selectable = NO;

The issue was caused by the editable property being false in the Storyboard.该问题是由 Storyboard 中的editable属性为 false 引起的。 I have absolutely no idea why this caused the font to remain unchanged - and only on iOS 6.我完全不知道为什么这会导致字体保持不变 - 并且仅在 iOS 6 上。

For me it's work if you set the text of your UITextView and after set the font (same for color) :对我来说,如果您设置 UITextView 的文本并在设置字体(颜色相同)之后,它就可以工作:

_myTextView.text = @"text";
[_myTextView setFont:[UIFont fontWithName:@"Helvetica Neue" size:18.0f]];
_myTextView.textColor = [UIColor whiteColor];

Thank you for all the answers guys.谢谢大家的回答。 Issue is still present on iOS9. iOS9 上仍然存在问题。 What i've found out, is that when you set " User Interaction Enabled = false " in the Interface Builder you can leave Editable and Selectable = true and user will not be able to edit a text view.我发现,当您在界面生成器中设置“用户交互启用 = false ”时,您可以保留EditableSelectable = true并且用户将无法编辑文本视图。

So, my solution is:所以,我的解决方案是:

  1. Set User Interaction Enabled = False in IB在 IB 中设置用户交互启用 = False
  2. Set Editable = True in IB在 IB 中设置Editable = True
  3. Set Selectable = True in IB在 IB 中设置Selectable = True
  4. Configure your text view in whatever way you want.以您想要的任何方式配置您的文本视图。

Code for swift:快速代码:

textOutlet.editable = true
textOutlet.textColor = UIColor.whiteColor()
textOutlet.font = UIFont(name: "ArialMT", size: 20)
textOutlet.editable = false

Or if you change the text first it magically gets solved或者,如果您先更改文本,它会神奇地解决

textOutlet.text = "omg lol wtf"
textOutlet.textColor = UIColor.whiteColor()
textOutlet.font = UIFont(name: "ArialMT", size: 20)

I found the font size was being ignored.我发现字体大小被忽略了。 This was resolved by ticking the checkbox called: Selectable (having selected the UITextView within the storyboard)这是通过勾选名为的复选框解决的:Selectable(在故事板中选择了 UITextView)

This issue only happens when setting Selectable property to FALSE in the Interface Builder.只有在 Interface Builder 中将 Selectable 属性设置为 FALSE 时才会发生此问题。

In case you are required to have the Editable and Selectable properties set to FALSE do it from the CODE and not in the Interface Builder.如果您需要将 Editable 和 Selectable 属性设置为 FALSE,请从 CODE 而不是在 Interface Builder 中进行。

Summing up, make Editable and Selectable properties = YES in the Interface Builder and then add the following code in case you need the properties to be FALSE:总而言之,在Interface Builder中使 Editable 和 Selectable属性 = YES ,然后添加以下代码,以防您需要这些属性为 FALSE:

_textView.editable   = NO;
_textView.selectable = NO;

Hope this helps,希望这有帮助,

Swift 3 category that worked for me:对我有用的 Swift 3 类别:

extension UITextView {
    func setFontAndUpdate(_ font: UIFont?) {
        self.font = font

        // Font doesn't update without text change
        let text = self.text
        self.text = nil
        self.text = text
    }
}

In my case(Developing on Xcode 7.3, iOS 9),就我而言(在 Xcode 7.3、iOS 9 上开发),

The cause was the order of setting text and font-family/size, not the options of editable or selectable many answers tell there.(and I don't get any storyboard, xib on that Textview.)原因是设置文本和字体系列/大小的顺序,而不是许多答案在那里讲述的可编辑或可选择选项。(而且我没有得到任何故事板,该 Textview 上的 xib。)

If I input like如果我输入像

[myTextView setFont:[UIFont fontWithName:@"HelveticaNeue-Italic" size:20]];
myTextView.attributedText = mAttStr;

then the font's family and size are not changed, but else when I reverse those two step, it works.那么字体的系列和大小不会改变,但是当我反转这两个步骤时,它会起作用。 Setting text should be ahead of setting font's family/size.设置文本应该在设置字体的系列/大小之前。

As mentioned by others:正如其他人所提到的:

textView.font = UIFont.systemFont(ofSize: 16)
textView.isEditable = false

ps no need to first set isEditable as true since it's true by default: a little shorter, a little nicer PS无需第一套isEditabletrue ,因为它是true默认:短一点,更好一点

就我而言,我通过在“viewDidLayoutSubviews”中设置新字体来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM