1.畫一些簡單的文字紫新,帶有陰影和基本效果蜈垮,具體屬性去NSAttributedString.h熙卡。文章底部有描述
2.畫圖片芍锦,給定大小為view大小
UIImage * image = [UIImage imageNamed:@"黃人"];
CGPoint p = CGPointMake(50, 50);
[image drawAtPoint:p];
2.1給定某個(gè)點(diǎn)畫圖
UIImage * image = [UIImage imageNamed:@"阿貍頭像"];
CGPoint p = CGPointMake(50, 50);
[image drawAtPoint:p];
2.2 平鋪
UIImage * image = [UIImage imageNamed:@"阿貍頭像"];
[image drawAsPatternInRect:self.bounds];
具體代碼如下:
//
// 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ū)域當(dāng)中.
//[image drawInRect:rect];
//裁剪(超過裁剪區(qū)域以久的內(nèi)容,都會(huì)被自動(dòng)裁剪掉)
//設(shè)置裁剪區(qū)域一定要在繪制之前進(jìn)行設(shè)置
//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];
//設(shè)置顏色
dict[NSForegroundColorAttributeName] = [UIColor redColor];
//設(shè)置描邊
dict[NSStrokeColorAttributeName] = [UIColor greenColor];
dict[NSStrokeWidthAttributeName] = @2;
//設(shè)置陰影
NSShadow *shaw = [[NSShadow alloc] init];
shaw.shadowColor = [UIColor blueColor];
//設(shè)置陰影的偏移量
shaw.shadowOffset = CGSizeMake(1, 1);
shaw.shadowBlurRadius = 2;
dict[NSShadowAttributeName] = shaw;
[str drawAtPoint:CGPointZero withAttributes:dict];
}
@end
字符屬性
** 字符屬性可以應(yīng)用于 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(字體)
該屬性所對應(yīng)的值是一個(gè) UIFont 對象裹虫。該屬性用于改變一段文本的字體。如果不指定該屬性融击,則默認(rèn)為12-point Helvetica(Neue)筑公。
2> NSParagraphStyleAttributeName(段落)
該屬性所對應(yīng)的值是一個(gè) NSParagraphStyle 對象。該屬性在一段文本上應(yīng)用多個(gè)屬性尊浪。如果不指定該屬性匣屡,則默認(rèn)為 NSParagraphStyle 的defaultParagraphStyle 方法返回的默認(rèn)段落屬性。
3> NSForegroundColorAttributeName(字體顏色)
該屬性所對應(yīng)的值是一個(gè) UIColor 對象拇涤。該屬性用于指定一段文本的字體顏色捣作。如果不指定該屬性,則默認(rèn)為黑色鹅士。
4> NSBackgroundColorAttributeName(字體背景色)
該屬性所對應(yīng)的值是一個(gè) UIColor 對象券躁。該屬性用于指定一段文本的背景顏色。如果不指定該屬性掉盅,則默認(rèn)無背景色也拜。
5> NSLigatureAttributeName(連字符)
該屬性所對應(yīng)的值是一個(gè) NSNumber 對象(整數(shù))。連體字符是指某些連在一起的字符趾痘,它們采用單個(gè)的圖元符號(hào)慢哈。0 表示沒有連體字符。1 表示使用默認(rèn)的連體字符永票。2表示使用所有連體符號(hào)卵贱。默認(rèn)值為 1(注意滥沫,iOS 不支持值為 2)。
6> NSKernAttributeName(字間距)
該屬性所對應(yīng)的值是一個(gè) NSNumber 對象(整數(shù))艰赞。字母緊排指定了用于調(diào)整字距的像素點(diǎn)數(shù)佣谐。字母緊排的效果依賴于字體。值為 0 表示不使用字母緊排方妖。默認(rèn)值為0狭魂。
7> NSStrikethroughStyleAttributeName(刪除線)
該屬性所對應(yīng)的值是一個(gè) NSNumber 對象(整數(shù))。該值指定是否在文字上加上刪除線党觅,該值參考“Underline Style Attributes”雌澄。默認(rèn)值是NSUnderlineStyleNone。
8> NSUnderlineStyleAttributeName(下劃線)
該屬性所對應(yīng)的值是一個(gè) NSNumber 對象(整數(shù))杯瞻。該值指定是否在文字上加上下劃線镐牺,該值參考“Underline Style Attributes”。默認(rèn)值是NSUnderlineStyleNone魁莉。
9> NSStrokeColorAttributeName(邊線顏色)
該屬性所對應(yīng)的值是一個(gè) UIColor 對象睬涧。如果該屬性不指定(默認(rèn)),則等同于 NSForegroundColorAttributeName旗唁。否則畦浓,指定為刪除線或下劃線顏色。更多細(xì)節(jié)見“Drawing attributedstrings that are both filled and stroked”检疫。
10> NSStrokeWidthAttributeName(邊線寬度)
該屬性所對應(yīng)的值是一個(gè) NSNumber 對象(小數(shù))讶请。該值改變描邊寬度(相對于字體size 的百分比)。默認(rèn)為 0屎媳,即不改變夺溢。正數(shù)只改變描邊寬度。負(fù)數(shù)同時(shí)改變文字的描邊和填充寬度烛谊。例如风响,對于常見的空心字,這個(gè)值通常為3.0丹禀。
11> NSShadowAttributeName(陰影)
該屬性所對應(yīng)的值是一個(gè) NSShadow 對象状勤。默認(rèn)為 nil。
12> NSVerticalGlyphFormAttributeName(橫豎排版)
該屬性所對應(yīng)的值是一個(gè) NSNumber 對象(整數(shù))湃崩。0 表示橫排文本荧降。1 表示豎排文本接箫。在 iOS 中攒读,總是使用橫排文本,0 以外的值都未定義辛友。