簡體   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