繁体   English   中英

在UIButton中绘制已经包含png图像的行

[英]Draw line in UIButton that already has png image in it

我知道有很多关于绘制视图的问题/答案,但这是一种特殊情况,因为它结合了几个特征。

  1. 我正在使用UIButton
  2. 它有一种自定义类型
  3. 它有一个png图像

所以我需要保留所有这些,但另外,我需要沿底边绘制一条线(可能插入几个点),如下图所示。

这是在应用该行之前

在此输入图像描述

这是在应用线之后

在此输入图像描述

以下是其中一个按钮的规格

在此输入图像描述

如果你想在xib上绘制它,你必须自定义你的按钮。

@interface DrawLineButton()
   @property (nonatomic, strong) IBInspectable UIColor* fillColor;
   @property (nonatomic, assign) IBInspectable CGFloat  toLeft;
   @property (nonatomic, assign) IBInspectable CGFloat  toRight;
   @property (nonatomic, assign) IBInspectable CGFloat  toBottom;
   @property (nonatomic, assign) IBInspectable CGFloat  lineHeight;
@end
@implementation DrawLineButton
   // Only override drawRect: if you perform custom drawing.
   // An empty implementation adversely affects performance during animation.
   - (void)drawRect:(CGRect)rect {
   [super drawRect:rect];
    CGRect pathRect = CGRectMake(_toLeft,
                         self.bounds.size.height - _toBottom - _lineHeight,
                         self.bounds.size.width - _toLeft - _toRight,
                         _lineHeight);
    UIBezierPath* path = [UIBezierPath bezierPathWithRect:pathRect];
    path.lineWidth = _lineHeight;
    UIColor* fillColor = _fillColor;
    [fillColor set];
    [path fill];
    [path stroke];
    }
@end
    //.h
IB_DESIGNABLE //Must this
@interface DrawLineButton : UIButton
@end

然后将自定义按钮添加到xib,如下所示: 在此输入图像描述

我希望能帮助你,谢谢!

一个简单的解决方案,如果您不确定如何绘制线条,通常是创建一个UILabel,将框架设置为:

UILabel *line = [[UILabel alloc] initWithFrame(x,y,w,h)];

然后,设置标签的背景颜色

line.backgroundColor = [UIColor red];

然后,将其添加到按钮

[button addSubview:line];

这很难说,因为我不确定你为什么要实现这个目标,但可能的解决办法是:

  1. 创建新的UIView

  2. 将对齐边设置为等于按钮(从而使其直接位于按钮顶部)

在此输入图像描述

  1. 将视图背景颜色设置为透明

  2. 在新创建的UIView集上:

isUserInteractionEnabled = false

现在你可以在那个视图中绘制你想要的任何内容,正如你所提到的那样,在许多其他地方都有详细介绍

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM