自定义控件-中间画线的view

来源:互联网 发布:开源淘宝客系统 编辑:程序博客网 时间:2024/06/06 05:53

直接看效果图:


代码:

#import <UIKit/UIKit.h>

@interface JJCrossedView :UIView


- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title font:(UIFont *)font lineColor:(UIColor *)lineColor;


@end


#import "JJCrossedView.h"


@interface JJCrossedView()


@property(nonatomic,copy)NSString *title;

@property(nonatomic,retain)UIFont *font;

@property(nonatomic,retain)UIColor *lineColor;


@end


@implementation JJCrossedView


- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title font:(UIFont *)font lineColor:(UIColor *)lineColor

{

   self = [superinitWithFrame:frame];

   if (self) {

       self.title = title;

       self.font = font;

       self.lineColor = lineColor;

    }

    return self;

}

- (void)drawRect:(CGRect)rect {

    

    //[super drawRect:rect] // 可调用父类方法,显示文本

    CGContextRef  ref =UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(ref,self.lineColor.CGColor);

    //[self.titleColor setStroke];

    CGContextSetLineWidth(ref,1.0);

    CGContextMoveToPoint(ref,0.0,self.frame.size.height/2.0-2.0);

    CGContextAddLineToPoint(ref,self.frame.size.width,self.frame.size.height/2.0+2.0);

    CGContextDrawPath(ref,kCGPathStroke);


    [self.titledrawInRect:CGRectMake(0.0,0.0,self.frame.size.width,self.frame.size.height)withFont:self.font];

}


使用:

- (void)viewDidLoad {

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColorlightGrayColor];

   UIFont *font = [UIFontsystemFontOfSize:14.0];

   NSString *str =@"封疆大吏估计";

   CGFloat width = [strsizeWithFont:font].width;

   CGFloat height = [strsizeWithFont:font].height;

   JJCrossedView *view = [[JJCrossedViewalloc]initWithFrame:CGRectMake(20.0,80.0, width, height)

                                                        title:str

                                                         font:font

                                                    lineColor:[UIColorgrayColor]];

    view.backgroundColor = [UIColorwhiteColor];

    [self.viewaddSubview:view];


}




0 0
原创粉丝点击