UITextView字体是零 [英] UITextView font is nil
问题描述
我在故事板中创建了一个字体大小为14的UITextView,并将其连接到ViewController的detailDescriptionLabel属性。此代码在viewDidLoad中:
self.detailDescriptionLabel.font = [UIFont systemFontOfSize:10];
NSLog(@text is%@; font is%@,self.detailDescriptionLabel.text,self.detailDescriptionLabel.font);
控制台输出如下:
文本是Lorem Ipsum;字体为(空)
为什么字体设置为零? setFont:正在工作;字体确实收缩。我想获得字体,所以在手势后我可以调用lineHeight的字体。通过这种方式,我可以找出哪一行已经被点击了下面的代码:
$ $ p $ $ $ $ $ $> int line = [tap locationInView:tap。查看] .y / self.detailDescriptionLabel.font.lineHeight;
字体也是零。行被设置为-2147483648,这显然不是我想要的。尝试检查这个UITextView的'selectable'复选框在Interface Builder中,如果你没有选中它(它在属性检查器中)。
我刚遇到类似的问题。我有一个UITextView的字体属性为零的IBOutlet,它将不会响应任何字体或文本颜色的代码更改,直到之后的文本属性被设置。我能够通过试验和错误的方式将问题的根源缩小到特定的复选框,但我无法解释它为什么会起作用(看起来像一个系统错误)。
I created a UITextView with a font size of 14 in a storyboard and hooked it up to the detailDescriptionLabel property of my ViewController. This code is in viewDidLoad:
self.detailDescriptionLabel.font=[UIFont systemFontOfSize:10];
NSLog(@"text is %@; font is %@", self.detailDescriptionLabel.text, self.detailDescriptionLabel.font);
The console output follows:
text is Lorem Ipsum; font is (null)
Why is the font set to nil? The setFont: is working; the font does shrink. I want to get the font so after a gesture I can call lineHeight on the font. This way, I can find out which line has been tapped with the following code:
int line = [tap locationInView:tap.view].y / self.detailDescriptionLabel.font.lineHeight;
Here, too, the font is nil. line is set to -2147483648, which is obviously not what I want.
Try checking the 'selectable' checkbox for this UITextView in Interface Builder, if you have it unchecked (it's in the Attributes Inspector).
I just encountered similar issue. I had an IBOutlet to a UITextView whose font property was nil, and it would not respond to any font or text-color changes in code until after its text property had been set. I was able to narrow down the source of the problem to this particular checkbox through trial and error, but I can't explain why it works (seems like a system bug).
这篇关于UITextView字体是零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!