你好。我需要在自定义按钮上写一些文本,所以我子类化了UIButton。在-drawrect中,我编写了所需的文本。问题是文本最终会出现在按钮下方。在我编写文本后,UIButton的drawrect会在我的上方绘制。
即使我之前打算在进行绘制之前调用[super drawrect:rect],但我甚至没有调用它。似乎无论如何都会调用UIButton的drawrect。
有什么想法吗?谢谢。
如其他人所说,你无需子类化UIButton - 事实上最好不要这样做,因为UIButton很复杂(正如你发现的那样)。你有三个选择:
addTarget:action:forControlEvents:
)。UIButton在其上实现了状态性。如果你不需要像UIButton的setTitle:forState:
和相关功能之类的东西,但你正在使用很多这些控件,我会使用第三个选项。否则使用第一个选项(如果你在3.0上使用第二个选项)。
这对我来说很有效(忽略复杂的绘图代码)。
这是我的UIButton代码:
h文件:
#import <UIKit/UIKit.h>
@interface DoneButton : UIButton
@property (assign, nonatomic) BOOL selected;
@end
m文件:
#import "DoneButton.h"
@implementation DoneButton
@synthesize selected = _selected;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected
{
if (_selected != selected) {
_selected = selected;
[self setNeedsDisplay];
}
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (_selected) {
//// Color Declarations
UIColor* color2 = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 1];
//// Bezier Drawing
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(49.39, 31.25)];
[bezierPath addLineToPoint: CGPointMake(49.39, 31.25)];
[bezierPath addCurveToPoint: CGPointMake(46.64, 25.19) controlPoint1: CGPointMake(49.39, 29.02) controlPoint2: CGPointMake(48.38, 26.94)];
[bezierPath addCurveToPoint: CGPointMake(44.3, 18.95) controlPoint1: CGPointMake(46.63, 22.72) controlPoint2: CGPointMake(45.88, 20.53)];
[bezierPath addCurveToPoint: CGPointMake(42.44, 17.59) controlPoint1: CGPointMake(43.75, 18.4) controlPoint2: CGPointMake(43.13, 17.95)];
[bezierPath addCurveToPoint: CGPointMake(38.07, 16.61) controlPoint1: CGPointMake(41.17, 16.93) controlPoint2: CGPointMake(39.67, 16.62)];
[bezierPath addCurveToPoint: CGPointMake(33.15, 13.95) controlPoint1: CGPointMake(36.62, 15.18) controlPoint2: CGPointMake(34.95, 14.23)];
[bezierPath addCurveToPoint: CGPointMake(32.78, 13.92) controlPoint1: CGPointMake(33.03, 13.93) controlPoint2: CGPointMake(32.9, 13.93)];
[bezierPath addCurveToPoint: CGPointMake(32, 13.86) controlPoint1: CGPointMake(32.52, 13.89) controlPoint2: CGPointMake(32.26, 13.86)];
[bezierPath addLineToPoint: CGPointMake(32, 13.86)];
[bezierPath addLineToPoint: CGPointMake(32, 13.86)];
[bezierPath addLineToPoint: CGPointMake(32, 13.86)];
[bezierPath addLineToPoint: CGPointMake(32, 13.86)];
[bezierPath addCurveToPoint: CGPointMake(31.38, 13.91) controlPoint1: CGPointMake(31.79, 13.86) controlPoint2: CGPointMake(31.59, 13.89)];
[bezierPath addCurveToPoint: CGPointMake(30.85, 13.95) controlPoint1: CGPointMake(31.2, 13.92) controlPoint2: CGPointMake(31.02, 13.92)];
[bezierPath addCurveToPoint: CGPointMake(26.92, 15.74) controlPoint1: CGPointMake(29.44, 14.17) controlPoint2: CGPointMake(28.12, 14.8)];
[bezierPath addCurveToPoint: CGPointMake(25.94, 16.61) controlPoint1: CGPointMake(26.58, 16.01) controlPoint2: CGPointMake(26.25, 16.29)];
[bezierPath addCurveToPoint: CGPointMake(24.13, 16.76) controlPoint1: CGPointMake(25.31, 16.61) controlPoint2: CGPointMake(24.71, 16.66)];
[bezierPath addCurveToPoint: CGPointMake(19.7, 18.95) controlPoint1: CGPointMake(22.41, 17.06) controlPoint2: CGPointMake(20.88, 17.77)];
[bezierPath addCurveToPoint: CGPointMake(17.36, 25.19) controlPoint1: CGPointMake(18.12, 20.53) controlPoint2: CGPointMake(17.37, 22.72)];
[bezierPath addCurveToPoint: CGPointMake(14.61, 31.25) controlPoint1: CGPointMake(15.62, 26.94) controlPoint2: CGPointMake(14.61, 29.02)];
[bezierPath addLineToPoint: CGPointMake(14.61, 31.25)];
[bezierPath addCurveToPoint: CGPointMake(14.61, 31.25) controlPoint1: CGPointMake(14.61, 31.25) controlPoint2: CGPointMake(14.61, 31.25)];
[bezierPath addCurveToPoint: CGPointMake(14.66, 31.92) controlPoint1: CGPointMake(14.61, 31.48) controlPoint2: CGPointMake(14.64, 31.7)];
[bezierPath addCurveToPoint: CGPointMake(14.7, 32.4) controlPoint1: CGPointMake(14.67, 32.08) controlPoint2: CGPointMake(14.67, 32.24)];
[bezierPath addCurveToPoint: CGPointMake(15.86, 35.44) controlPoint1: CGPointMake(14.86, 33.46) controlPoint2: CGPointMake(15.27, 34.48)];
[bezierPath addCurveToPoint: CGPointMake(15.97, 35.64) controlPoint1: CGPointMake(15.9, 35.5) controlPoint2: CGPointMake(15.93, 35.57)];
[bezierPath addCurveToPoint: CGPointMake(17.36, 37.32) controlPoint1: CGPointMake(16.36, 36.23) controlPoint2: CGPointMake(16.83, 36.78)];
[bezierPath addCurveToPoint: CGPointMake(17.49, 38.98) controlPoint1: CGPointMake(17.36, 37.89) controlPoint2: CGPointMake(17.41, 38.44)];
[bezierPath addCurveToPoint: CGPointMake(18.35, 41.7) controlPoint1: CGPointMake(17.65, 39.95) controlPoint2: CGPointMake(17.92, 40.87)];
[bezierPath addCurveToPoint: CGPointMake(19.7, 43.55) controlPoint1: CGPointMake(18.7, 42.38) controlPoint2: CGPointMake(19.15, 43)];
[bezierPath addCurveToPoint: CGPointMake(25.93, 45.89) controlPoint1: CGPointMake(21.28, 45.13) controlPoint2: CGPointMake(23.47, 45.88)];
[bezierPath addCurveToPoint: CGPointMake(27.61, 47.28) controlPoint1: CGPointMake(26.47, 46.42) controlPoint2: CGPointMake(27.02, 46.89)];
[bezierPath addCurveToPoint: CGPointMake(32, 48.64) controlPoint1: CGPointMake(28.96, 48.16) controlPoint2: CGPointMake(30.44, 48.64)];
[bezierPath addLineToPoint: CGPointMake(32, 48.64)];
[bezierPath addLineToPoint: CGPointMake(32, 48.64)];
[bezierPath addLineToPoint: CGPointMake(32, 48.64)];
[bezierPath addLineToPoint: CGPointMake(32, 48.64)];
[bezierPath addCurveToPoint: CGPointMake(38.06, 45.89) controlPoint1: CGPointMake(34.23, 48.64) controlPoint2: CGPointMake(36.31, 47.63)];
[bezierPath addCurveToPoint: CGPointMake(44.3, 43.55) controlPoint1: CGPointMake(40.53, 45.88) controlPoint2: CGPointMake(42.72, 45.13)];
[bezierPath addLineToPoint: CGPointMake(44.3, 43.55)];
[bezierPath addLineToPoint: CGPointMake(44.3, 43.55)];
[bezierPath addCurveToPoint: CGPointMake(46.64, 37.31) controlPoint1: CGPointMake(45.88, 41.97) controlPoint2: CGPointMake(46.63, 39.78)];
[bezierPath addCurveToPoint: CGPointMake(49.39, 31.25) controlPoint1: CGPointMake(48.38, 35.56) controlPoint2: CGPointMake(49.39, 33.48)];
[bezierPath addLineToPoint: CGPointMake(49.39, 31.25)];
[bezierPath closePath];
[color2 setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];
//// Bezier 2 Drawing
UIBezierPath* bezier2Path = [UIBezierPath bezierPath];
[bezier2Path moveToPoint: CGPointMake(22.42, 29.85)];
[bezier2Path addLineToPoint: CGPointMake(29.74, 37.17)];
[bezier2Path addLineToPoint: CGPointMake(41.58, 25.33)];
bezier2Path.usesEvenOddFillRule = YES;
[color2 setStroke];
bezier2Path.lineWidth = 1;
[bezier2Path stroke];
} else {
//// Color Declarations
UIColor* color3 = [UIColor colorWithRed: 0.5 green: 0.5 blue: 0.5 alpha: 1];
//// Bezier 2 Drawing
UIBezierPath* bezier2Path = [UIBezierPath bezierPath];
[bezier2Path moveToPoint: CGPointMake(22.42, 29.85)];
[bezier2Path addLineToPoint: CGPointMake(29.74, 37.17)];
[bezier2Path addLineToPoint: CGPointMake(41.58, 25.33)];
bezier2Path.usesEvenOddFillRule = YES;
[color3 setStroke];
bezier2Path.lineWidth = 1;
[bezier2Path stroke];
}
}
如果按钮被选择,绘制第一段代码;否则,绘制另一段代码。我更喜欢使用Paint Code这种方法,因为可以节省大量时间,并且不需要创建数千个PNG或JPEG文件。
如果您想要一个按钮在图像上方绘制文本而不是旁边,请将图像设置为按钮的背景图像。请使用setBackgroundImage:forState:
而不是setImage:forState:
,然后正常设置标题。
无论如何,您都必须在drawRect:
中绘制图像,或将文本移动到子视图中以便在图像上方绘制。我提到的解决方案是第二个选项的版本。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 44, 44);
[button setTitle:@"x" forState:UIControlStateNormal];
[self.view addSubview:button];
您还可以设置按钮的字体属性或使用-setTitleColor:forState:更改颜色。