【问题标题】:UITextView wraps text when built with iOS 7 SDK使用 iOS 7 SDK 构建时 UITextView 包装文本
【发布时间】:2013-09-22 21:42:18
【问题描述】:

我在UIScrollView 中有一个UITextView,它在由xcode 4.x 构建的iOS 6 上工作得非常好,但是现在用xcode 5 构建它不能正常工作,即使在iOS 6 上也是如此。

问题是即使UITextViewUIScrollView 的宽度很大,文本也会随着屏幕宽度换行。我使用这段代码来计算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


【解决方案1】:

尝试使用:

[textView.text boundingRectWithSize:CGSizeMake(txtFrame.size.width, CGFLOAT_MAX)
                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                           attributes:[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)
    {
        //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];
}

编辑

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

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

祝你好运!

【讨论】:

  • 谢谢,但这似乎并没有解决我的问题。我同时使用灵活的宽度和高度。看起来我得到了正确的尺寸,因为 textview 在两个方向上滚动得足够多,但是文本本身仍然是自动换行的。
  • 虽然我还没有弄清这件事的真相,但我还是把赏金奖励给你,以免浪费。感谢您的尝试。
  • 谢谢。我刚刚用您可能想尝试的另一件事更新了答案...
【解决方案2】:

方法 sizeWithFont:(UIFont *)font constrainedToSize ..." 在 iOS 7 中已被弃用。

它会正常运行。

在 iOS 7 中查看它的替代品

NSString的实例方法

-(CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:

(NSDictionary *)attributes context:(NSStringDrawingContext *)context

看看这个答案。 Replacement for deprecated sizeWithFont: in iOS 7?

【讨论】:

  • 谢谢。奇怪的是我怎么没有得到编译器警告。请参阅我对问题的编辑。谢谢
【解决方案3】:

我认为您想计算内容大小,以便您也可以增加滚动视图的内容大小。如果是,请使用此链接,https://stackoverflow.com/a/19152955/1023083

【讨论】:

    【解决方案4】:

    回复“谢谢,但这似乎不能解决我的问题。我同时使用灵活的宽度和高度。看起来我得到了正确的尺寸,因为 textview 在两个方向上滚动都足够了,但是文本本身仍在自动换行。”

    我通过 [textView sizeToFit] 解决了这个问题;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 2013-11-09
      • 1970-01-01
      • 2022-07-15
      相关资源
      最近更新 更多