Air*_*Ltd 3 nsstring ios nsshadow ios8
我正在尝试使用投影来绘制多行文本,而不使用已弃用的API.它适用于单行.相关代码如下所示:
-(void)drawRect:(CGRect)rect
{
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
paragraph.alignment = NSTextAlignmentCenter;
UIFont *f = [UIFont systemFontOfSize:20.0];
NSMutableDictionary *attributes = [NSMutableDictionary new];
[attributes setValuesForKeysWithDictionary:@{ NSFontAttributeName : f,
NSParagraphStyleAttributeName : paragraph,
NSForegroundColorAttributeName : [UIColor blueColor] }];
NSShadow * shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(4,4);
shadow.shadowColor = [UIColor redColor];
[attributes setValue:shadow forKey:NSShadowAttributeName];
rect.origin.y = 100;
[@"test string on one line" drawInRect:rect withAttributes:attributes];
rect.origin.y = 150;
[@"test string spanning more than one line" drawInRect:rect withAttributes:attributes];
}
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:

我在iPhone 5(7.1.2),iPhone 6(8.0)上进行了测试,使用xCode 6构建.我在使用xCode 5进行构建时也在iPhone 5上进行了测试.
更多的实验,我发现答案是使用NSAttributedString.
虽然这没有显示阴影:
NSString *s = @"test string spanning more than one line"
[s drawInRect:rect withAttributes:attributes]
Run Code Online (Sandbox Code Playgroud)
这样做:
NSAttributedString *as = [[NSAttributedString alloc] initWithString:s attributes:attributes];
[as drawInRect:rect];
Run Code Online (Sandbox Code Playgroud)
我不认为这在任何地方都有记录,否则我很乐意听到.
| 归档时间: |
|
| 查看次数: |
274 次 |
| 最近记录: |