使用iOS 7 SDK构建时,UITextView会包装文本 [英] UITextView wraps text when built with iOS 7 SDK

查看:98
本文介绍了使用iOS 7 SDK构建时,UITextView会包装文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIScrollView 中有一个 UITextView ,它在 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
{
//重新计算文本字段的大小
CGSize maxSize = CGSizeMake(MAXFLOAT,MAXFLOAT);
CGSize reqSize = [textView.text sizeWithFont:[UIFont fontWithName:@Couriersize:12] constrainedToSize:maxSize lineBreakMode:NSLineBreakByClipping];

self.responceTextView.frame = CGRectMake(0,0,reqSize.width + 16,reqSize.height + 16);

//如果需要更小,请调整滚动视图的大小,使文本保持在屏幕顶部
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;
}

//设置内容大小
self.responceScrollView.contentSize = CGSizeMake(self.responceTextView.frame.size.width,self.responceTextView.frame.size.height) ;

[self scrollToCursor];
}

编辑----



好的,所以似乎 sizeWithFont 在iOS 7中已被弃用。奇怪的是我没有得到编译器警告。
它仍然没有意义,它在iOS 6上不起作用(或者在使用iOS 7 SDK构建时是否完全删除?)



I尝试了这两种替代方案,但从所有方面获得完全相同的大小。

  NSDictionary * attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@Couriersize:12],NSFontAttributeName,
nil];
CGRect rect = [textView.text boundingRectWithSize:maxSize选项:NSLineBreakByClipping | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading属性:attributes context:nil];

返回: {{0,0},{439.27148,168}}

  CGSize rect2 = [textView.text sizeWithAttributes:attributes]; 

返回: {439.27148,168}



以上原件返回 {439.27148,168}



<他们都应该回归更广阔的视野。



编辑2 ----
从上面可以看出,返回的帧是正确的(439宽)然而它仍然是文字包裹在里面textview。

解决方案

尝试使用:

  [textView.text boundingRectWithSize:CGSizeMake(txtFrame.size.width,CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
属性:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName,nil] context:nil];

字符串测量似乎非常错误。对于我已经完成的测试,这是唯一给出正确大小的选项组合。



我在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)
{
//好的,新的框架很大。让我们使用滚动
txtFrame.size.height = MAX_HEIGHT_MESSAGE_TEXTBOX;
textView.scrollEnabled = YES;
[textView scrollRangeToVisible:NSMakeRange([textView.text length],0)];
}
其他
{
if(textView.frame.size.height< MIN_HEIGHT_MESSAGE_TEXTBOX){
//好的,新框架很小。我们设置最小尺寸
txtFrame.size.height = MIN_HEIGHT_MESSAGE_TEXTBOX;
}
//无需滚动
textView.scrollEnabled = NO;
}
//设置框架
textView.frame = txtFrame;
}

- (void)setframeToTextSize:(UITextView *)textView animated:(BOOL)animated
{
//获取当前高度
CGRect txtFrame = textView.frame;

//计算所选字体所需的高度。请注意选项。
//在用户点击返回键后附加一个新行为光标腾出空间
txtFrame.size.height = [[NSString stringWithFormat:@%@ \ n,textView.text]
boundingRectWithSize:CGSizeMake(txtFrame.size.width,CGFLOAT_MAX)
选项:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
属性:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName,nil] context:nil] .size.height;

if(animated){
//设置新帧,为更好的过渡设置动画
[UIView animateWithDuration:0.2 delay:0.0选项:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowAnimatedAtent动画:^ {
[self setFrameToTextSize:txtFrame textView:textView];
}完成:nil];
}
else
{
[self setFrameToTextSize:txtFrame textView:textView];
}
}

- (void)textViewDidChange:(UITextView *)textView
{
[self setframeToTextSize:textView animated:YES];
}

编辑



当字符串测量正确时,您可能需要更改 UITextView 上的 lineBreakMode 的textContainer。 ( NSTextContainer 是iOS7中的一个新类,包含有关文本应如何布局的信息):

  textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping; //默认为NSLineBreakByWordWrapping 

祝你好运!


I have a UITextView inside a UIScrollView that worked perfectly fine on iOS 6 built from xcode 4.x, however now building with xcode 5 it doesn't work properly, even on iOS 6.

The problem is the text wraps with the screen width even though the UITextView and UIScrollView have large widths. I use this code to work out the new width and height of the UITextView, and even though the textview scrolls left/right the text is wrapped as if the width is only the width of the screen.

Thanks

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];
}

EDIT ----

Ok, so it seems sizeWithFont is deprecated in iOS 7. Strange how I get no compiler warning. It still doesn't make sense that it doesn't work on iOS 6 (or is it completely removed when built with iOS 7 SDK?)

I have tried these 2 alternatives, but get exactly the same size back from all.

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];

returns: {{0, 0}, {439.27148, 168}}

CGSize rect2 = [textView.text sizeWithAttributes:attributes];

returns: {439.27148, 168}

And the original above returns {439.27148, 168}

They should all return a wider view.

EDIT 2 ---- It seems from above that the returned frame is correct (439 wide) however it's the text that is still being word wrapped inside the textview.

解决方案

try using:

[textView.text boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX)
                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                           attributes:[NSDictionary dictionaryWithObjectsAndKeys:textView.font,NSFontAttributeName, nil] context:nil];

The string measuring seems pretty buggy. This is the only option combination that gives the right size, for the testing I have done.

I'm using the following code with success in iOS7 (it's a UITextField with a minimum and maximum height. When the text's height get larger then MAX_HEIGHT_MESSAGE_TEXTBOX, scrollbars appears in the 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];
}

EDIT

When the string measuring is correct, you might need to change the lineBreakModeon the UITextView's textContainer. (NSTextContainer is a new class in iOS7, containing information about how text should be laid out):

textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping; // default is NSLineBreakByWordWrapping 

Good luck!

这篇关于使用iOS 7 SDK构建时,UITextView会包装文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆