【发布时间】:2013-09-22 21:42:18
【问题描述】:
我在UIScrollView 中有一个UITextView,它在由xcode 4.x 构建的iOS 6 上工作得非常好,但是现在用xcode 5 构建它不能正常工作,即使在iOS 6 上也是如此。
问题是即使UITextView 和UIScrollView 的宽度很大,文本也会随着屏幕宽度换行。我使用这段代码来计算UITextView 的新宽度和高度,即使文本视图向左/向右滚动,文本也会被换行,就好像宽度只是屏幕的宽度一样。
谢谢
self.responceTextView.text = [NSString stringWithFormat:@"%@%@",_responceTextView.text,responce];
[self textViewDidChange:self.responceTextView];
- (void)textViewDidChange:(UITextView *)textView
{
// Recalculate size of text field
CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
CGSize reqSize = [textView.text sizeWithFont:[UIFont fontWithName:@"Courier" size:12] constrainedToSize:maxSize lineBreakMode:NSLineBreakByClipping];
self.responceTextView.frame = CGRectMake(0, 0, reqSize.width+16, reqSize.height+16);
// Resize scroll view if needs to be smaller so text stays at top of screen
CGFloat maxScrollHeight = maxScrollViewSize.size.height;
if (self.responceTextView.frame.size.height < maxScrollHeight) {
self.responceScrollView.frame = CGRectMake(self.responceScrollView.frame.origin.x, self.responceScrollView.frame.origin.y, self.responceScrollView.frame.size.width, self.responceTextView.frame.size.height);
} else {
self.responceScrollView.frame = maxScrollViewSize;
}
// Set content size
self.responceScrollView.contentSize = CGSizeMake(self.responceTextView.frame.size.width, self.responceTextView.frame.size.height);
[self scrollToCursor];
}
编辑----
好的,看来sizeWithFont 在 iOS 7 中已被弃用。奇怪的是我如何没有收到编译器警告。
它在 iOS 6 上不起作用仍然没有意义(或者在使用 iOS 7 SDK 构建时完全删除?)
我已经尝试了这 2 个替代方案,但得到的尺寸完全相同。
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Courier" size:12], NSFontAttributeName,
nil];
CGRect rect = [textView.text boundingRectWithSize:maxSize options:NSLineBreakByClipping | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil];
返回:{{0, 0}, {439.27148, 168}}
CGSize rect2 = [textView.text sizeWithAttributes:attributes];
返回:{439.27148, 168}
上面的原始返回{439.27148, 168}
它们都应该返回更广阔的视野。
编辑 2 ---- 从上面看来,返回的框架是正确的(439 宽),但它是仍在 textview 中被文字包裹的文本。
【问题讨论】:
-
您没有收到关于 iOS 7 弃用的警告,因为您仍在为 iOS 6 构建。它不能告诉您迁移到新的 API,因为新的 API 不存在在你的目标之一。顺便说一句,这是“响应”而不是“响应”:)
-
我喜欢让我的代码保持唯一性;)
标签: ios objective-c uikit uitextview ios7