Skip to content

Commit

Permalink
优化圆角的逻辑,支持设置百分比,以及最大不超过50%
Browse files Browse the repository at this point in the history
  • Loading branch information
jingcheng1988 committed Jan 2, 2024
1 parent afee717 commit 1756551
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
6 changes: 4 additions & 2 deletions GaiaXiOS/GaiaXiOS/Component/Node/GXBaseNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) CGFloat opacity;
// 边框宽度, 默认0
@property (nonatomic) CGFloat borderWidth;
// 圆角, 默认0
@property (nonatomic) CGFloat cornerRadius;
// 圆角, 默认0,最大不超过50%
@property (nonatomic) CGFloat cornerRadius;
// 百分比圆角,最大不超过50%
@property (nonatomic) CGFloat percentCornerRadius;
// 超出视图截断,默认为YES
@property (nonatomic, assign) BOOL clipsToBounds;
// 边框颜色, 默认nil
Expand Down
26 changes: 23 additions & 3 deletions GaiaXiOS/GaiaXiOS/Component/Node/GXBaseNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ - (BOOL)updateLayoutStyle:(NSDictionary *)styleInfo{
self.bottomRightRadius = [GXStyleHelper converSimpletValue:bottomRight];
}
self.cornerRadius = 0.f;
self.percentCornerRadius = 0.f;

//更新标记
if (!isMark) {
Expand All @@ -404,7 +405,7 @@ - (BOOL)updateLayoutStyle:(NSDictionary *)styleInfo{

} else if (cornerRadius) {
//设置全圆角属性
self.cornerRadius = [GXStyleHelper converSimpletValue:cornerRadius];
[self handleBorderRadius:cornerRadius];
self.topLeftRadius = 0.f;
self.topRightRadius = 0.f;
self.bottomLeftRadius = 0.f;
Expand Down Expand Up @@ -536,7 +537,14 @@ - (void)setupCornerRadius:(UIView *)view{
//设置圆角 & 表框
view.layer.borderColor = self.borderColor.CGColor;
view.layer.borderWidth = self.borderWidth;
view.layer.cornerRadius = self.cornerRadius;

CGFloat radius = 0.0;
if (self.percentCornerRadius > 0) {
radius = view.frame.size.height * self.percentCornerRadius;
} else {
radius = MIN(view.frame.size.height / 2.0, self.cornerRadius);
}
view.layer.cornerRadius = radius;
}

}
Expand Down Expand Up @@ -727,9 +735,10 @@ - (void)configureStyleInfo:(NSDictionary *)styleJson{
NSString *borderRadius = [styleJson gx_stringForKey:@"border-radius"];
if (borderRadius.length) {
isNeedFlat = NO;
self.cornerRadius = [GXStyleHelper converSimpletValue:borderRadius];
[self handleBorderRadius:borderRadius];
} else {
self.cornerRadius = 0.f;
self.percentCornerRadius = 0.f;
}
}

Expand All @@ -742,6 +751,17 @@ - (void)configureStyleInfo:(NSDictionary *)styleJson{

}

// 处理全圆角的逻辑
- (void)handleBorderRadius:(NSString *)borderRadius {
if ([borderRadius hasSuffix:@"%"]) {
CGFloat value = [[borderRadius substringToIndex:(borderRadius.length - 1)] floatValue] / 100.0f;
self.percentCornerRadius = MAX(MIN(0.5, value), 0);
self.cornerRadius = 0.0;
} else {
self.cornerRadius = [GXStyleHelper converSimpletValue:borderRadius];
self.percentCornerRadius = 0.0;
}
}

#pragma mark - 属性设置

Expand Down

0 comments on commit 1756551

Please sign in to comment.