一般使用UIKit給我們提供的繪圖來繪制一些文字,圖片這些東西.
UIKit給我們提供畫圖的方法底層也是分為四步.所以也必須在drawRect方法當中去寫.
1.如何畫文字?
先創建好要畫的文字
使用UIKit提供的方法進行繪制.
方法說明:
drawAtPoint:要畫到哪個位置
withAttributes:文本的樣式.
[str drawAtPoint:CGPointZero withAttributes:nil];
2.如何添加繪制文字屬性?
通過繪制方法的最后一個屬性withAttributes來設置文字屬性.
它要求傳入的是一個字典.它是通過字典的key和Value的形式來設置文字樣式.
那傳什么key,什么值我們可以在UIKit頭文件當中的NSAttributedString類當中去找.
使用形式如下:
創建一個可變的字典,設置key,value
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:50];
顏色
dict[NSForegroundColorAttributeName] = [UIColor redColor];
設置邊框顏色
dict[NSStrokeColorAttributeName] = [UIColor redColor];
dict[NSStrokeWidthAttributeName] = @1;
陰影
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(10, 10);
shadow.shadowColor = [UIColor greenColor];
shadow.shadowBlurRadius = 3;
dict[NSShadowAttributeName] = shadow;
3.drawAtPoint:和drawInRect:的區別?
drawAtPoint:不能夠自動換行
drawInRect:能夠自動換行
4.如果繪制圖片?
繪制圖片同樣開始要先把圖片素材導入.
AtPoint:參數說明圖片要繪制到哪個位置.
通過調用UIKit的方法drawAtPoint:CGPointZero方法進行繪制;
5.在繪制圖片過程當中.drawAtPoint:和drawInRect:兩個方法的區別?
drawAtPoint:繪制出來的圖圖片跟圖片的實際尺寸一樣大
drawInRect:使用這個方法繪制出來的圖片尺寸會和傳入的rect區域一樣大.
6.如果進行平鋪圖片?
[image drawaspatternInRect:rect];
7.如何選用UIKit提供的方法快速畫一個矩形?
快速的用矩形去填充一個區域
UIRectFill(rect);
8.如何利用UIKit裁剪一個區域?
UIRectClip(CGRectMake(0, 0, 50, 50));
這個方法必須要設置好裁剪區域,才能有裁剪
新聞熱點
疑難解答