textView的动态高度和宽度

时间:2014-01-07 06:55:13

标签: ios iphone objective-c xcode

我是iPhone开发人员的初学者。我想在textView上给出动态宽度和高度我在下面的代码中使用,当我在textview中写入时,它会在视图上朝上而不是写实际的帧大小。

我用过

-(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;
}

所以,提供适用于我的代码的任何建议和源代码

谢谢......

2 个答案:

答案 0 :(得分:0)

使用textView Delegates并使用它

 [textView sizeToFit];
 [textView layoutIfNeeded];

对于Ex:

 -(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;
    }

答案 1 :(得分: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工作......所以......它