ファイルのパスを1つの列に表示するNSTableViewがあります。ユーザーがテーブルビューのサイズを変更すると、パス名(例:/Users/name/testfile.m)のサイズを変更したいが、パス名の末尾に(例えば... name/testfile.m)を表示したいデフォルトで発生するように、パスの開始(例:/ Users/test/te ...)私は、私がやりたいことを成功させる関数を書いたが、ユーザーがテーブルビューを拡大するときに再描画する間に、テーブルビューがちらつく。私はこれを行うためのより良い、より洗練されたアルゴリズムがあるはずだと思うが、私はNSStringとStackoverflowのドキュメントを見て、私は良い解決策を与える何かを見つけることができません。誰かがこの問題のより洗練された解決策を持っている場合、それは非常に高く評価されるでしょう。ありがとう!乾杯、トロン文字列を切り捨てます。
私の現在の機能は:
-(NSString *) truncateString:(NSString *) myString withFontSize:(int) myFontSize withMaxWidth:(NSInteger) maxWidth
{
// Get the width of the current string for a given font
NSFont *font = [NSFont systemFontOfSize:myFontSize];
CGSize textSize = NSSizeToCGSize([myString sizeWithAttributes:[NSDictionary dictionaryWithObject:font forKey: NSFontAttributeName]]);
NSInteger lenURL =(int)textSize.width;
// Prepare for new truncated string
NSString *myStringShort;
NSMutableString *truncatedString = [[myString mutableCopy] autorelease];
// If the available width is smaller than the string, start truncating from first character
if (lenURL > maxWidth)
{
// Get range for first character in string
NSRange range = {0, 1};
while ([truncatedString sizeWithAttributes:[NSDictionary dictionaryWithObject:font forKey: NSFontAttributeName]].width > MAX(TKstringPad,maxWidth))
{
// Delete character at start of string
[truncatedString deleteCharactersInRange:range];
}
myStringShort = [NSString stringWithFormat:@"...%@",truncatedString];
}
else
{
myStringShort=myString;
}
return myStringShort;
}
また、インターフェイスビルダーで列だけでなくセルを選択し、そこで改行モードを変更することもできます。同じ効果が、別の場所で適用されただけです。 – Dondragmer
@Dondragmer +1。追加されました。ありがとう。 – justin
@ジャスティン:お返事ありがとうございます。私はあなたの提案に従おうとしましたが、うまくいかなかったのです。それは、私が自分のNSCellを持っているからです。どこで ' - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView'をオーバーライドして各テーブル列にテキストを描画しますか?私はIBにこれを変更せずに設定しようとしました。助言がありますか?ありがとう! –