4 回答

TA貢獻1815條經驗 獲得超10個贊
因為題主這兩個 View 之間是用 Segue 連接的,所以可以給第一個 View 中加入
prepareForSegue 方法來實現此功能。舉個例子,我有兩個 View,第一個叫做 View Controller,第二個叫做 Second View Controller,分別對應 ViewController.swift 和 SecondViewController.swift,則:
Main.storyboard 的結構:
(注意兩個 View 之間要用 Modal 類型的 Segue 來連接,如果使用 Push 而且沒有相應的 Navigation Controller 的話程序會停止響應)
ViewController.swift 的內容(其實僅僅加了一個 prepareForSegue 方法):
SecondViewController.swift 的內容:
最后程序運行的結果:
在第一個 View 中輸入:
點擊 Send 按鈕后在第二個 View 中顯示:

TA貢獻1803條經驗 獲得超6個贊
在viewController里 增加一個IBOutlet 鏈接到你的自定義的view。
獲取一下它的frame屬性就知道它的高寬了。
setFrame來設置它的高寬

TA貢獻1796條經驗 獲得超4個贊
其實動態獲取字符串NSString的內容寬度高度最主要的還是API NSString類提供的一個方法:
sizeWithFont: constrainedToSize
例如:
CGSize feelSize = [feeling sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(190,200)];
float feelHeight = feelSize.height;
這樣就可以根據自己定義的長高的最大限值來獲取當前文本的size。

TA貢獻2011條經驗 獲得超2個贊
//初始化label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
//設置自動行數與字符換行
[label setNumberOfLines:0];
label.lineBreakMode = UILineBreakModeWordWrap;
// 測試字串
NSString *s = @"這是一個測試?。。?br/>UIFont *font = [UIFont fontWithName:@"Arial" size:12];
//設置一個行高上限
CGSize size = CGSizeMake(320,2000);
//計算實際frame大小,并將label的frame變成實際大小
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
。。
添加回答
舉報