ios - 在已经有 png 图像的 UIButton 中画线

标签 ios objective-c uibutton

我知道有很多关于在 View 中绘制的问题/答案,但这是一种特殊情况,因为它结合了多种功能。

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

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

这是应用线之前

enter image description here

这是应用线之后

enter image description here

这是其中一个按钮的规范

enter image description here

最佳答案

如果你想在 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 中,如下所示: enter image description here

希望能帮到你,谢谢!

关于ios - 在已经有 png 图像的 UIButton 中画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40371170/

相关文章:

iphone - UIView 动画问题,当尝试不对图像进行动画处理时

ios - iOS中解析原始类型的编码标准

objective-c - 如何移动QTMovie的光标?

ios - 苹果 watch : will the App on iPhone run in the background

ios - 如何正确重置UIView的运动效果?

ios - Xcode 可以对按钮格式差异负责吗?

swift - 我无法将图像放入 Swift 5 中的 UIButton

ios - 在 Swift 上每次按下按钮时都会增加按钮的大小

ios - 忽略以标签或提及开头的单词

objective-c - CABasicAnimation 完成后重置