1.畫一些簡單的文字,帶有陰影和基本效果,具體屬性去NSAttributedString.h。文章底部有描述

Paste_Image.png
2.畫圖片,給定大小為view大小
UIImage * image = [UIImage imageNamed:@"黃人"];
CGPoint p = CGPointMake(50, 50);
[image drawAtPoint:p];

Paste_Image.png
2.1給定某個點畫圖
UIImage * image = [UIImage imageNamed:@"阿貍頭像"];
CGPoint p = CGPointMake(50, 50);
[image drawAtPoint:p];

Paste_Image.png
2.2 平鋪
UIImage * image = [UIImage imageNamed:@"阿貍頭像"];
[image drawAsPatternInRect:self.bounds];

Paste_Image.png
具體代碼如下:
//
// DrawView.m
// 05-UIKit繪圖演練(熟悉)
//
// Created by 李亮 on 2016/12/1.
// Copyright ? 2016年 www.thelast.com. All rights reserved.
//
#import "DrawView.h"
@implementation DrawView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
[self drawImage];
}
- (void)drawImage{
//畫圖片
// UIImage *image = [UIImage imageNamed:@"001"];
//drawAtPoint繪制的是原始圖片的大小
//[image drawAtPoint:CGPointZero];
//把要繪制的圖片給填充到給定的區(qū)域當中.
//[image drawInRect:rect];
//裁剪(超過裁剪區(qū)域以久的內容,都會被自動裁剪掉)
//設置裁剪區(qū)域一定要在繪制之前進行設置
//UIRectClip(CGRectMake(0, 0, 50, 50));
//平鋪
//[image drawAsPatternInRect:self.bounds];
// UIRectFill(CGRectMake(50, 50, 50, 50));
// UIImage * img = [UIImage imageNamed:@"黃人"];
// UIImage * img = [UIImage imageNamed:@"阿貍頭像"];
// CGPoint p = CGPointMake(50, 50);
// [img drawAtPoint:p];
// [img drawAsPatternInRect:self.bounds];
// [img drawInRect:self.bounds];
}
- (void)drawString{
NSString * str = @"3年了3年了3年了3年了3年了";
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
//字體大小
dict[NSFontAttributeName] = [UIFont systemFontOfSize:30];
//設置顏色
dict[NSForegroundColorAttributeName] = [UIColor redColor];
//設置描邊
dict[NSStrokeColorAttributeName] = [UIColor greenColor];
dict[NSStrokeWidthAttributeName] = @2;
//設置陰影
NSShadow *shaw = [[NSShadow alloc] init];
shaw.shadowColor = [UIColor blueColor];
//設置陰影的偏移量
shaw.shadowOffset = CGSizeMake(1, 1);
shaw.shadowBlurRadius = 2;
dict[NSShadowAttributeName] = shaw;
[str drawAtPoint:CGPointZero withAttributes:dict];
}
@end
字符屬性
** 字符屬性可以應用于 attributed string 的文本中。**
NSString *const NSFontAttributeName;(字體)
NSString *const NSParagraphStyleAttributeName;(段落)
NSString *const NSForegroundColorAttributeName;(字體顏色)
NSString *const NSBackgroundColorAttributeName;(字體背景色)
NSString *const NSLigatureAttributeName;(連字符)
NSString *const NSKernAttributeName;(字間距)
NSString *const NSStrikethroughStyleAttributeName;(刪除線)
NSString *const NSUnderlineStyleAttributeName;(下劃線)
NSString *const NSStrokeColorAttributeName;(邊線顏色)
NSString *const NSStrokeWidthAttributeName;(邊線寬度)
NSString *const NSShadowAttributeName;(陰影)(橫豎排版)
NSString *const NSVerticalGlyphFormAttributeName;
常量
1> NSFontAttributeName(字體)
該屬性所對應的值是一個 UIFont 對象。該屬性用于改變一段文本的字體。如果不指定該屬性,則默認為12-point Helvetica(Neue)。
2> NSParagraphStyleAttributeName(段落)
該屬性所對應的值是一個 NSParagraphStyle 對象。該屬性在一段文本上應用多個屬性。如果不指定該屬性,則默認為 NSParagraphStyle 的defaultParagraphStyle 方法返回的默認段落屬性。
3> NSForegroundColorAttributeName(字體顏色)
該屬性所對應的值是一個 UIColor 對象。該屬性用于指定一段文本的字體顏色。如果不指定該屬性,則默認為黑色。
4> NSBackgroundColorAttributeName(字體背景色)
該屬性所對應的值是一個 UIColor 對象。該屬性用于指定一段文本的背景顏色。如果不指定該屬性,則默認無背景色。
5> NSLigatureAttributeName(連字符)
該屬性所對應的值是一個 NSNumber 對象(整數(shù))。連體字符是指某些連在一起的字符,它們采用單個的圖元符號。0 表示沒有連體字符。1 表示使用默認的連體字符。2表示使用所有連體符號。默認值為 1(注意,iOS 不支持值為 2)。
6> NSKernAttributeName(字間距)
該屬性所對應的值是一個 NSNumber 對象(整數(shù))。字母緊排指定了用于調整字距的像素點數(shù)。字母緊排的效果依賴于字體。值為 0 表示不使用字母緊排。默認值為0。
7> NSStrikethroughStyleAttributeName(刪除線)
該屬性所對應的值是一個 NSNumber 對象(整數(shù))。該值指定是否在文字上加上刪除線,該值參考“Underline Style Attributes”。默認值是NSUnderlineStyleNone。
8> NSUnderlineStyleAttributeName(下劃線)
該屬性所對應的值是一個 NSNumber 對象(整數(shù))。該值指定是否在文字上加上下劃線,該值參考“Underline Style Attributes”。默認值是NSUnderlineStyleNone。
9> NSStrokeColorAttributeName(邊線顏色)
該屬性所對應的值是一個 UIColor 對象。如果該屬性不指定(默認),則等同于 NSForegroundColorAttributeName。否則,指定為刪除線或下劃線顏色。更多細節(jié)見“Drawing attributedstrings that are both filled and stroked”。
10> NSStrokeWidthAttributeName(邊線寬度)
該屬性所對應的值是一個 NSNumber 對象(小數(shù))。該值改變描邊寬度(相對于字體size 的百分比)。默認為 0,即不改變。正數(shù)只改變描邊寬度。負數(shù)同時改變文字的描邊和填充寬度。例如,對于常見的空心字,這個值通常為3.0。
11> NSShadowAttributeName(陰影)
該屬性所對應的值是一個 NSShadow 對象。默認為 nil。
12> NSVerticalGlyphFormAttributeName(橫豎排版)
該屬性所對應的值是一個 NSNumber 對象(整數(shù))。0 表示橫排文本。1 表示豎排文本。在 iOS 中,總是使用橫排文本,0 以外的值都未定義。