我在UIView
中添加了UITextView
。 UIView
有一些高度取决于屏幕尺寸。 UITextView
可以有更多或更少的文字。所以我想让UITextView
的高度动态化,所以如果文字多了,它应该有更多的高度,但它应该小于主视图的高度。如果文字少,那么它的高度应该较小。ios中textview的动态高度?
0
A
回答
5
大小尝试一个UITextView它的内容编程:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[textView setDelegate:self];
[textView setText:@"your long text"];
[textView setScrollEnabled:NO];
[self.view addSubview:textView];
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
-1
与此
-(void)dynamicTextSize{
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(200, 300, 200, 30)];
textView.center = self.view.center;
[self.view addSubview:textView];
NSString *string = @"This is pour text.a hjajksdkja kajhdsjk akjdhs jakhd skjahs ajkdhsjkad hskja akjdhs ajkhdjskar";
textView.text = string;
//UIFont *font = [UIFont fontWithName:@"Arial" size:16.0f];
NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:textView.font, NSFontAttributeName, nil];
textView.backgroundColor = [UIColor lightGrayColor];
CGRect frame = [textView.text boundingRectWithSize:CGSizeMake(textView.frame.size.width, 10000) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
CGRect mFrame = textView.frame;
mFrame.size.width = frame.size.width;
mFrame.size.height = frame.size.height;
textView.frame = mFrame;
//NSLog(@"frame2:%@",NSStringFromCGRect(textView.frame));
}
0
+0
请注意约束值是根据我的需要给出。您可以根据您的需要修改它们 –
+0
用于文本环绕图像我使用textview作为标签 – Techiee
+0
您甚至可以将文本包装在uilabel中,唯一的区别是标签是只读的而文本视图是可编辑的。 –
0
同第一个答案,但在夫特4:
let screenSize = UIScreen.main.bounds
let textView = UITextView.init(frame: CGRect(x: 1, y: 40, width: screenSize.width, height: screenSize.height))
textView.delegate = self as? UITextViewDelegate
textView.isScrollEnabled = false
view.addSubview(textView)
let fixedWidth = textView.frame.size.width
let newSize: CGSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT)))
var newFrame = textView.frame
newFrame.size = CGSize(width: CGFloat(fmaxf(Float(newSize.width), Float(fixedWidth))), height: newSize.height)
相关问题
- 1. textView Swift的动态单元格高度
- 2. Android:动态增加textview的高度
- 3. Android - TextView动态高度问题
- 4. Android - 将高度设置为TextView动态
- 5. 动态更改Textview-和TableviewCell行高度
- 6. UITableViewCell和heightForRowAtIndexPath的iOS动态高度
- 7. IOS对高度的限制(动态高度变化)
- 8. iOS隐藏TextView并不保留高度
- 9. iOS 7.1不允许动态tableview高度
- 10. 如何动态更改iOS中视图的高度和宽度?
- 11. TextView高度
- 12. 如何随着textView的高度增加动态地改变textView的超级视图高度
- 13. 无法在iOS中的WebView中获取动态html高度
- 14. 基于其他div动态高度的动态div高度
- 15. 如何自动增加在android中动态创建TextView的高度?
- 16. 在TableLayout中动态调整TextView宽度
- 17. 动态UITableViewCell高度
- 18. 动态高度列
- 19. 动态DIV高度
- 20. WPF:GroupBox动态高度
- 21. 动态UITableCellView高度
- 22. CardView动态高度
- 23. 动态高度tinyscrollbar
- 24. 动态高度jQuery
- 25. CollectionView在iOS中使用Swift 3的动态高度
- 26. ViewPager动态宽度高度
- 27. Collectionviewcell自动布局高度,动态高度,内容高度
- 28. TextView动态高度和y坐标不起作用
- 29. 无法根据textView动态增加UITableViewCell高度
- 30. SimpleModal动态模态高度
你不想让textview滚动吗? –
不,我不想滚动 – Techiee
使用故事板? –