如何使用 drawInRect:withAttributes: 而不是 drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment: 在 iOS 7 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19442653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use drawInRect:withAttributes: instead of drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment: in iOS 7
提问by Vincent Sit
This method is deprecated in iOS 7.0:
此方法在 iOS 7.0 中已弃用:
drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:
Now use drawInRect:withAttributes:
instead.
现在drawInRect:withAttributes:
改用。
I can't find the attributeName of fontSize and baselineAdjustment.
我找不到fontSize 和baselineAdjustment 的attributeName。
Edit
编辑
Thanks @Puneet answer.
谢谢@Puneet 的回答。
Actually, I mean if there doesn't have these key, how to implement this method in iOS 7?
实际上,我的意思是如果没有这些密钥,如何在 iOS 7 中实现此方法?
Like below method:
像下面的方法:
+ (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;
}
I don't know how to pass fontSize
and baselineAdjustment
to
我不知道如何通过fontSize
和baselineAdjustment
到达
attributes
dictionary.
attributes
字典。
e.g.
例如
NSBaselineOffsetAttributeName
key should pass a NSNumer
to it, but the baselineAdjustment
is Enum
.
NSBaselineOffsetAttributeName
键应该传递 aNSNumer
给它,但baselineAdjustment
is Enum
。
Isn't there have other way to pass the two variables?
没有其他方法可以传递这两个变量吗?
采纳答案by Puneet Sharma
You can use NSDictionary
and apply attributes like this:
您可以使用NSDictionary
和应用这样的属性:
NSFont *font = [NSFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary =
[NSDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName,
[NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil];
Use attrsDictionary
as argument.
使用attrsDictionary
作为参数。
Refer: Attributed String Programming Guide
参考: 属性字符串编程指南
Refer: Standard Attributes
参考:标准属性
SWIFT:IN StringdrawInRect is not available but we can use NSStringinstead:
SWIFT:IN StringdrawInRect 不可用,但我们可以使用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)
回答by Florian
It is a little more complicated than before and you cannot use a minimum font size, but have to use minimum font scale factor. There is also a bug in the iOS SDK, which breaks it for most use cases (see notes at the bottom). Here is what you have to do:
它比以前稍微复杂一些,您不能使用最小字体大小,而必须使用最小字体比例因子。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];
Notes:
笔记:
There seems to be a bug in the iOS 7 SDK at least up to version 7.0.3: If you specify a custom font in the attributes, the miniumScaleFactor is ignored. If you pass nil for the attributes, the text is scaled correctly.
The
NSStringDrawingUsesLineFragmentOrigin
option is important. It tells the text drawing system, that the drawing rect's origin should be at the top left corner.There is no way to set the baselineAdjustment using the new method. You would have to do that yourself by calling
boundingRectWithSize:options:attributes:context:
first and then adjusting the rect before you pass it todrawWithRect:options:attributes:context
.
至少在版本 7.0.3 之前的 iOS 7 SDK 中似乎存在一个错误:如果您在属性中指定自定义字体,则会忽略 miniumScaleFactor。如果为属性传递 nil,则文本将正确缩放。
该
NSStringDrawingUsesLineFragmentOrigin
选项是非常重要的。它告诉文本绘图系统,绘图矩形的原点应该在左上角。无法使用新方法设置基线调整。您必须自己
boundingRectWithSize:options:attributes:context:
先调用,然后在将 rect 传递给drawWithRect:options:attributes:context
.