iOS 指定控件使用圆角属性

22 篇文章 0 订阅

以Button为例
创建Button就不写了

设置圆角通常用layer.cornerRadius,也就是像下面这样
button.layer.masksToBounds = YES;
button.layer.cornerRadius = 4.;

但是UI也经常变化的,例如要设置成某个角为圆角,layer.cornerRadius就摸瞎了,毕竟这个属性是通用全角的。
这里就需要使用UIBezierPath去设置,具体如下:

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 << 0,
    UIRectCornerTopRight    = 1 << 1,
    UIRectCornerBottomLeft  = 1 << 2,
    UIRectCornerBottomRight = 1 << 3,
    UIRectCornerAllCorners  = ~0UL
};

UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(4, 4)];
            CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
            shapeLayer.frame = button.bounds;
            shapeLayer.path = bezierPath.CGPath;
            button.layer.mask = shapeLayer;

参数:UIRectCorner:意思就是表面的意思
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 .NET MAUI 中,您可以创建一个自定义控件来实现带圆角的 Label。以下是一个简单的示例: 1. 在您的 MAUI 项目中创建一个名为 "RoundedLabel" 的新控件类,并从 Label 类继承。 2. 在 RoundedLabel 类中添加名为 "CornerRadius" 的新绑定属性,以便您可以在 XAML 中设置圆角半径。 ```csharp public class RoundedLabel : Label { public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(int), typeof(RoundedLabel), defaultValue: 0); public int CornerRadius { get => (int)GetValue(CornerRadiusProperty); set => SetValue(CornerRadiusProperty, value); } } ``` 3. 创建一个自定义渲染器,将 Label 控件渲染为具有圆角控件。 针对 Android 平台: ```csharp [assembly: ExportRenderer(typeof(RoundedLabel), typeof(RoundedLabelRenderer))] namespace YourNamespace { public class RoundedLabelRenderer : LabelRenderer { public RoundedLabelRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Label> e) { base.OnElementChanged(e); if (Control != null && e.NewElement != null) { var roundedLabel = (RoundedLabel)e.NewElement; var cornerRadius = Context.ToPixels(roundedLabel.CornerRadius); Control.SetBackground(GetRoundRectDrawable(cornerRadius, roundedLabel.TextColor.ToAndroid())); } } private GradientDrawable GetRoundRectDrawable(float radius, Android.Graphics.Color color) { var shape = new GradientDrawable(); shape.SetShape(ShapeType.Rectangle); shape.SetCornerRadii(new float[] { radius, radius, radius, radius, radius, radius, radius, radius }); shape.SetColor(color); return shape; } } } ``` 针对 iOS 平台: ```csharp [assembly: ExportRenderer(typeof(RoundedLabel), typeof(RoundedLabelRenderer))] namespace YourNamespace { public class RoundedLabelRenderer : LabelRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Label> e) { base.OnElementChanged(e); if (Control != null && e.NewElement != null) { var roundedLabel = (RoundedLabel)e.NewElement; var cornerRadius = roundedLabel.CornerRadius; Control.Layer.CornerRadius = cornerRadius; Control.Layer.MasksToBounds = true; } } } } ``` 4. 在 XAML 中使用自定义 RoundedLabel 控件,并设置 CornerRadius 属性来添加圆角。 ```xml <local:RoundedLabel CornerRadius="10" Text="Hello, world!" /> ``` 请注意,上述示例仅适用于单个平台。您需要为每个平台创建一个自定义渲染器,以便在所有平台上正确地显示您的自定义 RoundedLabel 控件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值