objective-c 在 UITextField 文本上放置阴影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1274168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:04:17  来源:igfitidea点击:

Drop Shadow on UITextField text

iphoneobjective-cuikituitextfieldshadow

提问by DotSlashSlash

Is it possible to add a shadow to the text in a UITextField?

是否可以为 a 中的文本添加阴影UITextField

回答by egarc

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);

回答by deanWombourne

I have a slightly different problem - I want a blurred shadow on a UILabel. Luckily, the solution to this turned out to be number (2) from Tyler

我有一个稍微不同的问题 - 我想要一个 UILabel 上的模糊阴影。幸运的是,这个问题的解决方案是来自 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 文档

回答by Tyler

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 UITextFieldbehind the original, at a very small offset (maybe by (0.2,0.8)? ). You can listen to every text change key-by-key by implementing the textField:shouldChangeCharactersInRange:replacementString:method in the UITextFieldDelegateprotocol. 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.

(1) [编码UITextField有点棘手。]在原始值后面加一秒,偏移量很小(可能是 (0.2,0.8)? )。您可以通过实现协议中的textField:shouldChangeCharactersInRange:replacementString:方法来逐键收听每个文本更改UITextFieldDelegate。使用它,您可以同时更新下部文本。您还可以使用部分偏移文本矩形看起来模糊的事实使下部文本(阴影文本)变灰,甚至略微模糊。补充:哦,是的,[UIColor clearColor]如果您同意这个想法,请不要忘记将顶部文本字段的背景颜色设置为。

(2) [Even more fun to code.]Subclass UITextFieldand override the drawRect:method. 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. Now set up the drawing context to draw shadows via the CGContextSetShadowfunctions, and call [super drawRect:rect];. Hopefully that works -- in case the original UITextFieldcode 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 UITextFieldslike copy-and-paste and kanji input in Japanese.

(2) [编写代码更有趣。]子类化UITextField并覆盖drawRect:方法。我以前没有这样做过,所以我会在前面提到这取决于这是指定的绘图方法,并且可能结果是您必须覆盖另一个绘图函数,例如drawTextInRect:特定于UITextField. 现在设置了绘图环境通过绘制阴影CGContextSetShadow功能和呼叫[super drawRect:rect];。希望能奏效——如果原始UITextField代码清除了绘图上下文的阴影参数,那么这个想法就被扼杀了,你必须自己编写整个绘图代码,我不推荐这样做,因为UITextFields像复制一样附带所有附加功能-and-paste 和 kanji 输入日语。

回答by cnotethegr8

Although the method of applying the shadow directly to the UITextViewwill work, it's the wrong way to do this. By adding the shadow directly with a clear background color, all subviews will get the shadow, even the cursor.

尽管将阴影直接应用于UITextView将起作用的方法,但这是错误的方法。直接用清晰的背景色添加阴影,所有子视图都会得到阴影,甚至光标。

The approach that should be used is with NSAttributedString.

应该使用的方法是 with 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. If you must support lower versions, you could use the following approach. (Dont forget to add #import <QuartzCore/QuartzCore.h>)

但是 textView.attributedText 适用于 iOS6。如果您必须支持较低版本,则可以使用以下方法。(不要忘记添加#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;