[英]UITextView font is nil
I created a UITextView with a font size of 14 in a storyboard and hooked it up to the detailDescriptionLabel property of my ViewController. 我在情节提要中创建了一个字体大小为14的UITextView,并将其连接到ViewController的detailDescriptionLabel属性。 This code is in viewDidLoad: 这段代码在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? 为什么将字体设置为nil? The setFont: is working; setFont:正在工作; the font does shrink. 字体确实缩小。 I want to get the font so after a gesture I can call lineHeight on the font. 我想要获取字体,因此在手势之后我可以在字体上调用lineHeight。 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. 在这里,字体也是nil。 line is set to -2147483648, which is obviously not what I want. 行设置为-2147483648,这显然不是我想要的。
Try checking the "selectable" checkbox for this UITextView
in Interface Builder. 尝试在Interface Builder中选中此UITextView
的“可选”复选框。 It's in the Attributes Inspector. 在“属性”检查器中。 Per @VahramDodoryan's comment below, you can then set selectable
to false if you don't want to support selection. 根据下面的@VahramDodoryan的评论,如果不想支持选择,则可以将selectable
设置为false。
I can't explain why this works, but it's probably a UIKit bug. 我无法解释为什么这样做,但这可能是UIKit的错误。 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. 我有一个到UITextView
的IBOutlet
,它的font属性为nil,并且在设置了text属性之后 ,它不会响应代码中的任何字体或文本颜色更改。 I arrived at this solution through trial-and-error. 我通过反复试验得出了这个解决方案。
If you're still encountering this issue on recent releases of iOS, consider opening a radar: 如果您在最新版本的iOS上仍然遇到此问题,请考虑打开雷达:
I tried changing every property and constraint , I had on my text view. 我尝试更改文本视图中的每个属性和约束。
Including selectable as suggested above 包括以上建议的可选内容
In frustration in the end I deleted and re-added it and that was the unsatisfying fix. 最后,我无奈地删除并重新添加了它,这是不令人满意的解决方案。
You should try logging self.detailDescriptionLabel
. 您应该尝试记录self.detailDescriptionLabel
。 The value might be null. 该值可能为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.