【问题标题】:UITextView with a heigh of 15, Only top of the text visible, bottom half getting clipped高度为 15 的 UITextView,仅文本顶部可见,下半部分被剪裁
【发布时间】:2011-04-17 23:04:46
【问题描述】:

我的 UITextView 只有 1 行文本。但是文本的下半部分被截断了,文本似乎被画得很低,或者文本视图似乎有一些高度,它需要大于我设置的 15。

        UIFont *font = [UIFont fontWithName:@"Marker Felt" size:13];
    CGSize size = CGSizeMake(310, 1000.f);
    CGSize stringSize = [[messagesArray objectAtIndex:indexPath.row] 
sizeWithFont:font constrainedToSize:size];
    NSLog(@"txtView height: %d",(int)stringSize.height);
    UITextView *txtview = [[UITextView alloc] initWithFrame:CGRectMake(5, 0, 
310, (int)stringSize.height)];
    txtview.autoresizesSubviews = NO;
    [txtview setBackgroundColor:[UIColor blueColor]];
    [txtview setFont:[UIFont fontWithName:@"Marker Felt" size:13]];
    txtview.textColor = [UIColor whiteColor];       
    [txtview setOpaque:NO];
    txtview.editable = NO;
    txtview.text = [messagesArray objectAtIndex:indexPath.row];
    [cell addSubview:txtview];

这一行

NSLog(@"txtView height: %d",(int)stringSize.height);

总是打印出 15 的值。字体大小为 13,所以我希望文本在 UITextView 中垂直居中放置,顶部和底部有 1 个像素。 15-13等

关于设置 UITextFields,我是否完全遗漏了什么?

非常感谢, 代码

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    UITextView 上有一个默认的填充。试试这个:

    [txtView setContentInset:UIEdgeInsetsMake(-8,-8,0,0)];
    

    也许您需要使用这些值 :)

    【讨论】:

      【解决方案2】:

      问题是默认情况下 UITextView 在底部添加了大约 32 作为 contentInset。您可以通过继承 UITextView 并覆盖 contentInset 方法来轻松解决此问题,以返回您想要的内容:

      @interface EditItemText : UITextView {
      }
      @end
      
      
      @implementation EditItemText
      
      // override the contentInset method so the scrolling behaviour of the text view is normal
      - (UIEdgeInsets) contentInset { 
          //I am padding with 4 but you could return UIEdgeInsetsZero; 
          return UIEdgeInsetsMake (4,4,4,4);
      }
      
      @end
      

      然后,您将使用新的子类而不是 UITextView:

      EditItem *vwTxt = [[[EditItemText alloc] initWithFrame:CGRectMake(10, 8.0, 170, 70.0)] autorelease];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        • 2012-01-13
        • 2010-12-06
        • 2012-12-27
        相关资源
        最近更新 更多