- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想子类化 UITextView 以便能够绘制带有自定义边框和顶部+左侧内部阴影的圆形矩形。
我在为任何(静态边界) View 创建这种效果(通过阅读 this 帖子)方面取得了巨大成功,但在 ScrollView 方面遇到了问题。
我在我的自定义类的 -setFrame 实例方法中做的这个效果:
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
UIColor *borderColor = [UIColor colorWithRed:BORDER_COLOR_RED green:BORDER_COLOR_GREEN blue:BORDER_COLOR_BLUE alpha:BORDER_COLOR_ALPHA];
// Store index of the shadow sublayer for future use
[self setShadowLayerIndex:[LayerFormatter formatAsRoundRectWithShadowOn:self withBackgroundColor:[UIColor whiteColor] andBorderColor:borderColor]];}
formatAsRoundRectWithShadowOn: 是一个类方法,定义为:
+(NSUInteger)formatAsRoundRectWithShadowOn:(UIView*)view withBackgroundColor:(UIColor *)backgroundColor andBorderColor:(UIColor *)borderColor {
if([view isKindOfClass:[UITextField class]])
((UITextField*)view).borderStyle = UITextBorderStyleNone;
view.backgroundColor = backgroundColor;
view.layer.borderWidth = 1.0;
view.layer.borderColor = [borderColor CGColor];
view.layer.cornerRadius = CORNER_RADIUS;
view.layer.masksToBounds = YES;
//Add some insets to the text: https://stackoverflow.com/a/4423805
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
if([view isKindOfClass:[UITextField class]])
{
((UITextField*)view).leftView = paddingView;
((UITextField*)view).leftViewMode = UITextFieldViewModeAlways;
}
// Create and apply a shadow layer. Help here: https://stackoverflow.com/questions/4431292/inner-shadow-effect-on-uiview-layer
CAShapeLayer* shadowLayer = [CAShapeLayer layer];
[shadowLayer setFrame:view.bounds];
[shadowLayer setShadowColor:[[UIColor blackColor] CGColor]];
[shadowLayer setShadowRadius:3.0f];
[shadowLayer setShadowOpacity:0.35f];
[shadowLayer setShadowOffset:CGSizeMake(2.0f, 2.0f)];
[shadowLayer setFillColor:[backgroundColor CGColor]];
// Causes the inner region in this example to NOT be filled.
[shadowLayer setFillRule:kCAFillRuleEvenOdd];
// Create inner and outer rectangle paths.
CGMutablePathRef path = CGPathCreateMutable();
// Outer path should be bigger than the field
UIBezierPath *bpOuter = [UIBezierPath bezierPathWithRect:CGRectInset(shadowLayer.bounds, -10, -10)];
// Inner path is the visible part of the view
UIBezierPath *bpInner = [UIBezierPath bezierPathWithRoundedRect:shadowLayer.bounds cornerRadius:CORNER_RADIUS];
// Add outer path and then add the inner path so it's subtracted from the outer path.
CGPathAddPath(path, NULL, bpOuter.CGPath);
CGPathAddPath(path, NULL, bpInner.CGPath);
CGPathCloseSubpath(path);
[shadowLayer setPath:path];
CGPathRelease(path);
[[view layer] addSublayer:shadowLayer];
NSUInteger addedAtIndex = [[[view layer] sublayers] indexOfObject:shadowLayer];
return addedAtIndex;}
为了在 TextView 滚动时处理正确的阴影+边框显示,我使用我的自定义类 -setBounds 方法来更新阴影层框架:
-(void)setBounds:(CGRect)bounds{
[super setBounds:bounds];
// Change the frame of the shadow layer to reflect new bounds
[[[[self layer] sublayers] objectAtIndex:self.shadowLayerIndex] setFrame:bounds];}
我遇到的问题是向下滚动(插入新文本行时)或顶部(向上滚动到文本开头时)底部的阴影+框架绘制不正确。
插入新行或滚动完成后( View 再次是静态的) View 绘制正确。
欢迎就此问题提出任何见解。
最佳答案
我花了一些时间来深入研究这个问题并找到了一个我想分享的解决方案。
起初我意识到最好在 -awakeFromNib
中而不是在 -setFrame
中设置阴影层。
为了在 TextView 滚动时处理正确的阴影+边框显示,我改变了这样的方法:更新阴影层我现在在我的自定义类中使用 -layoutSubviews
覆盖。在 -layoutSubviews
重写中,我重新创建阴影层以遵守新边界,然后调用 [super layoutSubviews]
。
滚动、改变方向 - 它就像一个魅力!
- (void)layoutSubviews
{
[self updateShadow];
[super layoutSubviews];
}
- (void)updateShadow
{
if (shadowLayer)
[shadowLayer removeFromSuperlayer];
shadowLayer = [LayerFormatter addInnerShadowLayerOn:self withShadowColor:[UIColor blackColor]];
}
请注意,+(NSUInteger)formatAsRoundRectWithShadowOn:(UIView*)view withBackgroundColor:(UIColor *)backgroundColor andBorderColor:(UIColor *)borderColor
现在返回对 View 中已添加图层的图层引用层次结构(返工很简单):
+(CAShapeLayer *)addInnerShadowLayerOn:(UIView *)view withShadowColor:(UIColor *)shadowColor;
任何证明此方法正确且不仅有效的证据/评论都将受到高度赞赏。
关于iphone - 在 UITextView 上滚动内部阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13653672/
我有一个程序: [textView setFont:[UIFont fontWithName:@"ArialMT" size:20]]; [textVi
我正在 UITextViewDelegate 中处理代码。那里的每个函数都会接收接收事件的 UITextView 实例,例如: - (void)textViewDidBeginEditing:(UIT
我想在默认图像附件菜单选项(复制图像、保存到相机胶卷)中添加另一个菜单选项。请注意,如果 textView 未处于编辑模式,则当您长按嵌入在 UITextView 中的图像时会显示这些选项。 我已经尝
我想尝试禁用 UITextTapRecognizer,我认为它是 UITextView 的内置属性,因为我怀疑它会干扰我想要实现的点击手势识别器。这怎么可能? 感谢阅读! 最佳答案 我不确定这是否直接
首先我使用这个 UITextView 类 TextView 类.h @interface textViewClass : UITextView TextView 类.m @implementation
我正在开发一个应用程序,我需要为大文本放置一个 UITextView。我有一个尺寸为 180X89 的背景图像(与 UITextView 框架大小相同)。当将图像放在 UITextView 后面时,一
我只是想在 UITextView 中输入一些默认文本。有几个段落,我想用一个新行分隔这些段落。但是,当我按回车时,它只是完成了数据的输入。 在界面生成器中为 UITextView 创建默认文本时,如何
我的 iPhone 应用程序中有两个 UITextView。当用户选择一个 UITextView 时, Controller 会自动转到另一个 UITextView。我在我的项目中尝试了以下方式。但是
我正在使用 UITextView 并在我的代码中分配一些文本,但出于某种原因,它总是在 5-6 行之后开始。 UITextView 是一个 AttributedTextView 或允许属性字符串。
我有一个自定义 UIView,我想在其中布局一组 UITextView 以响应 AJAX 调用。所以我想我需要在代码中创建这些。我目前将位置设置为固定位置,但宁愿将其设置为比前一个位置低 20 像素。
我正在开发一个应用程序,它具有动态添加 TextView 的功能,在我们拖动它时增加和减小它的大小。 我可以根据字符数调整 TextView 大小,方法是将 TextView 的内容大小设置为 Tex
这可能是个愚蠢的问题。我正在尝试像所附图片一样设置左/右页边距。我通过在UIScrollView中添加UITextView成功实现了。但是,我几乎可以单独使用 UITextView 实现我想要的一切。
我有一个 UITextView。我正在根据方法 textViewDidChange 中文本的大小调整 TextView 的大小。这在我打字时工作正常。但是当我粘贴一些东西时,这不会接到电话。这是预期的
我正在开发一个基于消息的 iPhone 应用程序。我有一个屏幕可以回复收到的消息。此屏幕包含两个 UITextView,如 bottomTextView 和 topTextView。 topTextV
我用 View 制作了一个 UITextview 自定义控件。它工作正常。当我输入新行时, TextView 会随着 View (深蓝色)而增加。此后 TextViell 将以滚动的形式工作。 代码是
我想在用户完成编辑并点击屏幕上的其他地方后对我的 UITextView 执行一些处理。最佳做法是什么? 我几乎设法通过 func textViewDidEndEditing(textView: UIT
我想更改富 UITextView (iOS 6) 的部分或全部属性文本,并允许用户撤消更改。 看完NSUndoManager documentation ,我尝试了第一种方法: “Simple und
当我在 IOS7 模拟器上测试我的应用程序时。 有时我在使用 sizeToFit 时发现它很奇怪的 UITextView .sizeToFit后的框架看起来是对的,但文字只能部分显示,就像下面的照片一
我想要一个 tablew View ,其行为类似于 Apple 的 iPhone 联系人应用程序:一个 uitableviewcell,里面有一个 uitextview,这样当我在 uitextvie
我有一个可编辑的 UITextView,然后是一个仅供选择的 UITextView。当我在第一个 UITextView 中写东西时,有时我想引用另一个 UITextView 来复制文本,然后将其粘贴到
我是一名优秀的程序员,十分优秀!