Dar*_*ren 10 objective-c uitextview uikit ios ios7
我有一个UITextView内部的UIScrollView工作完美无缺的iOS 6建造xcode 4.x,但现在建设与xcode 5它无法正常工作,甚至在iOS 6.
问题是,即使屏幕宽度的文本换行UITextView,并UIScrollView有较大的宽度.我使用此代码来计算出新的宽度和高度UITextView,即使textview向左/向右滚动,文本也会被包裹,好像宽度只是屏幕的宽度.
谢谢
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];
}
Run Code Online (Sandbox Code Playgroud)
编辑----
好吧,所以它似乎sizeWithFont在iOS 7中被弃用了.奇怪的是我没有得到编译器警告.它仍然没有意义,它在iOS 6上不起作用(或者在使用iOS 7 SDK构建时是否完全删除?)
我尝试了这两种替代方案,但从所有方面获得完全相同的大小.
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];
Run Code Online (Sandbox Code Playgroud)
收益: {{0, 0}, {439.27148, 168}}
CGSize rect2 = [textView.text sizeWithAttributes:attributes];
Run Code Online (Sandbox Code Playgroud)
收益: {439.27148, 168}
原来上面的回报 {439.27148, 168}
他们都应该回归更广阔的视野.
编辑2 ----从上面看,返回的帧是正确的(439宽)但是它仍然是文本视图中包含的文字.
Esb*_*enB 18
尝试使用:
[textView.text boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName, nil] context:nil];
Run Code Online (Sandbox Code Playgroud)
字符串测量似乎非常多.对于我所做的测试,这是唯一能够提供合适尺寸的选项组合.
我在iOS7中成功使用以下代码(它UITextField具有最小和最大高度.当文本的高度变大时MAX_HEIGHT_MESSAGE_TEXTBOX,滚动条出现在UITextField)中.
const float MAX_HEIGHT_MESSAGE_TEXTBOX = 80;
const float MIN_HEIGHT_MESSAGE_TEXTBOX = 30;
- (void)setFrameToTextSize:(CGRect)txtFrame textView:(UITextView *)textView
{
if(txtFrame.size.height > MAX_HEIGHT_MESSAGE_TEXTBOX)
{
//OK, the new frame is to large. Let's use scroll
txtFrame.size.height = MAX_HEIGHT_MESSAGE_TEXTBOX;
textView.scrollEnabled = YES;
[textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
}
else
{
if (textView.frame.size.height < MIN_HEIGHT_MESSAGE_TEXTBOX) {
//OK, the new frame is to small. Let's set minimum size
txtFrame.size.height = MIN_HEIGHT_MESSAGE_TEXTBOX;
}
//no need for scroll
textView.scrollEnabled = NO;
}
//set the frame
textView.frame = txtFrame;
}
- (void)setframeToTextSize:(UITextView *)textView animated:(BOOL)animated
{
//get current height
CGRect txtFrame = textView.frame;
//calculate height needed with selected font. Note the options.
//append a new line to make space for the cursor after user hit the return key
txtFrame.size.height =[[NSString stringWithFormat:@"%@\n ",textView.text]
boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName, nil] context:nil].size.height;
if (animated) {
//set the new frame, animated for a more nice transition
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseOut |UIViewAnimationOptionAllowAnimatedContent animations:^{
[self setFrameToTextSize:txtFrame textView:textView];
} completion:nil];
}
else
{
[self setFrameToTextSize:txtFrame textView:textView];
}
}
- (void)textViewDidChange:(UITextView *)textView
{
[self setframeToTextSize:textView animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
编辑
当串测量是正确的,则可能需要更改lineBreakMode上UITextView的textContainer.(NSTextContainer是iOS7中的新类,包含有关如何布局文本的信息):
textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping; // default is NSLineBreakByWordWrapping
Run Code Online (Sandbox Code Playgroud)
祝好运!
该方法sizeWithFont:(UIFont *)font constrainedToSize ..."在 iOS 7 中已被弃用。
它将正常运行。
查看 iOS 7 中的替代方案
的实例方法NSString
-(CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:
(NSDictionary *)attributes context:(NSStringDrawingContext *)context
Run Code Online (Sandbox Code Playgroud)
检查一下这个答案。 替换已弃用的 sizeWithFont: 在 iOS 7 中?
| 归档时间: |
|
| 查看次数: |
19219 次 |
| 最近记录: |