我在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. GridLayout中TextView的動態高度
- 2. Android:動態增加textview的高度
- 3. textView Swift的動態單元格高度
- 4. Android - 將高度設置爲TextView動態
- 5. 動態更改Textview-和TableviewCell行高度
- 6. Android - TextView動態高度問題
- 7. 在Android中動態設置TextView的寬度和高度
- 8. 的iOS:動態高度的UITableViewCell
- 9. UITableViewCell和heightForRowAtIndexPath的iOS動態高度
- 10. iOS - 動態更改uitableview的高度
- 11. 動態添加的TextView具有零寬度和高度
- 12. IOS對高度的限制(動態高度變化)
- 13. iOS隱藏TextView並不保留高度
- 14. iOS 7.1不允許動態tableview高度
- 15. 動態單元格高度iOS
- 16. TableLayout中的TextView高度
- 17. 如何動態更改iOS中視圖的高度和寬度?
- 18. TextView高度
- 19. 動態高度
- 20. 自動佈局ios中scrollview子視圖的動態高度
- 21. 如何隨着textView的高度增加動態地改變textView的超級視圖高度
- 22. 帶有動態高度的ImageView上方的TextView
- 23. 設置佈局高度,以Wrapcontent到動態創建的TextView
- 24. 無法在iOS中的WebView中獲取動態html高度
- 25. 基於其他div動態高度的動態div高度
- 26. 在ios中動態自動展開UITextView高度
- 27. 如何自動增加在android中動態創建TextView的高度?
- 28. 在TableLayout中動態調整TextView寬度
- 29. 使用動態高度頁腳滾動動態高度div
- 30. CardView動態高度
你不想讓textview滾動嗎? –
不,我不想滾動 – Techiee
使用故事板? –