0

I need UITextView fit to conent in ios 6, - Max width =250.0f; - heigth is FLT_MAX i was search and try so many times(found very many result in stackoverflow but it won't able). My code here :

UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];
UIFont *fon = [UIFont systemFontOfSize:13.0f];
UITextView *textView = [[UITextView alloc]init];
textView.textColor =[UIColor whiteColor];
textView.backgroundColor = [UIColor clearColor];
textView.text = message;
textView.font = fon;
[textView sizeToFit];

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1){

    //in IOS7 it works perfectly
    textView.layoutManager.allowsNonContiguousLayout = NO;
    CGSize myTextViewSize = [textView sizeThatFits:CGSizeMake(250.0, FLT_MAX)];
    textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, myTextViewSize.width, myTextViewSize.height);
    returnView.frame = CGRectMake(0.0, 0.0, myTextViewSize.width, myTextViewSize.height);
}else{
    //how to do ?
}
[returnView addSubview:textView];
return returnView;
modusCell
  • 13,151
  • 9
  • 53
  • 80
Lantranduc
  • 29
  • 7
  • Try this link, hopes this will help you. http://stackoverflow.com/questions/50467/how-do-i-size-a-uitextview-to-its-content – Rameswar Prasad Aug 16 '14 at 13:07
  • I was try, but it require fix Width. i want be dynamic height and width , like chat bubble of IMessage app . do you have any ideal ? – Lantranduc Aug 17 '14 at 02:12

1 Answers1

0

Okay, I found something, may be good for who looking this problem

UITextView *textView = [[UITextView alloc]init];
textView.textColor =[UIColor whiteColor];
textView.backgroundColor = [UIColor clearColor];
textView.editable=NO;
textView.font = fon;
const char *c = [[[self checkStringIsNull:message wantReturn:1] retain] cStringUsingEncoding:NSISOLatin1StringEncoding];
message = [[NSString alloc]initWithCString:c encoding:NSUTF8StringEncoding];
[textView sizeToFit];
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1){
    textView.text = message;
    textView.layoutManager.allowsNonContiguousLayout = NO;
    CGSize myTextViewSize = [textView sizeThatFits:CGSizeMake(250.0, FLT_MAX)];
    textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, myTextViewSize.width, myTextViewSize.height);
    returnView.frame = CGRectMake(0.0, 0.0, myTextViewSize.width, myTextViewSize.height);
    [returnView addSubview:textView];
}else{
    maxHeight=maxWidth=0;
    NSArray *strArr = [message componentsSeparatedByString:@"\n"];
    for (int i=0; i<[strArr count]; i++) {
        float width= [self widthOfString:[strArr objectAtIndex:i] withFont:[UIFont systemFontOfSize:13.0]];
        if (width>maxWidth) {
            if (width>250.0) {
                width=250.0;
                maxHeight+=20.0;
            }
            maxWidth = width;
        }
        maxHeight+=24.0;
    }
    if (maxWidth<=240.0) {
        maxWidth+=20.0;
    }
    textView.text= message;
    textView.frame = CGRectMake(0.0, 0.0, maxWidth, maxHeight);
    [returnView addSubview:textView];
    returnView.frame = CGRectMake(0.0,0.0, maxWidth,maxHeight);
}

return returnView;
Lantranduc
  • 29
  • 7