【问题标题】:Replace UITableViewCell text with UITextView用 UITextView 替换 UITableViewCell 文本
【发布时间】:2012-12-05 13:05:16
【问题描述】:

在我的应用程序中,我将 UITextView 设置为 UITableViewCell 的 contentView,以便您可以写入其中。我是这样设置的:

        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(cell.bounds.origin.x, cell.bounds.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
        [textView setFont:[UIFont fontWithName:@"Arial" size:18.0f]];
        [textView setBackgroundColor:[UIColor blueColor]];
        [textView setAutocorrectionType:UITextAutocorrectionTypeNo];

        textView.text = cell.textLabel.text;
        textView.delegate = self;
        cell.textLabel.text = @"";
        self.textView = textView;
        [cell.contentView addSubview:textView];
        [textView becomeFirstResponder];

我想将textView.text 设置在cell.textlabel.text 所在的右侧,但它比之前的单元格文本更靠左和靠上一点。如何将单元格的文本对齐和/或文本偏移设置为 UITextView,以便文本位于同一位置?

【问题讨论】:

    标签: ios ipad uitableview uitextview


    【解决方案1】:
    yourUITextView.contentInset = UIEdgeInsetsMake(-4,-8,0,0); // Mess around with these values
    

    或者……

    设置 textView 的框架

        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 0, cell.contentView.frame.size.width - 10, cell.contentView.frame.size.height)];
    

    【讨论】:

    • 非常感谢,这几乎成功了。我可以得到正确的垂直对齐,但无论我尝试左或右哪个值都没有区别。任何想法为什么?
    • 您是否尝试将文本从视图左侧稍微向右移动?
    • 是的,我愿意。我不想改变框架的大小,因为那会弄乱我的heightForRowAtIndexPath 返回值:/
    • 更改单元格中包含的对象的框架大小会如何弄乱 indexpath 处的行高度?我认为您正在混淆单元格本身的内容。直接设置单元格的大小,与里面的objects frame的大小无关。
    • 您是否尝试过将 UIView 添加到 contentView 以便您可以正确分配它的高度,然后为 textView 添加子视图?为了提供准确的解决方案,我需要准确地听到您期望的行为。内容视图确保在切换进入和退出编辑模式时正确对齐,因此将 UIView 直接添加到此,然后将 textView 添加到 UIView 应该可以解决您的问题。除非有你所期待的其他行为。
    猜你喜欢
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    相关资源
    最近更新 更多