在iOS 7.0中不推荐使用此方法:
drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment:
Run Code Online (Sandbox Code Playgroud)
现在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;
}
Run Code Online (Sandbox Code Playgroud)
我不知道怎么打发fontSize,并baselineAdjustment以
attributes 字典.
例如
NSBaselineOffsetAttributeName …
使用 Swift 的一个常见烦恼是子类必须实现 init?(coder),即使您不使用 Storyboard。
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Run Code Online (Sandbox Code Playgroud)
这并不优雅。如何避免在子类中覆盖它?
这是一个带有相关值的常见枚举.
enum MultiplierType {
case width(Double)
case height(Double)
case xxxxx1(Double)
case xxxxx2(Double)
case xxxxx3(Double)
var value: Double {
switch self {
// Normal way.
case let .width(value):
return value
case let .height(value):
return value
case let .xxxxx1(value):
return value
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何做到这一点?
var value: Double {
switch self {
// How to get the value in one case?
case let .width(value), .height(value), .xxx:
return value
}
}
Run Code Online (Sandbox Code Playgroud)
要么
var value: Double {
switch self {
// How to …Run Code Online (Sandbox Code Playgroud) #import "ViewController.h"
@interface ViewController ()
{
NSString *string;
}
@end
@implementation ViewController{
NSString *string;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Run Code Online (Sandbox Code Playgroud)
这两个陈述有什么区别?哪个更好?