如何使用drawInRect:withAttributes:而不是drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:在iOS 7中

时间:2013-10-18 06:02:19

标签: ios nsstring ios7

此方法在iOS 7.0中已弃用:

drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:

现在改用drawInRect:withAttributes:

我找不到fontSize和baselineAdjustment的attributeName。

修改

谢谢@Puneet的回答。

实际上,我的意思是如果没有这些密钥,如何在iOS 7中实现这个方法?

如下方法:

+ (CGSize)drawWithString:(NSString *)string atPoint:(CGPoint)point forWidth:(CGFloat)width withFont:(UIFont *)font fontSize:(CGFloat)fontSize
           lineBreakMode:(IBLLineBreakMode)lineBreakMode
      baselineAdjustment:(UIBaselineAdjustment)baselineAdjustment {
    if (iOS7) {
        CGRect rect = CGRectMake(point.x, point.y, width, CGFLOAT_MAX);

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = lineBreakMode;

        NSDictionary *attributes = @{NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle};

        [string drawInRect:rect withAttributes:attributes];

        size = CGSizeZero;
    }
    else {
        size = [string drawAtPoint:point forWidth:width withFont:font fontSize:fontSize lineBreakMode:lineBreakMode baselineAdjustment:baselineAdjustment];
    }
    return size;
}

我不知道如何将fontSizebaselineAdjustment传递给

attributes字典。

e.g。

NSBaselineOffsetAttributeName密钥应将NSNumer传递给它,但baselineAdjustmentEnum

是否有其他方法可以传递这两个变量?

2 个答案:

答案 0 :(得分:38)

您可以使用NSDictionary并应用以下属性:

NSFont *font = [NSFont fontWithName:@"Palatino-Roman" size:14.0];

NSDictionary *attrsDictionary =

[NSDictionary dictionaryWithObjectsAndKeys:
                              font, NSFontAttributeName,
                              [NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil];

使用attrsDictionary作为参数。

参考:Attributed String Programming Guide

参考:Standard Attributes

<强> SWIFT: IN 字符串 drawInRect不可用,但我们可以使用 NSString

let font = UIFont(name: "Palatino-Roman", size: 14.0)
let baselineAdjust = 1.0
let attrsDictionary =  [NSFontAttributeName:font, NSBaselineOffsetAttributeName:baselineAdjust] as [NSObject : AnyObject]
let str:NSString = "Hello World"
str.drawInRect(CGRectZero, withAttributes: attrsDictionary)

答案 1 :(得分:34)

它比以前复杂一点,你不能使用最小字体大小,但必须使用最小字体比例因子。 iOS SDK中还有一个错误,它在大多数用例中都会破坏它(请参阅底部的注释)。这是你要做的:

// Create text attributes
NSDictionary *textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:18.0]};

// Create string drawing context
NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];
drawingContext.minimumScaleFactor = 0.5; // Half the font size

CGRect drawRect = CGRectMake(0.0, 0.0, 200.0, 100.0);
[string drawWithRect:drawRect
             options:NSStringDrawingUsesLineFragmentOrigin
          attributes:textAttributes
             context:drawingContext];

备注:

  • iOS 7 SDK中似乎存在至少版本7.0.3的错误:如果在属性中指定自定义字体,则忽略miniumScaleFactor。如果为属性传递nil,则正确缩放文本。

  • NSStringDrawingUsesLineFragmentOrigin选项非常重要。它告诉文本绘图系统,绘图矩形的原点应该在左上角。

  • 无法使用新方法设置baselineAdjustment。您必须首先调用boundingRectWithSize:options:attributes:context:,然后在将其传递给drawWithRect:options:attributes:context之前调整矩形来自行完成。