2014-01-07 99 views
-2

我是iPhone開發者的初學者。我想給textView上的動態寬度和高度,我已經在下面的代碼中使用了寬度和高度,當我在textview中寫入時,它將在視圖上向上,而不是寫入幀的實際大小。texView動態高度和寬度

我用

-(void)viewDidLoad 
{ 
    writTxtView = [[UIView alloc]initWithFrame:CGRectMake(100,90,50,40)]; 
    [writTxtView setBackgroundColor:[UIColor clearColor]]; 
    [self.view addSubview:writTxtView]; 

    writeTxt = [[UITextView alloc]initWithFrame:CGRectMake(0,0,50,40)]; 
    [writeTxt setBackgroundColor:[UIColor clearColor]]; 
    [writeTxt setFont:[UIFont systemFontOfSize:12]]; 
    [writeTxt setTextColor:[UIColor whiteColor]]; 
    [writeTxt setDelegate:self]; 
    [writeTxt setHidden:YES]; 
    [writTxtView addSubview:writeTxt]; 

} 

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { 
    tooDoneBar.hidden = NO; 

    if([text isEqualToString:@"\n"]) { 
     //[textView resignFirstResponder]; 
     return YES; 
    } 

    textView.backgroundColor = [UIColor greenColor]; 
    CGFloat maxHeight = 312.0f; 
    NSLog(@"MytextViewframe :%@",NSStringFromCGRect(textView.frame)); 
    CGFloat fixedWidth = writTxtView.frame.origin.x; 
    NSLog(@"FixedWidth :%f",fixedWidth); 
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)]; 
    CGRect newFrame = textView.frame; 
    NSLog(@"newFrame :%@",NSStringFromCGRect(textView.frame)); 
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), fminf(newSize.height, maxHeight)); 
    textView.frame = newFrame; 
    NSLog(@"OthernewFrame :%@",NSStringFromCGRect(textView.frame)); 
    writTxtView.frame = newFrame; 
    return YES; 
} 

所以,給適用於我的代碼

謝謝你......

回答

0

使用的TextView代表任何建議及源代碼,並用它

[textView sizeToFit]; 
[textView layoutIfNeeded]; 

例如:

-(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ 

     // NSLog(@"%d", textView.tag); 
     frame = textView.frame; 
     frame.size.height = textView.contentSize.height; 
     frame.size.width = 730; 

     if (frame.size.height == textView.frame.size.height) { 
      NSLog(@"NO needs to Change"); 
     } else { 
      textView.frame = frame; 
      NSLog(@"NEeds to Change"); 
     } 
     [textView sizeToFit]; 
     [textView layoutIfNeeded]; 
     return YES; 
    } 
0
-(CGSize)GetHeightDyanamic:(UITextView*)textview 
    { 
     NSRange range = NSMakeRange(0, [textview.text length]); 
     CGSize constraint; 
     if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) { 
     constraint= CGSizeMake(722 ,MAXFLOAT); 
    } 
    else 
     constraint= CGSizeMake(287 ,MAXFLOAT); 
     CGSize size; 
     NSString *ver = [[UIDevice currentDevice] systemVersion]; 
    float ver_float = [ver floatValue]; 
    if (ver_float>6.0) { 
     NSDictionary *attributes = [textview.attributedText attributesAtIndex:0 effectiveRange:&range]; 
     CGSize boundingBox = [textview.text boundingRectWithSize:constraint options: NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; 

     size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height)); 
    } 
    else{ 


     size = [textview.text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]; 
    } 
    return size; 
} 

我希望它有用 它的UILabel工作... so..try它