3
我想要使用陰影繪製多行文本而不使用不贊成使用的API。它適用於單行。相關的代碼看起來是這樣的:無法在多行nsstring上得到陰影
-(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];
}
和輸出看起來像這樣:
我已經在iPhone 5(7.1.2),iPhone 6(8.0),建築測試這與xCode 6.我也測試了它在iPhone 5上使用xCode 5構建時。
哎呀,我會剛剛使用兩個相同的標籤,一個疊加另一個和偏移量,透明背景。 – 2014-09-24 12:26:04
這對於非零的shadowBlurRadius會起作用嗎? – 2014-09-24 12:28:33