//字體,水印
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