標籤:
1 @interface NJTextImage : UIView 2 @end 3 4 5 @implementation NJTextImage 6 7 - (void)drawRect:(CGRect)rect 8 { 9 // [self test];10 // 1.載入圖片到記憶體中11 UIImage *image = [UIImage imageNamed:@"bg"];12 13 // 利用OC方法將圖片繪製到layer上14 // 將圖片繪製到指定的位置15 [image drawAtPoint:CGPointMake(0, 0)];16 17 // 利用drawInRect方法繪製圖片到layer, 是通過展開原有圖片18 // [image drawInRect:CGRectMake(0, 0, 40, 40)];19 20 // 利用drawAsPatternInRec方法繪製圖片到layer, 是通過平鋪原有圖片21 // [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];22 23 }24 25 - (void)test26 {27 // 畫文字28 NSString *str = @"天氣好熱地方和電腦的開了房間了開始的解放路口時間瘋狂的老師;快瘋了;SD卡;焚枯食淡;李開複;順豐快遞說了;開發;展開放假快樂的設計風格看了就打算離開房間的數量會計分錄開始覺得";29 30 // 1.擷取上下文31 // CGContextRef ctx = UIGraphicsGetCurrentContext();32 // 2.繪圖33 // 不推薦使用C語言的方法繪製文字, 因為quraz2d中的座標系和UIkit中的座標系不一致, 繪製出來的文字是顛倒的, 而且通過C語言的方法繪製文字相當麻煩34 // CGContextSelectFont(<#CGContextRef c#>, <#const char *name#>, <#CGFloat size#>, <#CGTextEncoding textEncoding#>)35 // CGContextShowText(ctx, <#const char *string#>, <#size_t length#>)36 37 // 繪製矩形38 // 1.擷取上下文39 CGContextRef ctx = UIGraphicsGetCurrentContext();40 // 2.繪圖41 CGContextAddRect(ctx, CGRectMake(50, 50, 100, 100));42 // 3.渲染43 CGContextStrokePath(ctx);44 45 NSMutableDictionary *md = [NSMutableDictionary dictionary];46 // 設定文字顏色47 md[NSForegroundColorAttributeName] =[UIColor redColor];48 // 設定文字背景顏色49 md[NSBackgroundColorAttributeName] = [UIColor greenColor];50 // 設定文字大小51 md[NSFontAttributeName] = [UIFont systemFontOfSize:20];52 53 // 將文字繪製到指點的位置54 // [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];55 56 // 將文字繪製到指定的範圍內, 如果一行裝不下會自動換行, 當文字超出範圍後就不顯示57 [str drawInRect:CGRectMake(50, 50, 100, 100) withAttributes:nil];58 59 60 }61 62 @end
ios繪製基本圖形之浮水印背景