ファイルのパスを 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;
}