gpt4 book ai didi

ios - UITextView 在使用 iOS 7 SDK 构建时换行文本

转载 作者:技术小花猫 更新时间:2023-10-29 10:59:29 24 4
gpt4 key购买 nike

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

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

编辑----

好的,看来 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 中进行换行的文本。

最佳答案

尝试使用:

[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 

祝你好运!

关于ios - UITextView 在使用 iOS 7 SDK 构建时换行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18867006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com