2011-04-09 143 views
0

我有一個NSTextField在其中的NSView。問題是,每次顯示視圖之前,NSTextField的大小都會改變,我希望NSView的大小隨之改變。因此,如果textfield長度爲2行,nsview將會很小(只是幾乎不包圍文本框),但是當它的10行長時,我不希望視圖的大小切斷文本字段,我希望視圖隨它一起增長。NSView調整大小

我該怎麼做?謝謝。

+1

我想你會需要添加一些更多的信息。例如,如何輸入文本,它是否包含新行,你想寬度,高度或兩者都改變​​。我的想法是你需要計算字符的長度和調整視圖的矩形即每行長度爲20個字符,每增加20個字符大小 – markhunte 2011-04-09 08:10:16

回答

0

這是我在我的tableview使用,但它應同樣適用於您的情況:

float textLabelWidth = 190; 

    NSString *Text = [[deals objectAtIndex:[indexPath section]] objectForKey:@"title"]; 
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:13.0]; 
    CGSize constraintSize = CGSizeMake(textLabelWidth, MAXFLOAT); 
    CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return labelSize.height + 20; // <--- this is what your NSView's height should be, just edit the textLabelWidth and padding (in this case 20) to whatever you desire. 

希望幫助!

0

您需要設置「contentHugging和contentCompression」優先級。

簡而言之:

contentHugging: sets the preference where a control resists being made *larger* than it's intrinsic size 

contentCompression: sets the preference where a control resists being made *smaller* than it's intrinsic size 

蘋果這裏有一個參考:

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html

具體向下滾動到Setting Content-Hugging and Compression-Resistance Priorities部分。

這與文本框等什麼交易...削波(即當一個文本框有「一些測試值」變成了「一些testV ......」等等......