2011-06-29 56 views
3

我想在NSTextView中切換RTF格式。我曾嘗試以下操作:如何在NSTextView中以編程方式在可可中切換RTF格式的文本

[contentView setRichText:NO]; 
[contentView setImportsGraphics:NO]; 

,但是,這並沒有改變NSTextView內容,並仍然允許做的文本格式。

請讓我知道在NSTextView中切換/切換富文本格式的簡單方法,就像TextEdit一樣。

我已經檢查過「TextEdit」示例項目,但似乎很難從中找到可用的代碼。

謝謝

回答

4

從以下鏈接找到一些幫助。

click here to see solution

基於上文給出的鏈接的解決方案,我已創建類別我的視圖控制器如下:

#define TabWidth @"TabWidth" 

@interface MyViewController (Helper) 

- (NSDictionary *)defaultTextAttributes:(BOOL)forRichText; 
- (void)removeAttachments; 
- (void)setRichText:(BOOL)flag; 

@end 

@implementation MyViewController (Helper) 

- (NSDictionary *)defaultTextAttributes:(BOOL)forRichText { 
    static NSParagraphStyle *defaultRichParaStyle = nil; 
    NSMutableDictionary *textAttributes = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease]; 
    if (forRichText) { 
     [textAttributes setObject:[NSFont userFontOfSize:0.0] forKey:NSFontAttributeName]; 
     if (defaultRichParaStyle == nil) { // We do this once... 
      NSInteger cnt; 
      NSString *measurementUnits = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleMeasurementUnits"]; 
      CGFloat tabInterval = ([@"Centimeters" isEqual:measurementUnits]) ? (72.0/2.54) : (72.0/2.0); // Every cm or half inch 
      NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init] autorelease]; 
      [paraStyle setTabStops:[NSArray array]]; // This first clears all tab stops 
      for (cnt = 0; cnt < 12; cnt++) { // Add 12 tab stops, at desired intervals... 
       NSTextTab *tabStop = [[NSTextTab alloc] initWithType:NSLeftTabStopType location:tabInterval * (cnt + 1)]; 
       [paraStyle addTabStop:tabStop]; 
       [tabStop release]; 
      } 
      defaultRichParaStyle = [paraStyle copy]; 
     } 
     [textAttributes setObject:defaultRichParaStyle forKey:NSParagraphStyleAttributeName]; 
    } else { 
     NSFont *plainFont = [NSFont userFixedPitchFontOfSize:0.0]; 
     NSInteger tabWidth = [[NSUserDefaults standardUserDefaults] integerForKey:TabWidth]; 
     CGFloat charWidth = [@" " sizeWithAttributes:[NSDictionary dictionaryWithObject:plainFont forKey:NSFontAttributeName]].width; 
     if (charWidth == 0) charWidth = [[plainFont screenFontWithRenderingMode:NSFontDefaultRenderingMode] maximumAdvancement].width; 

     // Now use a default paragraph style, but with the tab width adjusted 
     NSMutableParagraphStyle *mStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease]; 
     [mStyle setTabStops:[NSArray array]]; 
     [mStyle setDefaultTabInterval:(charWidth * tabWidth)]; 
     [textAttributes setObject:[[mStyle copy] autorelease] forKey:NSParagraphStyleAttributeName]; 

     // Also set the font 
     [textAttributes setObject:plainFont forKey:NSFontAttributeName]; 
    } 
    return textAttributes; 
} 

/* Used when converting to plain text 
*/ 
- (void)removeAttachments { 
    NSTextStorage *attrString = [contentView textStorage]; 
    NSUInteger loc = 0; 
    NSUInteger end = [attrString length]; 
    [attrString beginEditing]; 
    while (loc < end) { /* Run through the string in terms of attachment runs */ 
     NSRange attachmentRange; /* Attachment attribute run */ 
     NSTextAttachment *attachment = [attrString attribute:NSAttachmentAttributeName atIndex:loc longestEffectiveRange:&attachmentRange inRange:NSMakeRange(loc, end-loc)]; 
     if (attachment) { /* If there is an attachment and it is on an attachment character, remove the character */ 
      unichar ch = [[attrString string] characterAtIndex:loc]; 
      if (ch == NSAttachmentCharacter) { 
       if ([contentView shouldChangeTextInRange:NSMakeRange(loc, 1) replacementString:@""]) { 
        [attrString replaceCharactersInRange:NSMakeRange(loc, 1) withString:@""]; 
        [contentView didChangeText]; 
       } 
       end = [attrString length]; /* New length */ 
      } 
      else loc++; /* Just skip over the current character... */ 
     } 
     else loc = NSMaxRange(attachmentRange); 
    } 
    [attrString endEditing]; 
} 

- (void)setRichText:(BOOL)flag { 
    NSDictionary *textAttributes; 

    BOOL isRichText = flag; 

    if (!isRichText) [self removeAttachments]; 

    [contentView setRichText:isRichText]; 
    [contentView setUsesRuler:isRichText]; /* If NO, this correctly gets rid 
               of the ruler if it was up */ 
    if (isRichText && NO) 
     [contentView setRulerVisible:YES]; /* Show ruler if rich, and desired */ 
    [contentView setImportsGraphics:isRichText]; 

    textAttributes = [self defaultTextAttributes:isRichText]; 

    if ([[contentView textStorage] length]) { 
     [[contentView textStorage] setAttributes:textAttributes range: NSMakeRange(0,[[contentView textStorage] length])]; 
    } 
    [contentView setTypingAttributes:textAttributes]; 
} 

@end 

在哪裏內容查看是NSTextView的IBOutlet中。希望這會幫助某人,或者讓我知道某人是否有較短的方法。

謝謝

+0

用法:[self setRichText:YES]; – AmitSri

相關問題