本文介绍了如何使用drawInRect:withAttributes:而不是drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:在iOS 7中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此方法在iOS 7.0中已弃用:

This method is deprecated in iOS 7.0:

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

现在使用 drawInRect:withAttributes:代替。

我找不到fontSize和baselineAdjustment的attributeName。

I can't find the attributeName of fontSize and baselineAdjustment.

编辑

谢谢@Puneet的回答。

Thanks @Puneet answer.

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

Actually, I mean if there doesn't have these key, how to implement this method in 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;
}

我不知道如何传递 fontSize baselineAdjustment to

I don't know how to pass fontSize and baselineAdjustment to

attributes 字典。

例如

NSBaselineOffsetAttributeName key应传递 NSNumer 到它,但 baselineAdjustment 枚举

NSBaselineOffsetAttributeName key should pass a NSNumer to it, but the baselineAdjustment is Enum.

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

Isn't there have other way to pass the two variables?

推荐答案

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

You can use NSDictionary and apply attributes like this:

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

NSDictionary *attrsDictionary =

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

使用 attrsDictionary 作为参数。

参考:

参考:

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

SWIFT:IN String drawInRect is not available but we can use NSString instead:

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)

这篇关于如何使用drawInRect:withAttributes:而不是drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:在iOS 7中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:26