如何在NSTextView中统一缩放富文本?

Tod*_*orf 12 cocoa objective-c nstextview appkit

背景:

我有一个普通的基于文档的Cocoa Mac OS X应用程序,它使用NSTextView富文本输入.用户可以编辑文本的字体系列,点大小和颜色NSTextView.

基础SDK:10.7
部署目标:10.6


题:

我想NSTextView在用户编辑文本时以编程方式(包括)实现整个UI的缩放.缩放框架NSTextView是没有问题的.但我不知道如何缩放视图中的可编辑文本,该文本可能在整个文本运行的不同子部分中包含多个不同的点大小.

如何将统一比例因子应用于显示的富文本NSTextView

这应该与"富文本",使得用户的字体,颜色发挥很好,尤其是点大小(这可能是在文字的运行的不同位置有不同)将被保留,但按比例均匀/相对.

这可能是我的Base SDK和部署目标吗?是否可以使用较新的Base SDK或部署目标?

Mar*_*unz 11

如果目的是缩放视图(而不是实际更改字符串中的属性),我建议使用scaleUnitSquareToSize:method:以及ScalingScrollView(可与TextEdit示例代码一起使用)以获得正确的滚动条行为.

ScalingScrollView的核心部分是:

- (void)setScaleFactor:(CGFloat)newScaleFactor adjustPopup:(BOOL)flag
{
CGFloat oldScaleFactor = scaleFactor;
    if (scaleFactor != newScaleFactor)
    {
        NSSize curDocFrameSize, newDocBoundsSize;
        NSView *clipView = [[self documentView] superview];

        scaleFactor = newScaleFactor;

        // Get the frame.  The frame must stay the same.
        curDocFrameSize = [clipView frame].size;

        // The new bounds will be frame divided by scale factor
        newDocBoundsSize.width = curDocFrameSize.width / scaleFactor;
        newDocBoundsSize.height = curDocFrameSize.height / scaleFactor;
    }
    scaleFactor = newScaleFactor;
    [scale_delegate scaleChanged:oldScaleFactor newScale:newScaleFactor]; 
}
Run Code Online (Sandbox Code Playgroud)

scale_delegate是你的委托,它可以调整你的NSTextView对象:

- (void) scaleChanged:(CGFloat)oldScale newScale:(CGFloat)newScale
{
    NSInteger     percent  = lroundf(newScale * 100);

    CGFloat scaler = newScale / oldScale;   
    [textView scaleUnitSquareToSize:NSMakeSize(scaler, scaler)];

    NSLayoutManager* lm = [textView layoutManager];
    NSTextContainer* tc = [textView textContainer];
    [lm ensureLayoutForTextContainer:tc];
}
Run Code Online (Sandbox Code Playgroud)

scaleUnitSquareToSize:方法相对于其当前状态进行缩放,因此您可以跟踪比例因子,然后将绝对比例请求(200%)转换为相对比例请求.


hfo*_*sli 6

适用于iOS和Mac OS

@implementation NSAttributedString (Scale)

- (NSAttributedString *)attributedStringWithScale:(double)scale
{
    if(scale == 1.0)
    {
        return self;
    }

    NSMutableAttributedString *copy = [self mutableCopy];
    [copy beginEditing];

    NSRange fullRange = NSMakeRange(0, copy.length);

    [self enumerateAttribute:NSFontAttributeName inRange:fullRange options:0 usingBlock:^(UIFont *oldFont, NSRange range, BOOL *stop) {
        double currentFontSize = oldFont.pointSize;
        double newFontSize = currentFontSize * scale;

        // don't trust -[UIFont fontWithSize:]
        UIFont *scaledFont = [UIFont fontWithName:oldFont.fontName size:newFontSize];

        [copy removeAttribute:NSFontAttributeName range:range];
        [copy addAttribute:NSFontAttributeName value:scaledFont range:range];
    }];

    [self enumerateAttribute:NSParagraphStyleAttributeName inRange:fullRange options:0 usingBlock:^(NSParagraphStyle *oldParagraphStyle, NSRange range, BOOL *stop) {

        NSMutableParagraphStyle *newParagraphStyle = [oldParagraphStyle mutableCopy];
        newParagraphStyle.lineSpacing *= scale;
        newParagraphStyle.paragraphSpacing *= scale;
        newParagraphStyle.firstLineHeadIndent *= scale;
        newParagraphStyle.headIndent *= scale;
        newParagraphStyle.tailIndent *= scale;
        newParagraphStyle.minimumLineHeight *= scale;
        newParagraphStyle.maximumLineHeight *= scale;
        newParagraphStyle.paragraphSpacing *= scale;
        newParagraphStyle.paragraphSpacingBefore *= scale;

        [copy removeAttribute:NSParagraphStyleAttributeName range:range];
        [copy addAttribute:NSParagraphStyleAttributeName value:newParagraphStyle range:range];
    }];

    [copy endEditing];
    return copy;
}

@end
Run Code Online (Sandbox Code Playgroud)