开发者

UITextView with a heigh of 15, Only top of the text visible, bottom half getting clipped

My UITextView has only 1 line of text. But the bottom half of the text is being cut off, the text seems to be drawn to low, or the Text View seems to have some height that it needs to be which is larger than the 15 i am setting it too.

        UIFont *font = [UIFont fontWithName:@"Marker Fel开发者_如何学Pythont" 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];

This line

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

Always prints out the value of 15. The Font is off size 13, so I would have expected the text to be positioned vertically centered in the UITextView with 1 pixel free at the top and the bottom. 15-13 etc.

Is there something that I am completely missing regards to setting up UITextFields?

Many Thanks, Code


There is a default padding on UITextView. Try this:

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

Maybe you need to play with the values :)


The problem is that by default UITextView adds about 32 to the bottom as the contentInset. You can get around this easily by subclassing the UITextView and overriding the contentInset method to return what you want like so:

@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

Then instead of a UITextView you would use your new subclass:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜