[英]Draw line in UIButton that already has png image in it
如果你想在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
我希望能幫助你,謝謝!
一個簡單的解決方案,如果您不確定如何繪制線條,通常是創建一個UILabel,將框架設置為:
UILabel *line = [[UILabel alloc] initWithFrame(x,y,w,h)];
然后,設置標簽的背景顏色
line.backgroundColor = [UIColor red];
然后,將其添加到按鈕
[button addSubview:line];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.