Ios 使用QuartzCore为UITextView创建阴影

Ios 使用QuartzCore为UITextView创建阴影,ios,uitextview,shadow,quartz-core,Ios,Uitextview,Shadow,Quartz Core,我已经使用以下代码为我的UITextView创建了一个阴影QuartzCore myTextView.layer.masksToBounds = NO; myTextView.layer.shadowColor = [UIColor blackColor].CGColor; myTextView.layer.shadowOpacity = 0.7f; myTextView.layer.shadowOffset = CGSizeMake(2.0f, 2.0f); myTextView.layer.

我已经使用以下代码为我的
UITextView
创建了一个阴影
QuartzCore

myTextView.layer.masksToBounds = NO;
myTextView.layer.shadowColor = [UIColor blackColor].CGColor;
myTextView.layer.shadowOpacity = 0.7f;
myTextView.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
myTextView.layer.shadowRadius = 8.0f;
myTextView.layer.shouldRasterize = YES;
它创建了一个
阴影
看起来也不错。
这里是我对上述代码的输出

但是,当我尝试向
myTextView
添加文本时,我的textView文本超出了范围,看起来超出了
myTextView
的范围,如下所示

只有在我添加阴影时才会发生。如果我不添加阴影,
textView
中的文本不会显示奇怪。我做错了什么??我怎样才能克服这个问题?为什么会这样

更新:

@borrrden
说 我发现它正在发生,因为设置了
maskToBounds=NO如果我们设置了
YES
则无法获得阴影。原因这里是一个

没有“正确”的解决方案,因为UIView行为。如果masksToBounds为“否”,则延伸到层边界之外的任何子层都将可见。和UITextField在UITextField层外滚动文本


在UITextView后面添加清晰视图,并在其上放置阴影。

只需在textView下添加另一个UIView,并将其图层设置为显示阴影(不要忘记将其背景颜色设置为除清晰颜色以外的颜色,否则不会绘制阴影)


除了上一个答案外,只需将所有属性从原始uiTextView复制到helper视图:

UITextView *helperTextView = [[UITextView alloc] init];
helperTextView = textView;  //copy all attributes to the helperTextView;

shadowTextView = [[UIView alloc] initWithFrame:textView.frame];
shadowTextView.backgroundColor = textView.backgroundColor;
shadowTextView.layer.opacity = 1.0f;
shadowTextView.layer.masksToBounds = NO;
shadowTextView.layer.shadowColor = [UIColorFromRGB(0x00abff)CGColor];
shadowTextView.layer.shadowOpacity = 1.0f;
shadowTextView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
shadowTextView.layer.shadowRadius = 10.0f;
shadowTextView.layer.cornerRadius = 8.0f;
shadowTextView.layer.shouldRasterize = YES;

[self.view addSubview:shadowTextView];
[self.view addSubview:helperTextView];

masksToBounds
设置为
NO
。如果没有此选项,视图将不会尝试剪裁其内容;然后它不会显示任何阴影。请尝试将
textView.contentInset
设置为适当的值,以便它不会在那里重叠。@ACB否。我认为这与contentInset无关。内容插入只是为了在所有侧面留出一些空间。不管怎样,我按照你说的做了尝试,但没有改变任何事情。你说的清晰视野是什么意思?如果我在textView后面有另一个superview呢?我想他的意思是在textView后面添加一个单独的UIVIew作为容器视图,并给它添加阴影效果。如果在多个位置使用此textview,可能可以将UIVIew子类化,并在自定义实现中将textview添加为子视图+1的建议。@ACB但实际上我正试图为我的文本视图添加一个阴影。但你给出的是另一种解决方案。我已经知道要在textView后面添加UIView,使其看起来像我想要的。不管怎样,这回答了我的问题:我怎样才能克服这个问题?。但为什么我变得像我的输出?如果我只想在textView中添加阴影,该怎么办?如果我有大量的文本视图怎么办??然后我需要不自觉地创建大量的UIView,并为它们提供出口…+1建议有另一种方式。@R.A由于UIView行为,没有“正确”的解决方案。如果masksToBounds为“否”,则延伸到层边界之外的任何子层都将可见。和UITextField在UITextField层外滚动文本。我需要在textView中添加阴影,而不是在容器视图中添加阴影。你能帮我处理一下我的输出吗?为什么我在文本视图中添加了阴影???+1,建议有一个替代方法。。
UITextView *helperTextView = [[UITextView alloc] init];
helperTextView = textView;  //copy all attributes to the helperTextView;

shadowTextView = [[UIView alloc] initWithFrame:textView.frame];
shadowTextView.backgroundColor = textView.backgroundColor;
shadowTextView.layer.opacity = 1.0f;
shadowTextView.layer.masksToBounds = NO;
shadowTextView.layer.shadowColor = [UIColorFromRGB(0x00abff)CGColor];
shadowTextView.layer.shadowOpacity = 1.0f;
shadowTextView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
shadowTextView.layer.shadowRadius = 10.0f;
shadowTextView.layer.cornerRadius = 8.0f;
shadowTextView.layer.shouldRasterize = YES;

[self.view addSubview:shadowTextView];
[self.view addSubview:helperTextView];