IOS开发(78)之绘制文本

1 前言

从今天起我们开始学习IOS的图形和动画。

在 Cocoa Touch 中,程序是由“窗口”和“视图”组成。一个带有 UI 的程序至少有一个窗口,这个窗口至少 包括一个到多个视图。在Cocoa Touch中,一个窗口就是一个UIWindow的实例。一般的,程序会打开到主窗 口,然后程序员会向这个窗口添加视图,来展示 UI 的不同的部分:例如按钮,文本,图像和自定义控件。所 有这些 UI 相关的组件是由 UIKit 来进行处理和绘制的。

Apple 为开发者提供了强有力的框架,来处理 iOS 和 OS X 中的图形和动画。下面是这些框架和技术:
UIKit
高级别框架,允许程序员创建视图,窗口,按钮,和其他 UI 相关的控件。它也将低层的 API 组合到一个 易于使用的高级别 API 中。
Quartz 2D
运行内部的用于 iOS 画图的主引擎;UIKit 使用了 Quarz。
Core Graphics
支持图形环境(后面会介绍),加载图片,绘制图片等等的框架。
Core Animation
顾名思义,iOS 上的动画框架。

今天我们来简单的学习一下在 iOS 设备上绘制文本。

2 代码实例

ZYViewControllerView.m

- (void)drawRect:(CGRect)rect{
    //设置字体样式
    UIFont *helveticaBold = [UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0f];
    /* Load the color */
    UIColor *magentaColor =[UIColor colorWithRed:0.5f
                                           green:0.0f blue:0.5f
                                           alpha:1.0f];
    /* Set the color in the graphical context */
    [magentaColor set];
    //文字内容
    NSString *myString = @"I Learn Really Fast";
    //在屏 幕上 x 轴的 25 及 y 轴 190 处以 30 点的字体画出一个简单的字符串
//    [myString drawAtPoint:CGPointMake(25, 190) withFont:helveticaBold];
    [myString drawInRect:CGRectMake(100,/* x */
                                    120, /* y */
                                    100, /* width */
                                    200) /* height */
                                    withFont:helveticaBold];
    //获得一个颜色用于Quartz 2D绘图。只读
    CGColorRef colorRef = [magentaColor CGColor];
    //返回颜色组件
    const CGFloat *components = CGColorGetComponents(colorRef);
    //返回颜色组件的个数
    NSUInteger componentsCount = CGColorGetNumberOfComponents(colorRef);
    NSUInteger counter = 0;
    for (counter = 0;counter <componentsCount; counter++){//循环输出
        NSLog(@"Component %lu = %.02f",(unsigned long)counter,components[counter]);
    }
    
}
工程截图


注意:需要将nib文件中的view视图的class设置为ZYViewControllerView


运行结果


控制台显示

2013-05-14 11:14:13.611 GraphicsStringTest[1030:c07] Component 0 = 0.50

2013-05-14 11:14:13.613 GraphicsStringTest[1030:c07] Component 1 = 0.00

2013-05-14 11:14:13.614 GraphicsStringTest[1030:c07] Component 2 = 0.50

2013-05-14 11:14:13.615 GraphicsStringTest[1030:c07] Component 3 = 1.00

3 结语

以上是所有内容,希望对大家有所帮助。

Demo下载地址:http://download.csdn.net/detail/u010013695/5372687

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值