我有一个UISlider
会更改的字体大小UITextView
。在更改字体大小之前,我想检查一下我的高度UITextView
是否低于其视图容器的高度。问题在于,对于相同的字体大小sizeToFit
,sizeWithAttribute
它们返回的高度不同。
为此,我试图UITextView
使用自定义fontSize来获取高度,但是我无法找到一种方法来实现,并且不理解为什么我这样做时无法得到相同的高度:
self.photoDescription.text = @"Test"
self.photoDescription.font = [self.photoDescription.font fontWithSize:18];
[self.photoDescription sizeToFit];
NSLog(@"real height %f", self.photoDescription.frame.size.height);
//getting : 37.5
和:
NSLog(@"calculated height %f", [self.photoDescription.text sizeWithAttributes:@{NSFontAttributeName:[self.photoDescription.font fontWithSize:18]}].height);
//getting: 20.97
同样的事情:
CGRect textRect = [self.photoDescription.text boundingRectWithSize:CGSizeMake(320, 300)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[self.photoDescription.font fontWithSize:18]}
context:nil];
CGSize size = textRect.size;
NSLog(@"height %f", size.height);
// getting : 20.97
有什么想法吗?
我终于找到了约旦·蒙特尔解决方案的答案:iOS7 UITextView contentsize.height替代
我用他的函数来获取宽度:
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
UITextView *textView = [[UITextView alloc] init];
[textView setAttributedText:text];
CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句