当前位置: 首页 > news >正文

猫猫学IOS(三十)UI之Quartz2D画图片画文字

回头看了看自己写的博客,猫猫决定以后更改风格
本意是想大家看效果直接拷贝代码能用,注释齐全也方便学习,但是发现这样对新手学习特别困难,以后素材源码不会上传了,有想要的可以去群里找。
以后风格基本是–>看标题–>看目录–>看图片–>看代码–>自己尝试。

当然,如果有好的给力Demo猫猫还是会原来那样放上来的。

猫猫分享,必须精品

原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents

画文字

效果:
这里写图片描述

代码:

//
//  NYTextView.m
//  画图片画文字
//
//  Created by apple on 15-5-5.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "NYTextView.h"

@implementation NYTextView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{

    // 画文字
    NSString *str = @"猫猫喵呜,!��猫猫喵呜,!��猫猫喵呜,!��猫猫喵呜,!��";

    // 1.获取上下文
    //    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.绘图
    // 不推荐使用C语言的方法绘制文字, 因为quraz2d中的坐标系和UIkit中的坐标系不一致, 绘制出来的文字是颠倒的, 而且通过C语言的方法绘制文字相当麻烦
    //    CGContextSelectFont(<#CGContextRef c#>, <#const char *name#>, <#CGFloat size#>, <#CGTextEncoding textEncoding#>)
    //    CGContextShowText(ctx, <#const char *string#>, <#size_t length#>)

    // 绘制矩形
    // 1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.绘图
    CGContextAddRect(ctx, CGRectMake(10, 10, 80, 80));
    // 3.渲染
    CGContextStrokePath(ctx);


    //    NSMutableDictionary *md = [NSMutableDictionary dictionary];
    //    // 设置文字颜色
    //    md[NSForegroundColorAttributeName] =[UIColor redColor];
    //    // 设置文字背景颜色
    //    md[NSBackgroundColorAttributeName] = [UIColor greenColor];
    //    // 设置文字大小
    //    md[NSFontAttributeName] = [UIFont systemFontOfSize:20];

    //    将文字绘制到指点的位置
    //    [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];

    //    将文字绘制到指定的范围内, 如果一行装不下会自动换行, 当文字超出范围后就不显示
    [str drawInRect:CGRectMake(20, 20, 80, 80) withAttributes:nil];
}


@end

画图片

平铺图片

这里写图片描述

//
//  NYImageView.m
//  画图片画文字
//
//  Created by apple on 15-5-5.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "NYImageView.h"

@implementation NYImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



- (void)drawRect:(CGRect)rect
{

    //    1.加载图片到内存中
    UIImage *image = [UIImage imageNamed:@"cat"];


    // 利用drawAsPatternInRec方法绘制图片到layer, 是通过平铺原有图片
    [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}


@end

拉伸图片

效果:
这里写图片描述

代码:

//
//  NYImageView.m
//  画图片画文字
//
//  Created by apple on 15-5-5.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "NYImageView.h"

@implementation NYImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



- (void)drawRect:(CGRect)rect
{

    //    1.加载图片到内存中
    UIImage *image = [UIImage imageNamed:@"cat"];


    // 利用OC方法将图片绘制到layer上

    // 利用drawInRect方法绘制图片到layer, 是通过拉伸原有图片
    [image drawInRect:CGRectMake(0, 0, 260, 260)];


}


@end

图片固定位置

效果:
这里写图片描述

代码:

//
//  NYImageView.m
//  画图片画文字
//
//  Created by apple on 15-5-5.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "NYImageView.h"

@implementation NYImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



- (void)drawRect:(CGRect)rect
{

    //    1.加载图片到内存中
    UIImage *image = [UIImage imageNamed:@"cat"];


    // 利用OC方法将图片绘制到layer上

    // 将图片绘制到指定的位置
    [image drawAtPoint:CGPointMake(100, 100)];
}

@end

转载于:https://www.cnblogs.com/znycat/p/4521021.html

相关文章:

  • Mac OS 安装boost
  • 使用Zookeeper存储Spring中properties的统一配置
  • 第四十二条:慎用可变参数
  • java使用Iterator、for循环同步数据
  • 不要盲目迷信多线程
  • 磁盘的5种卷,RAID—5的修复
  • IOS学习笔记--Objective-C之KVC、KVO
  • Skype for Business实战演练之八:安装Skype for Business Server 2015
  • android intent 传数据
  • java中HashSet详解(转)
  • 当我完善几年前的一个老项目时,我做了哪些改进
  • 简述ASP.NET MVC原理
  • 代码中的良好习惯从点滴做起
  • linux 下配置文件目录/etc/sysconfig
  • RabbitMQ与Redis做队列比较
  • 【面试系列】之二:关于js原型
  • Angular 响应式表单 基础例子
  • CentOS从零开始部署Nodejs项目
  • Java读取Properties文件的六种方法
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • mysql innodb 索引使用指南
  • Odoo domain写法及运用
  • quasar-framework cnodejs社区
  • Selenium实战教程系列(二)---元素定位
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • 提醒我喝水chrome插件开发指南
  • 通过几道题目学习二叉搜索树
  • 我从编程教室毕业
  • 小程序button引导用户授权
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 【云吞铺子】性能抖动剖析(二)
  • ionic入门之数据绑定显示-1
  • # Swust 12th acm 邀请赛# [ A ] A+B problem [题解]
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • #预处理和函数的对比以及条件编译
  • (16)Reactor的测试——响应式Spring的道法术器
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (pojstep1.1.2)2654(直叙式模拟)
  • (多级缓存)缓存同步
  • (附源码)springboot宠物医疗服务网站 毕业设计688413
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .Net IOC框架入门之一 Unity
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • .Net Remoting(分离服务程序实现) - Part.3
  • .NET6 命令行启动及发布单个Exe文件
  • .net打印*三角形
  • .NET开发不可不知、不可不用的辅助类(三)(报表导出---终结版)
  • .NET中winform传递参数至Url并获得返回值或文件
  • @Autowired标签与 @Resource标签 的区别
  • [ vulhub漏洞复现篇 ] Apache Flink目录遍历(CVE-2020-17519)
  • [23] 4K4D: Real-Time 4D View Synthesis at 4K Resolution
  • [30期] 我的学习方法