Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableViewCell sizeToFit中的UITextView未按预期工作_Iphone_Ios_Objective C_Ipad_Uitextview - Fatal编程技术网

Iphone UITableViewCell sizeToFit中的UITextView未按预期工作

Iphone UITableViewCell sizeToFit中的UITextView未按预期工作,iphone,ios,objective-c,ipad,uitextview,Iphone,Ios,Objective C,Ipad,Uitextview,我有一个UITextView,我需要调整它的大小,使它有一个宽度可以拉伸以填充屏幕,但高度可变,这样它就足够大以显示所有文本。调整大小时,应将UITextView设置为尽可能小的高度,以适应不同的尺寸 为此,我使用了以下代码: textView.text = textMessage; [textView sizeToFit]; CGRect rect = textView.frame; rect.size.height = text

我有一个
UITextView
,我需要调整它的大小,使它有一个宽度可以拉伸以填充屏幕,但高度可变,这样它就足够大以显示所有文本。调整大小时,应将
UITextView
设置为尽可能小的高度,以适应不同的尺寸

为此,我使用了以下代码:

textView.text            = textMessage;
[textView sizeToFit];
CGRect rect              = textView.frame;
rect.size.height         = textView.contentSize.height;
textView.frame           = rect;
当我这样做时,它似乎并没有使UITextView成为可能的最小高度。下面似乎还有一些多余的空间。下面是一个例子(为了说明我的观点,我将背景颜色设置为红色):

请有人告诉我为什么会发生这种情况,以及我如何解决这个问题?请注意,UITextView位于自定义UITableView单元格中。此单元格使用自动调整大小遮罩水平拉伸,以根据方向填充屏幕


我花了很长时间试图让它工作,但我不明白为什么底部有多余的空间,而且当我旋转设备时,UITextView的高度似乎都保持不变(即使我调用了
[UITableView reloadData]
方法,我也为其定义了行高方法).

我曾经遇到过同样的问题,但我发现了这个问题

CGSize maximumLabelSize = CGSizeMake(300,80);//Or whatever size you need to be the max

CGSize expectedLabelSize = [textMessage sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];
rect.size.height = expectedLabelSize.height;
textView.frame = rect;

希望这能有所帮助。

UITextView的
超级视图(或者同级视图是UITextView的底边固定到的)是防止UITextView高度降低的原因。在不知道视图的视图层次结构的情况下,我建议在UITextView的父视图上调用
setNeedsLayout

我同意mpwhitt的观点,但要考虑到表格单元格的拉伸特性,我会增加一点差异

    CGSize expectedLabelSize = [textView.text sizeWithFont:textView.font 
           constrainedToSize:CGSizeMake(textView.frame.size.width,9999)
           lineBreakMode:UILineBreakModeWordWrap];

    rect.size.height = expectedLabelSize.height;
    textView.frame = rect;
这样,您就不必担心字体是否更改或标签宽度是否更改。我不确定这是否会消除那一小块多余的高度,但我从来没有像你要求的那样需要它


你能从你找到的高度减去3-5个像素吗?

我在过去的两个项目中也遇到了同样的问题。我所做的就是这样

- (float)getHeightFortheDynamicLabel:(NSString *)stringForTheLabel
{
    CGSize maxSize = CGSizeMake(215, 2000.0);
    CGSize newSize = [stringForTheLabel sizeWithFont:[UIFont systemFontOfSize:14.0]
                                   constrainedToSize:maxSize];
    return newSize.height;
}
只需传递需要确定其高度的字符串,此方法将返回大小。为标签设置此高度


还请注意,使用行数设置为0的UILabel代替UITextView。这种方法与标签完美结合。在开始时,我也使用了UITextView,这导致了偏移量问题。后来有了这个标签,效果非常好。尝试使用UILabel。希望这对你有帮助。并再次检查用于标签的字体大小。使它们相同(对于方法中给出的标签和值)。

以下是问题的解决方案

//
//  CustomCell.m
//  Custom
//
//  Created by Syed Arsalan Pervez on 2/8/13.
//  Copyright (c) 2013 SAPLogix. All rights reserved.
//

#import "CustomCell.h"

@implementation CustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        _textView = [[UITextView alloc] initWithFrame:CGRectZero];
        [_textView setBackgroundColor:[UIColor clearColor]];
        [_textView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [[self contentView] addSubview:_textView];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [_textView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

- (void)dealloc
{
    [_textView release];
    [super dealloc];
}

@end
使用手机

//
//  TestViewController.m
//  Custom
//
//  Created by Syed Arsalan Pervez on 2/8/13.
//  Copyright (c) 2013 SAPLogix. All rights reserved.
//

#import "TestViewController.h"
#import "CustomCell.h"

@interface TestViewController ()

@end

@implementation TestViewController

- (void)viewDidLoad
{
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

#define DEFAULT_TEXT @"This is an example block of text which seems to run over the edge of the UITableView so it no longer shows up the way I want."

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *_identifier = @"CustomCell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:_identifier];
    if (!cell)
    {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_identifier];
        [[cell contentView] setBackgroundColor:[UIColor redColor]];
    }

    cell.textView.text = DEFAULT_TEXT;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect frame = [[UIApplication sharedApplication] statusBarFrame];
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
// You can replace DEFAULT_TEXT with text you want to be displayed in the cell.
    CGSize size = [DEFAULT_TEXT sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake( UIInterfaceOrientationIsPortrait(orientation) ? frame.size.width : frame.size.height, 9999)];

    return size.height;
}

- (UIInterfaceOrientation)interfaceOrientation
{
    return (UIInterfaceOrientation)UIInterfaceOrientationMaskAll;
}

- (void)dealloc
{
    [super dealloc];
}

@end

您可以尝试myTextView.contentInset=UIEdgeInsetsMake(0.0f,0.0f,0.0f,0.0f);不,我没有填充物。我也尝试过UIEdgeInsetsMake解决方案,但是它根本没有改变外观。我认为
contentSize
无法获得格式化内容的大小。它更像是滚动视图,您可以在其中指定可以滚动的内容大小。至少我是这么想的,也许我错了
NSLog
大小,并查看其是否因不同文本而改变。否则,如果它总是比实际文本大一行,您可以简单地减去一个固定值,如12pt。如果您没有在文本视图中编辑文本,则使用UILabel它会很好地工作。是的……这意味着您可以用您感兴趣的内容填充字体,以替换
[UIFont boldSystemFontOfSize:18]
。这应该是可行的,但应该将其放置在一个在调整视图大小时调用的位置<父视图的code>layoutSubviews
可能是坚持这一点的好地方。