开发者

Limiting Characters/ rows In UITextView using height, width of TextView & font size of character.

开发者 https://www.devze.com 2023-04-05 14:13 出处:网络
I have n number of UITextViews on a single page, The height & width of UITextView is say 200*300 respectively(varies for different text views). I need to Limit the max. number of rows/ characters

I have n number of UITextViews on a single page, The height & width of UITextView is say 200*300 respectively(varies for different text views). I need to Limit the max. number of rows/ characters of text the user can enter in each textview depending on ht & width of TextView. I've the font size of the characters that needs to be entered in text view. So how do i go about i开发者_运维技巧mplementing this?


textfield number of lines:

If you are using iOS 3, you need to use the leading property:
int numLines = txtview.contentSize.height / txtview.font.leading;
If you are using iOS 4, you need to use the lineHeight property:
int numLines = txtview.contentSize.height / txtview.font.lineHeight;


What you can do is , you can calculate the height of the text and you might have limit the text view height and width. Now you can compare the text height and your text view height. and do whatever your requirement is.

Calculating text height:-

CGSize labelsize;
NSString *text=@"your text";
[textView setFont:[UIFont fontWithName:@"Helvetica"size:14]];
labelsize=[text sizeWithFont:textView.font constrainedToSize:CGSizeMake(280, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"%f",labelsize.height);
if(labelsize.height>300)
{
//show text with 300 height
}
else
{
//show full text
}
0

精彩评论

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

关注公众号