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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19113673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:55:15  来源:igfitidea点击:

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

iosobjective-cxcodeuistoryboard

提问by Harry

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 配合使用。不过我有一个问题:

UITextViewdoesn't want to display font changes within code. 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 (i.e. systemFontOfSize:) but that didn't work. The text view onlyshows the font that's set in the 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.

UITextView不想在代码中显示字体更改。文本颜色更改工作正常。任何其他属性更改都可以。字体,完全没有。我使用的是自定义字体,所以我检查了不同大小的不同字体(即systemFontOfSize:),但这没有用。文本视图显示在 Storyboard 中设置的字体。我会在这里错过什么?是否有任何自动布局约束会干扰这种事情?我在迁移过程中遇到了一些约束问题,但正如我所说,字体在 iOS 7 中运行良好。

I guess it's something in the Storyboard that I'm missing, as if I create a UIViewControllerand 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.

我会提供一些代码,但我不确定在这种情况下它会有所帮助。

采纳答案by Harry

The issue was caused by the editableproperty being false in the Storyboard. I have absolutely no idea why this caused the font to remain unchanged - and only on iOS 6.

该问题是由editableStoryboard 中的属性为 false引起的。我完全不知道为什么这会导致字体保持不变 - 并且仅在 iOS 6 上。

回答by jab

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;

回答by Satachito

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 To set selectable

因此,我在 Storyboard Editor 中检查了 UITextView 的“selectable”属性以将其设置为 YES 设置可选

and later in viewWillAppear set this property to NO.

稍后在 viewWillAppear 中将此属性设置为 NO。

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

回答by Jordan Montel

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];

回答by Eugene Tartakovsky

Thank you for all the answers guys. Issue is still present on iOS9. What i've found out, is that when you set "User Interaction Enabled = false" in the Interface Builder you can leave Editableand Selectable= trueand user will not be able to edit a text view.

谢谢大家的回答。iOS9 上仍然存在问题。我发现,当您在界面生成器中设置“用户交互启用 = false”时,您可以保留EditableSelectable= true并且用户将无法编辑文本视图。

So, my solution is:

所以,我的解决方案是:

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

回答by Esqarrouth

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)

回答by Charlie Seligman

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)

回答by Daniel Belém Duarte

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 Selectableproperties = YESin the Interface Builderand 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,

希望这可以帮助,

回答by Anton Plebanovich

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
    }
}

回答by KoreanXcodeWorker

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.

那么字体的系列和大小不会改变,但是当我反转这两个步骤时,它会起作用。设置文本应该在设置字体的系列/大小之前。