如何在iOS 7中使用drawInRect:withAttributes:代替drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:

40

这种方法在iOS 7.0中已被弃用:

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

现在请使用drawInRect:withAttributes:代替。

我找不到字体大小和基线调整的属性名。

编辑

感谢@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字典中。

例如,NSBaselineOffsetAttributeName键应该向它传递一个NSNumber,但是baselineAdjustmentEnum

难道没有其他方法来传递这两个变量吗?


这些常量在“NSAttributedString”的UIKit附加文档中列出。 - rmaddy
1
谢谢,我也知道这个,但我不知道如何在iOS 7中实现这种方法。 - Vincent Sit
2个回答

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

参考: 标准属性

SWIFT:String中,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
请再次阅读问题。OP正在寻找两个特定的属性键。 - rmaddy
如何使字符串多行? - Olcay Ertaş

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似乎存在一个错误:如果您在属性中指定自定义字体,则最小缩放因子将被忽略。如果您将属性设置为nil,则文本将按比例缩放。

  • NSStringDrawingUsesLineFragmentOrigin选项很重要。它告诉文本绘制系统,绘图矩形的原点应位于左上角。

  • 使用新方法无法设置基线调整。您需要通过先调用boundingRectWithSize:options:attributes:context:并在将其传递给drawWithRect:options:attributes:context之前调整矩形来自行完成此操作。


3
更好的答案。简要说明,您可以使用NSBaselineOffsetAttributeName调整基线,但只有负值才能将文本向下移动。正值向上移动的文本会被限制为零。 - Benjohn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接