简体   繁体   English

在UITextField文本上删除阴影

[英]Drop Shadow on UITextField text

是否可以在UITextField为文本添加阴影?

As of 3.2, you can use the CALayer shadow properties. 从3.2开始,您可以使用CALayer阴影属性。

_textField.layer.shadowOpacity = 1.0;   
_textField.layer.shadowRadius = 0.0;
_textField.layer.shadowColor = [UIColor blackColor].CGColor;
_textField.layer.shadowOffset = CGSizeMake(0.0, -1.0);

I have a slightly different problem - I want a blurred shadow on a UILabel. 我有一个稍微不同的问题 - 我想在UILabel上模糊阴影。 Luckily, the solution to this turned out to be number (2) from Tyler 幸运的是,对此的解决方案原来是泰勒的数字(2)

Here's my code : 这是我的代码:

- (void) drawTextInRect:(CGRect)rect {
    CGSize myShadowOffset = CGSizeMake(4, -4);
    CGFloat myColorValues[] = {0, 0, 0, .8};

    CGContextRef myContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(myContext);

    CGColorSpaceRef myColorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef myColor = CGColorCreate(myColorSpace, myColorValues);
    CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);

    [super drawTextInRect:rect];

    CGColorRelease(myColor);
    CGColorSpaceRelease(myColorSpace); 

    CGContextRestoreGState(myContext);
}

This is in a class that extends from UILabel and draws the text with a shadow down and to the right 4px, the shadow is grey at 80% opacity and is sightly blurred. 这是一个从UILabel延伸的类,并向下绘制带有阴影的文本到右边的4px,阴影是80%不透明度的灰色,并且模糊不清。

I think that Tyler's solution number 2 is a little better for performance than Tyler's number 1 - you're only dealing with one UILabel in the view and, assuming that you're not redrawing every frame, it's not a hit in rendering performance over a normal UILabel. 我认为Tyler的2号解决方案在性能上要比Tyler的1号好一点 - 你只是在视图中处理一个UILabel而且假设你没有重新绘制每一帧,那么渲染性能并不比正常的UILabel。

PS This code borrowed heavily from the Quartz 2D documentation PS此代码大量借鉴了Quartz 2D文档

I don't think you get built-in support for text shadows here, the way you do with UILabel . 我不认为你在这里得到了对文本阴影的内置支持,就像你使用UILabel

Two ideas: 两个想法:

(1) [Moderately tricky to code.] Add a second UITextField behind the original, at a very small offset (maybe by (0.2,0.8)? ). (1) [中等难以编码。]在原始的后面添加第二个UITextField ,偏移很小(可能是(0.2,0.8)?)。 You can listen to every text change key-by-key by implementing the textField:shouldChangeCharactersInRange:replacementString: method in the UITextFieldDelegate protocol. 通过在UITextFieldDelegate协议中实现textField:shouldChangeCharactersInRange:replacementString:方法,您可以逐个键地监听每个文本更改。 Using that, you can update the lower text simultaneously. 使用它,您可以同时更新下部文本。 You could also make the lower text (the shadow text) gray, and even slightly blurry using the fact that fractionally-offset text rects appear blurry. 您还可以将较低的文本(阴影文本)设置为灰色,甚至使用分数偏移文本显示模糊的事实稍微模糊。 Added: Oh yea, don't forget to set the top text field's background color to [UIColor clearColor] if you go with this idea. 补充:哦,是的,如果你采用这个想法,不要忘记将顶部文本字段的背景颜色设置为[UIColor clearColor]

(2) [Even more fun to code.] Subclass UITextField and override the drawRect: method. (2) [代码更有趣。]子类UITextField并覆盖drawRect:方法。 I haven't done this before, so I'll mention up front that this depends on this being the designated drawing method, and it may turn out that you have to override another drawing function, such as drawTextInRect: , which is specific to UITextField . 我之前没有这样做过,所以我前面会提到这取决于这是指定的绘图方法,并且可能会发现你必须覆盖另一个绘图函数,例如drawTextInRect: ,这是UITextField特有的。 Now set up the drawing context to draw shadows via the CGContextSetShadow functions , and call [super drawRect:rect]; 现在设置绘图上下文以通过CGContextSetShadow函数绘制阴影,并调用[super drawRect:rect]; . Hopefully that works -- in case the original UITextField code clears the drawing context's shadow parameters, that idea is hosed, and you'll have to write the whole drawing code yourself, which I anti-recommend because of all the extras that come with UITextFields like copy-and-paste and kanji input in Japanese. 希望这有效 - 如果原始的UITextField代码清除了绘图上下文的阴影参数,那么这个想法就会被清除,你必须自己编写整个绘图代码,我反对推荐,因为UITextFields附带了所有附加功能。像日语中的复制粘贴和汉字输入一样。

Although the method of applying the shadow directly to the UITextView will work, it's the wrong way to do this. 虽然将阴影直接应用于UITextView的方法可行,但这样做是错误的。 By adding the shadow directly with a clear background color, all subviews will get the shadow, even the cursor. 通过使用清晰的背景颜色直接添加阴影,所有子视图都将获得阴影,甚至是光标。

The approach that should be used is with NSAttributedString . 应该使用的方法是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;

However textView.attributedText is for iOS6. 但是textView.attributedText适用于iOS6。 If you must support lower versions, you could use the following approach. 如果必须支持较低版本,则可以使用以下方法。 (Dont forget to add #import <QuartzCore/QuartzCore.h> ) (别忘了添加#import <QuartzCore/QuartzCore.h>

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM