Sto disegnando foto avatar, applicando solo cornerRadius
a un livello UIImageView
e aggiungendo un bordo tramite borderWith
e borderColor
. In questo modo:iOS: rettangolo arrotondato con bordo bleed colore
self.layer.masksToBounds = YES;
self.layer.cornerRadius = imageDimension/2.f;
self.layer.borderWidth = 1.f;
self.layer.borderColor = borderColor.CGColor;
che funziona grande, ad eccezione di questo piccolo, ma evidente emorragia di contenuti al di fuori del confine, in questo modo:
C'è un modo per appena fin frontiera da pochi punti 1/10, o inserire il contenuto più del confine?
Soluzione
Grazie a FelixLam, mi si avvicinò con una bella soluzione e lasceranno qui per l'aldilà:
@interface MMRoundImageViewWithBorder : UIView
- (id)initWithImage:(UIImage *)image borderWidth:(CGFloat)borderWidth;
@property (strong, nonatomic) UIImageView *imageView;
@property (assign, nonatomic) CGFloat borderWidth;
@property (strong, nonatomic) UIColor *borderColor;
@end
@implementation MMRoundImageViewWithBorder
- (id)initWithImage:(UIImage *)image borderWidth:(CGFloat)borderWidth {
if (self = [super init]) {
self.borderWidth = borderWidth;
self.borderColor = UIColor.whiteColor;
self.imageView = [[UIImageView alloc] initWithImage:image];
[self addSubview:self.imageView];
self.imageView.layer.masksToBounds = YES;
self.layer.masksToBounds = YES;
}
return self;
}
- (void)setBorderColor:(UIColor *)borderColor {
_borderColor = borderColor;
self.backgroundColor = borderColor;
}
- (void)layoutSubviews {
[super layoutSubviews];
[self refreshDimensions];
}
- (void)refreshDimensions {
self.layer.cornerRadius = CGRectGetWidth(self.bounds)/2.f;
self.imageView.frame = CGRectInset(self.bounds, _borderWidth, _borderWidth);
self.imageView.layer.cornerRadius = CGRectGetWidth(self.imageView.bounds)/2.f;
}
- (void)setBorderWidth:(CGFloat)borderWidth {
_borderWidth = borderWidth;
[self refreshDimensions];
}
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
[self refreshDimensions];
}
@end
Si può cercare di aggiungere un trasparente bordo di 1 pixel per il file di immagine, quindi l'immagine sarà di 2 pixel più larga e più alta, questo dovrebbe aiutare con la fusione – dariaa