我想子类化 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/
在这段Ruby代码中:ModuleMClassC当我尝试运行时出现“'M:Module'的未定义方法'helper'”错误c=M::C.new("world")c.work但直接从另一个类调用M::helper("world")工作正常。类不能调用在定义它们的同一模块中定义的模块函数吗?除了将类移出模块外,还有其他解决方法吗? 最佳答案 为了调用M::helper,你需要将它定义为defself.helper;结束为了进行比较,请查看以下修改后的代码段中的helper和helper2moduleMclassC
我想在Windows7上安装带有ruby1.9.3的rspec-railsgem。我收到一些错误消息,提示无法安装某些json库。所以,我使用下面的说明来解决它。来源=The'json'nativegemrequiresinstalledbuildtools从[rubyinstaller.org][3]下载[Ruby1.9.3][2]从[rubyinstaller.org][3]下载DevKit文件对于Ruby1.9.3,使用[DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe][4]将DevKit解压到路径C:\Ruby193\DevKit运行cd
来自Java,我正在尝试在Ruby中实现LinkedList。我在Java中实现它的通常方法是有一个名为LinkedList的类和一个名为Node的私有(private)内部类,其中LinkedList的每个对象都作为Node对象。classLinkedListprivateclassNodeattr_accessor:val,:nextendend我不想将Node类暴露给外部世界。然而,通过Ruby中的这个设置,我可以使用这个访问LinkedList类之外的私有(private)Node类对象-node=LinkedList::Node.new我知道,在Ruby1.9中,我们可以使用
我已经能够找到:a)用Ruby编写的Lisp解释器(即外部DSL)http://onestepback.org/index.cgi/Tech/Ruby/LispInRuby.redb)作为RubyDSL的Prologhttp://www.kdedevelopers.org/node/2369c)讨论Ruby“作为”一个Lisphttp://www.randomhacks.net/articles/2005/12/03/why-ruby-is-an-acceptable-lisp但奇怪的是,我实际上找不到Lisp的“内部”实现,例如Prolog的实现。我只是不够谷歌,还是还没有人发表过这
我正在使用screen,甚至在rvm1.9.2之后并且有一个合适的.rvmrc我的服务器将仍然使用jruby。我的.screenrc包含shell#{SHELL}rvm信息显示jruby。好像rvm1.9.2没有任何效果,但它在screen外工作正常。 最佳答案 好的,我找到了solution.创建一个.screenrc文件并将此代码段放入其中:shell-$SHELLofficialinstructions省略破折号。 关于ruby-RVMscreen内部损坏,我们在StackOver
我正在将我维护的rubygem从RDoc切换到YARD文档。但是,代码中有一些关键注释只需要保留在代码中,不应出现在文档中。例如:###SomeClassdocumentationhere.#--#CRITICALcommentthatshouldbeinthecodebutnotinthedocumentation,#andmustbeatthisparticularspotinthecode.#++#moredocumentationthatfollowsthecriticalcommentblock,butthispart#shouldbeinthegenerateddocu
这个问题在这里已经有了答案:WhydoRubysettersneed"self."qualificationwithintheclass?(3个答案)关闭7年前。我一直在研究PragmaticProgrammers的“ProgrammingRuby”一书,想知道是否可以在类中调用setter方法,而不是直接分配给实例变量。classBookInStockattr_reader:isbn,:pricedefinitialize(isbn,price)@isbn=isbn@price=Float(price)enddefprice_in_centsInteger(price*100+0.5
我想知道:在Ruby中,有没有一种方法可以在不使用其名称的情况下从自身内部调用方法?如果该方法是通过某些元编程技术创建的,那么通过其名称调用它可能会难以阅读。即使对于通常定义的方法,如果您不确定它的好名字,或者如果它的名字很长,通过一些关键字(类似于super)从自身内部调用它可能会很方便。 最佳答案 您可以使用Kernel#__method__以Symbol形式返回当前方法的名称。与super不同,它不是关键字而是常规方法,因此您必须将它连同必需的参数一起传递给send方法才能调用该方法。这是__method__返回的内容:obj
在一个模块中,我有一个名为Process的类。moduleMProcess=Class.newProcess::wait(0)end这会引发NoMethodError。如何从模块内部访问顶级Process?如果不重命名我的类(class),这完全有可能吗? 最佳答案 ::Process.wait(0) 关于ruby-如何从定义相同名称的模块内部访问ruby中的顶级实体,我们在StackOverflow上找到一个类似的问题: https://stackoverf
我有一个代码需要在eval中使用。有时我需要退出评估代码,但我的尝试会导致错误。例如:#expectedtosee1,2and5;not3nor4;andnoerrorseval"puts1;puts2;return;puts3;puts4"#=>Error:unexpectedreturnputs5我尝试了return、end、exit、break,但都没有成功。exit不会引发错误,但我没有得到5。(注意:我知道eval是邪恶的,但在这种情况下我需要使用它。) 最佳答案 谢谢大家,但我找到了最适合我的问题的解决方案:lambda