文字,水印,slider滑动,pickerView的实现

//字体,水印

#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滑动

#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];
}


#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);
}
@end



#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}];
}

转载于:https://my.oschina.net/u/2501648/blog/541033

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值