//字体,水印
ide
#import <UIKit/UIKit.h>
@interface myImage : UIView
@property(nonatomic,assign)float value;
@property(nonatomic,assign)float valueOne;
@end 布局
#import "font.h"
@implementation font
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
// [self drawText:context];
// [self drawImage:context];
[self drawImageAtImageContext:context];
}
#pragma mark 绘制文字
-(void)drawText :(CGContextRef)context{
NSString *str = @"今天举行了一年一度的绘图大赛,同窗们踊跃参与";
//定义文字显示区域
CGRect rect = CGRectMake(20, 50, 335, 300);
CGContextAddRect(context, rect);
[[UIColor colorWithRed:0.8 green:0.6 blue:0.8 alpha:0.9]set];
CGContextDrawPath(context, kCGPathFill);
//创建一个段落样式
NSMutableParagraphStyle *style =[[NSMutableParagraphStyle alloc]init];
style.alignment = NSTextAlignmentCenter;
//绘制文字,设置属性(字体大小,颜色)
[str drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:25],
NSForegroundColorAttributeName:[UIColor grayColor],
NSParagraphStyleAttributeName:style}];
}
#pragma mark 绘制图片
-(void)drawImage:(CGContextRef)context{
UIImage *image = [UIImage imageNamed:@"1"];
//1.从某一点开始绘制
[image drawAtPoint:CGPointMake(0,20)];
//2.绘制到指定的矩形中,注意:若是大小不合适会进行拉伸
// [image drawInRect:CGRectMake(0, 0, 375, 667)];
[image drawInRect:CGRectMake(0, 20, 375, 185)];
//3.平铺绘制
[image drawAsPatternInRect:CGRectMake(0, 0, 375, 667)];
}
#pragma mark 水印(图片上加图片)
-(void)drawImageAtImageContext:(CGContextRef)context{
//开始图片上下文(设置画布大小)
UIGraphicsBeginImageContext(CGSizeMake(375, 667));
//建立图片
UIImage *image = [UIImage imageNamed:@"1"];
//裁剪图片->椭圆
context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, CGRectMake(0, 0, 375, 667));
CGContextClip(context);//裁剪
[image drawInRect:CGRectMake(0, 0, 375, 667)];
//添加水印
NSString *str = @"photo by Bill";
[str drawInRect:CGRectMake(375/2.0 - 50, 667/2.0 - 10, 100, 30) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:15],NSForegroundColorAttributeName:[UIColor whiteColor]}];
//返回绘制的新图像
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭对应的上下文
UIGraphicsEndImageContext();
[newImage drawInRect:CGRectMake(0, 0, 375, 667)];
//保存图片
NSData *data = UIImagePNGRepresentation(newImage);
[data writeToFile:@"/Users/dc020/Desktop/Bill.png" atomically:YES];
}
@end字体
//sliper滑动
atom
#import "ViewController.h"
#import "font.h"
#import "myView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
font *view = [[font alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
myView *myview = [[myView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
view.backgroundColor = [UIColor colorWithRed:0.8 green:0.9 blue:0.8 alpha:0.6];
myview.backgroundColor = [UIColor colorWithRed:0.8 green:0.6 blue:0.8 alpha:0.9];
// [self.view addSubview:view];
[self.view addSubview:myview];
}
.net
#import "myImage.h"
@implementation myImage
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
NSLog(@"被刷新");
self.backgroundColor = [UIColor blackColor];
[self drawImage];
[self drawEllipse:context];
}
-(void)setValue:(float)value{
_value = value;//本来有的赋值操做
//刷新机制
[self setNeedsDisplay];
}
-(void)setValueOne:(float)valueOne{
_valueOne =valueOne;
[self setNeedsDisplay];
}
-(void)drawImage{
UIImage *image = [UIImage imageNamed:@"2"];
[image drawInRect:CGRectMake((335 - _value)/2, (335-_value)/2,_value , _value)];
}
-(void)drawEllipse:(CGContextRef)context{
CGContextMoveToPoint(context, 335/2.0, 335/2.0);
CGContextAddArc(context, 335/2, 335/2, 335/2, 0.0, _valueOne, 0);
[[UIColor redColor]set];
CGContextDrawPath(context,kCGPathFillStroke);
}
@endcomponent
#import "ViewController.h"
@interface ViewController (){
myImage *view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//页面布局
view = [[myImage alloc]initWithFrame:CGRectMake(20, 100, 335, 335)];
[self.view addSubview:view];
// 添加拖动条
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(20, 500, 335, 50)];
slider.maximumValue = 335;
slider.minimumValue = 0;
//添加拖动条拖动事件
[slider addTarget:self action:@selector(mySlider:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
UISlider *slider1 = [[UISlider alloc]initWithFrame:CGRectMake(20, 600, 335, 50)];
slider1.maximumValue = M_PI * 2;
slider1.minimumValue = 0;
[slider1 addTarget:self action:@selector(mySliderTwo:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider1];
}
-(void)mySlider:(UISlider *)slider{
NSLog(@"%f",slider.value);
view.value = slider.value;
}
-(void)mySliderTwo:(UISlider *)slider{
view.valueOne = slider.value;
}事件
//PickerView的实现
#import "ViewController.h"
#import "Picker.h"
@interface ViewController ()
{
Picker *picker;
UIPickerView *pickerview;
NSArray *array;
NSArray *arr;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
picker = [[Picker alloc]initWithFrame:CGRectMake(20, 100, 335, 335)];
picker.backgroundColor = [UIColor whiteColor];
[self.view addSubview:picker];
pickerview = [[UIPickerView alloc]initWithFrame:CGRectMake(20, 350, 335, 100)];
pickerview.delegate =self;
pickerview.dataSource = self;
array = @[@"十号字体",@"十一号字体",@"十二号字体",@"十三号字体",@"十四号字体",@"十五号字体",@"十六号字体",@"十七号字体",@"十八号字体",@"十九号字体"];
arr = @[@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19"];
[self.view addSubview:pickerview];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return array.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return array[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
picker.value =[arr[row] floatValue];
}
图片
#import "Picker.h"
@implementation Picker
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawText:context];
}
-(void)setValue:(float)value{
_value = value;
[self setNeedsDisplay];
}
-(void)drawText:(CGContextRef)context{
NSString *str = @"Hello Word!";
CGRect rect = CGRectMake(20, 20, 335, 300);
CGContextAddRect(context, rect);
[[UIColor whiteColor]set];
CGContextDrawPath(context, kCGPathFill);
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
style.alignment = NSTextAlignmentCenter;
[str drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:_value],NSForegroundColorAttributeName:[UIColor redColor],NSParagraphStyleAttributeName:style}];
}
ip