我有两种类型,设置动态高度为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