joh*_*ers 2 objective-c quartz-graphics uitextview ios
我一直在寻找一种简单的方法来为UITextView 的文本添加阴影,就像在UILabel中一样.我发现这个问题哪里有一个应该这样做的答案,然而,为什么会出现这种情况毫无意义.
问题:在UITextView层的图层中添加阴影不应该影响内部文本,而应该遮蔽整个对象,对吗?
在我的例子中,即使将阴影添加到textview的图层也没有任何效果(即使在添加QuartzCore标题之后).
我试过,发现你应该将UITextView的backgroundcolor设置为透明,所以阴影应该可以工作
UITextView *text = [[[UITextView alloc] initWithFrame:CGRectMake(0, 0, 150, 100)] autorelease];
text.layer.shadowColor = [[UIColor whiteColor] CGColor];
text.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
text.layer.shadowOpacity = 1.0f;
text.layer.shadowRadius = 1.0f;
text.textColor = [UIColor blackColor];
//here is important!!!!
text.backgroundColor = [UIColor clearColor];
text.text = @"test\nok!";
text.font = [UIFont systemFontOfSize:50];
[self.view addSubview:text];
Run Code Online (Sandbox Code Playgroud)

@adali的答案会奏效,但错了.您不应该为UITextView自身添加阴影以实现内部的可见视图.如您所见,通过将阴影应用于UITextView光标也将具有阴影.
应该使用的方法是NSAttributedString.
NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:textView.text];
NSRange range = NSMakeRange(0, [attString length]);
[attString addAttribute:NSFontAttributeName value:textView.font range:range];
[attString addAttribute:NSForegroundColorAttributeName value:textView.textColor range:range];
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor whiteColor];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
[attString addAttribute:NSShadowAttributeName value:shadow range:range];
textView.attributedText = attString;
Run Code Online (Sandbox Code Playgroud)
但是textView.attributedText适用于iOS6.如果必须支持较低版本,则可以使用以下方法.
CALayer *textLayer = (CALayer *)[textView.layer.sublayers objectAtIndex:0];
textLayer.shadowColor = [UIColor whiteColor].CGColor;
textLayer.shadowOffset = CGSizeMake(0.0f, 1.0f);
textLayer.shadowOpacity = 1.0f;
textLayer.shadowRadius = 0.0f;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5992 次 |
| 最近记录: |