如何在可可中以编程方式在NSTextView中切换RTF格式 [英] how to toggle rich text formatting in NSTextView programmatically in cocoa

查看:60
本文介绍了如何在可可中以编程方式在NSTextView中切换RTF格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在NSTextView中切换RTF格式.我尝试过以下操作:

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

但是,这并没有改变NSTextView的内容,仍然允许进行文本格式设置.

请让我知道在NSTextView中切换/切换RTF格式的简单方法,就像TextEdit一样.

我已经检查了"TextEdit"示例项目,但是似乎很难从中找到可用的代码.

谢谢

解决方案

从以下链接中获得了一些帮助.

contentView是NSTextView的IBOutlet.希望这可以帮助某人,或者让我知道某人是否使用较短的方法.

谢谢

I want to toggle rich text formatting in NSTextView. I have tried following:

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

but, that didn't changed the NSTextView content and still allowing to do the text formatting.

Please let me know the simple way to toggle/switch rich text formatting in NSTextView just like TextEdit.

I already check the "TextEdit" sample project, but it seems to be very hard to find the usable code from it.

Thanks

解决方案

Found some help from following link.

click here to see solution

Based on solution given in above link, i have created category for my view controller as follows:

#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

Where contentView is IBOutlet of NSTextView. Hope this will help someone or let me know if someone has shorter method.

Thanks

这篇关于如何在可可中以编程方式在NSTextView中切换RTF格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆