如何在UIButton子类中重写-drawrect?

时间:2009-05-03 00:51:50

标签: iphone uibutton

HI。我需要在自定义按钮上写一些文字。所以我将UIButton子类化了。在笔直,我写了我需要的文字。问题是文本最终在按钮下面。在我写完我的文字之后,UIButton的画面继续前进并吸引我。

我甚至没有打电话给[super drawrect:rect],即使我在做绘图之前预计会调用它。似乎无论如何都会调用UIButton的drawrect。

有什么想法吗?感谢。

5 个答案:

答案 0 :(得分:20)

正如其他人所说,你不需要子类化UIButton - 事实上最好不要因为UIButton非常复杂(正如你所发现的那样)。您有三种选择:

  • 将按钮标题留空,并在所需位置的按钮上添加新文本字段。
  • 如果您使用3.0,请访问按钮的标签属性。虽然标签本身是只读的,但它的属性(包括框架)不是。
  • 子类UIControl。 UIControl是一个UIView,因此您可以添加其他视图 - 文本和图像,它是实现动作消息行为(addTarget:action:forControlEvents:)的UIView。 UIButton在此基础上实现了有状态。

如果你不需要像UIButton的setTitle:forState:和相关功能这样的东西,但你使用了很多这些控件,我会使用第三个选项。否则使用第一个(如果你在3.0,则使用第二个)。

答案 1 :(得分:4)

这对我来说效果很好......(忽略复杂的绘图代码)。

  1. 创建UIButton的子类
  2. 在StoryBoard中创建UIButton
  3. 转到Identity Inspector并输入自定义的名称 按钮类
  4. 这里是我的UIButton代码:

    h file:

    #import <UIKit/UIKit.h>
    
    @interface DoneButton : UIButton
    
    @property (assign, nonatomic) BOOL selected;
    
    @end
    

    m file:

     #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。

答案 2 :(得分:3)

基于您对其他答案之一的评论,这是一个询问“我如何用Y来实现Z?”的情况。当你真的应该问“我如何实现Z?”

如果您想要一个按钮在图像顶部而不是旁边绘制文本,只需将图像设为按钮的背景图像即可。使用setBackgroundImage:forState:,而不是setImage:forState:,然后正常设置标题。

无论如何,您要么必须在drawRect:中绘制图像,要么将文本移动到子视图中,以便在图像上方绘制。我提到的解决方案是第二个选项的版本。

答案 3 :(得分:2)

您不需要将UIButton子类化为向其添加一些文本。这是在UIViewController子类的-viewDidLoad方法中设置自定义按钮的按钮标题的示例:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 44, 44);
[button setTitle:@"x" forState:UIControlStateNormal];
[self.view addSubview:button];

您还可以使用-setTitleColor:forState:设置按钮的font属性或更改颜色。

答案 4 :(得分:0)

如何在按钮视图上添加子视图?然后你可以完全控制它而不关心按钮做了什么。您甚至可以将其设为Label并自行保存drawRect。