小编Vin*_*Sit的帖子

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

在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 …

nsstring ios ios7

40
推荐指数
2
解决办法
4万
查看次数

Swift 如何避免在子类中覆盖 init?(编码器)

使用 Swift 的一个常见烦恼是子类必须实现 init?(coder),即使您不使用 Storyboard。

required init?(coder aDecoder: NSCoder) {
  fatalError("init(coder:) has not been implemented")
}
Run Code Online (Sandbox Code Playgroud)

这并不优雅。如何避免在子类中覆盖它?

ios swift

4
推荐指数
1
解决办法
751
查看次数

swift enum在单个switch-case中获取具有相同参数的多个case的关联值

这是一个带有相关值的常见枚举.

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)

ios swift

3
推荐指数
1
解决办法
621
查看次数

这两个陈述有什么区别?

#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)

这两个陈述有什么区别?哪个更好?

objective-c

1
推荐指数
1
解决办法
145
查看次数

标签 统计

ios ×3

swift ×2

ios7 ×1

nsstring ×1

objective-c ×1