iOS

iOS -- UIKit绘图

2015-11-12  本文已影响278人  iOS_成才录

注意:

实现:

// .h文件

#import <UIKit/UIKit.h>

@interface DrawTextView : UIView

@end

// .m 实现文件

#import "DrawTextView.h"

@implementation DrawTextView


/**
 *  重写该方法,进行视图重绘
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    /**
     *  富文本
     */
    // 向视图上画文字
    NSString *text = @"画文字";
 
    NSDictionary *dict = @{
                           NSFontAttributeName : [UIFont systemFontOfSize:50 ],
                           NSBackgroundColorAttributeName : [UIColor redColor],
                           NSStrokeColorAttributeName : [UIColor    greenColor],
                           NSStrokeWidthAttributeName : @"3"
                           };
    [text drawAtPoint:CGPointMake(0, 0) withAttributes:dict];
 
    // 向视图上绘制图片
    [self drawImage:CGRectMake(100, 100, 80, 80)];
}

/**
 *  向视图中绘制图片
 *
 *  @param rect 绘制图片的位置与尺寸
 */
- (void)drawImage:(CGRect) rect{
    UIImage *image = [UIImage imageNamed:@"Snip20150804_2"];
    
    //    [image drawAsPatternInRect:rect];
    //    [image drawAtPoint:CGPointMake(0, 0)];
    
    [image drawInRect:rect];
}

/**
 *  向视图上画文字
 */
- (void)drawText{
    NSString *text = @"呵呵";
    
        [text drawInRect:self.bounds withAttributes:nil];
        [text drawAtPoint:CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5) withAttributes:nil];
}
@end
上一篇 下一篇

猜你喜欢

热点阅读