我有兩種類型,設置動態高度爲UITextView
看都是波紋管......
UPDATE:
首先創建UITextView
編程像波紋管.. 。
-(IBAction)btnAddTextView:(id)sender
{
UIView *viewTxt = [[UIView alloc]initWithFrame:CGRectMake(imgBackBoard.center.x - 100,20, 220, 84)];
[viewTxt setBackgroundColor:[UIColor clearColor]];
viewTxt.userInteractionEnabled = YES;
UITextView *txtAddNote=[[UITextView alloc]initWithFrame:CGRectMake(20,20, 180, 44)];
[txtAddNote setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
[txtAddNote setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
txtAddNote.layer.borderWidth = 2.0;
txtAddNote.layer.borderColor= [UIColor redColor].CGColor;
viewTxt.tag = 111;
txtAddNote.tag = 111;
txtAddNote.userInteractionEnabled= YES;
txtAddNote.delegate = self;
txtAddNote.textColor = [UIColor whiteColor];
[viewTxt addSubview:txtAddNote];
[viewBoard addSubview:viewTxt];
[txtAddNote release];
}
首先
1.此波紋管的方法是UITextViewDelegate
委託方法。在.h
類
添加此UITextViewDelegate
然後給textView.delegate
到self
像波紋管..
yourTextView.delegate = self;
,並使用粘貼此波紋管方法...
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.contentSize.height);
return YES;
}
這裏的時候,你UITextView
編輯在一段時間與它的文本,textView的內容大小是變化的。
2.用我的波紋管自定義方法爲UILable
,UITextField
設置動態高度也UITextView
-(float) calculateHeightOfTextFromWidth:(NSString*) text: (UIFont*)withFont: (float)width :(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
使用這種方法就像波紋管......
CGSize sizeToMakeLabel = [yourTextView.text sizeWithFont:yourTextView.font];
yourTextView.frame = CGRectMake(yourTextView.frame.origin.x, yourTextView.frame.origin.y,
sizeToMakeLabel.width, sizeToMakeLabel.height);
我希望這是對你有幫助:)
提供一些例子中,你是如何準確地設置UIEdgeInsets爲了你?編輯後添加 – 2013-05-02 09:51:13
:那麼這是另一種類型的問題,因爲正確的方法是按照您指定的方式使用UIEdgeInsets(至少在您的初始問題上回答了這個屬性)。 – 2013-05-02 10:30:18
請參閱此處發佈的同一問題的答案:http://stackoverflow.com/a/24871575/3499969 – 2014-08-14 22:03:47