高度为15的UITextView,只有顶部文本可见,下半部分被剪辑



我的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个像素。战绩等。

是否有一些东西,我完全错过了关于设置UITextFields?

很多谢谢,代码

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

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

也许你需要玩的值:)

问题是默认情况下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];

最新更新