如何在NSAttributedString中為文本設置“隱藏”屬性?

[英]How can I set a “hidden” attribute for text inside NSAttributedString?


I have a Cocoa app with an NSTextView control which holds its text in an NSAttributedString (actually I believe it's a NSMutableAttributedString). I can easily set and modify different text attributes (such as font, underline, etc.) on different character ranges inside that string.

我有一個帶有NSTextView控件的Cocoa應用程序,它將文本保存在NSAttributedString中(實際上我相信它是一個NSMutableAttributedString)。我可以輕松地在該字符串內的不同字符范圍內設置和修改不同的文本屬性(如字體,下划線等)。

However, I want to set a part of the text as hidden (similar to the effect of the CSS attribute display: none). When an external event occurs (say a button clicked), I want to unhide or hide that specific range of characters.

但是,我想將文本的一部分設置為隱藏(類似於CSS屬性display:none的效果)。當外部事件發生時(比如單擊一個按鈕),我想取消隱藏或隱藏特定范圍的字符。

Is there anyway to do this with NSAttributedString?

無論如何使用NSAttributedString執行此操作?

5 个解决方案

#1


Another possibility would be to use a custom attribute on the text you want to hide, and then write your own method in a category on NSAttributedString that creates a new attributed string that excludes the text marked as hidden.

另一種可能性是在要隱藏的文本上使用自定義屬性,然后在NSAttributedString上的類別中編寫自己的方法,該類別創建一個新的屬性字符串,該字符串排除標記為隱藏的文本。

- (NSAttributedString *)attributedStringWithoutHiddenText {
    NSMutableAttributedString *result = [[[NSMutableString alloc] init] autorelease];
    NSRange fullRange = NSMakeRange(0, [self length]);
    NSRange range = NSZeroRange;
    while (NSMaxRange(range) < [self length]) {
        NSDictionary *attributes = [self attributesAtIndex:range.location longestEffectiveRange:&range inRange:fullRange];
        if ([[attributes objectForKey:MyHiddenTextAttribute] boolValue])
            continue;

        NSAttributedString *substring = [[NSAttributedString alloc] initWithString:[[self string] substringWithRange:range] attributes:attributes];
        [result appendAttributedString:substring];
        [substring release];
    }
    return result;
}

Caveat: I totally just wrote this off the top of my head, and it's not guaranteed to compile, work, light your hard drive on fire, not kick your dog, etc.

警告:我完全把它寫在了我的頭頂,而且不能保證編譯,工作,點亮你的硬盤着火,不要踢你的狗等等。

This would generate a string that's appropriate for drawing, but you would still need the original string for accessing any of the hidden text. Depending on the size of your strings, this could be a big memory overhead.

這將生成一個適合繪圖的字符串,但您仍然需要原始字符串來訪問任何隱藏文本。根據字符串的大小,這可能是一個很大的內存開銷。

#2


The supported attributes for NSAttributedString are listed in AppKit's NSAttributedString.h header. There's no key like 'hidden' or 'visible'. The attributes (styles) are not derived from html and cannot express all css features.

NSAttributedString支持的屬性列在AppKit的NSAttributedString.h頭文件中。沒有像'隱藏'或'可見'這樣的關鍵。屬性(樣式)不是從html派生的,也不能表達所有css功能。

Nevertheless, to hide a range of characters you can set the foreground color to transparent:

不過,要隱藏一系列字符,您可以將前景色設置為透明:

NSMutableAttributedString* myString;

[myString addAttribute:NSForegroundColorAttributeName
                 value:[NSColor clearColor]
                 range:NSMakeRange(0, 10)];

#3


A simple option is to set the font size of the hidden text to 0

一個簡單的選項是將隱藏文本的字體大小設置為0

#4


As far as I know there are no invisible type attributes for NSAttributedString, however you could create a subclass of NSAttributedString (or the Mutable version, but that might not be necessary) that overrides the drawInRect: to avoid drawing the portion of the text that has your attribute.

據我所知,NSAttributedString沒有不可見的類型屬性,但是你可以創建一個NSAttributedString的子類(或者可能沒有必要的Mutable版本)來覆蓋drawInRect:以避免繪制文本的一部分你的屬性。

But this could be a little messy.

但這可能有點凌亂。

#5


I realize this is a very old thread, but the other other option is to do custom glyph rendering. There was a session about advanced text handling techniques at WWDC 2010 that covered code folding. This uses a similar technique to what you'll need to do with this and that is to examine the text as its being lain out and render the null glyph for your hidden text rather than the actual string. The session is Session 114 - Advanced Cocoa Text Tips and Tricks from WWDC 2010 videos. If you're a Mac Developer Program member, you can download these through the developer portal.

我意識到這是一個非常古老的線程,但另一個選擇是做自定義字形渲染。在WWDC 2010上有一個關於高級文本處理技術的會議,其中涉及代碼折疊。這使用了與你需要做的相似的技術,那就是檢查文本是否為lain out並為隱藏文本而不是實際字符串呈現空字形。會議是第114節 - 來自WWDC 2010視頻的高級可可文本提示和技巧。如果您是Mac開發人員計划成員,可以通過開發人員門戶網站下載這些成員。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/04/72a277768343fd98afa9be69966e4061.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com