[英]UIButton Semi-Transparent Border
I have a custom button and the border is supposed to be semi-transparent white. 我有一个自定义按钮,边框应该是半透明的白色。
If I do this: 如果我这样做:
- (void) awakeFromNib {
self.layer.cornerRadius = 6.0f;
self.layer.borderWidth = 4.0f;
self.layer.borderColor = [[UIColor colorWithWhite:1.0f alpha:0.5f] CGColor];
}
I get this - transparency but of the original button color: 我明白了-透明度,但具有原始按钮的颜色:
The border is semi-transparent but the color of the button. 边框是半透明的,但是按钮的颜色。
Set the color of a sublayer to the color you want the button to be (don't set the background color of the button itself), and inset its rect relative to the button's rect, 将子图层的颜色设置为所需按钮的颜色(不要设置按钮本身的背景色),并相对于按钮的rect插入其rect,
- (void) awakeFromNib {
self.layer.cornerRadius = 6.0f;
self.layer.borderWidth = 4.0f;
self.layer.borderColor = [[UIColor colorWithWhite:1.0f alpha:0.5f] CGColor];
CALayer *sub = [CALayer new];
sub.frame = CGRectInset(self.bounds, 4, 4);
sub.backgroundColor = [UIColor redColor].CGColor;
[self.layer addSublayer:sub];
}
Another way to do it, which will work better if you want the background color to be rounded too, is use a background color for both the self.layer and the sublayer. 另一种方法是,如果您也希望将背景颜色也舍入,则效果更好,该方法是对self.layer和sublayer都使用背景颜色。 In this case theres on need to use a border at all.
在这种情况下,完全需要使用边框。
- (void) awakeFromNib {
self.layer.cornerRadius = 6.0f;
self.tintColor = [UIColor whiteColor]; // make white text
self.layer.backgroundColor = [[UIColor colorWithWhite:1.0f alpha:0.4] CGColor];
CALayer *sub = [CALayer new];
sub.cornerRadius = 4.0f;
sub.frame = CGRectInset(self.bounds, 4, 4);
sub.backgroundColor = [UIColor blueColor].CGColor;
[self.layer addSublayer:sub];
}
Have you tried something like the following: 您是否尝试过以下方法:
self.backgroundColor = [[UIColor colorWithWhite:1.0f alpha:0.5f] CGColor];
An easier approach is to set the color in Interface Builder by selecting the button, then choosing the alpha and background color in the Attributes Inspector: 一种更简单的方法是通过选择按钮,然后在Attributes Inspector中选择alpha和背景颜色,在Interface Builder中设置颜色:
Just in case if somebody looks for an UIImage with transparent outside border. 以防万一有人寻找带有透明外部边框的UIImage。 If you simply set a layer border, you will get a transparent border, but you will see inside image behind that border, not outside image.
如果仅设置图层边框,则将获得透明边框,但是将看到该边框后面的内部图像,而不是外部图像。 I managed to create an ImageView with transparent outside border.
我设法创建一个具有透明外部边框的ImageView。
The idea is simple. 这个想法很简单。 I save UIImage from UIImageView.
我从UIImageView保存UIImage。 Then I remove UIImage and set initial layer as border layer.
然后,我删除UIImage并将初始层设置为边界层。 Then I put a new smaller sublayer above it and set a saved UIImage as contents of it.
然后,在其上方放置一个新的较小子层,并将保存的UIImage设置为其内容。
#import <UIKit/UIKit.h>
@interface SSCircleImageView : UIImageView
@end
#import "SSCircleImageView.h"
@implementation SSCircleImageView
const CGFloat borderWidth = 5.0f;
const CGFloat borderAlpha = 0.3f;
- (void)awakeFromNib
{
UIImage *image = self.image;
self.image = nil;
self.clipsToBounds = YES;
self.layer.cornerRadius = self.frame.size.width / 2.0;
self.layer.borderWidth = borderWidth;
self.layer.borderColor = [[UIColor colorWithWhite:1.0f alpha:borderAlpha] CGColor];
CALayer *subLayer = [CALayer layer];
subLayer.frame = CGRectInset(self.bounds, self.layer.borderWidth, self.layer.borderWidth);
subLayer.cornerRadius = subLayer.frame.size.width / 2.0;
subLayer.contents = (id)image.CGImage;
[self.layer addSublayer:subLayer];
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.