您的位置:首页 > 其它

画分割线

2015-07-05 00:30 288 查看
//
//  ViewController.m
//  背景平铺
//
//  Created by zjj on 15/7/4.
//  Copyright (c) 2015年 zjj. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textViewsLine;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self testViewLine];
//    [self testBgLine];
//    [self testsBg];
}
/**
*  给指定控件设置分割线
*/
- (void)testViewLine
{
CGFloat rowW = self.view.frame.size.width;
CGFloat rowH = 30;//self.textViewsLine.font.lineHeight;
// 创建一行背景图片
UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW, rowH), NO, 0.0);
// 取出上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 画个矩形框
[[UIColor whiteColor] set];
CGContextAddRect(ctx, CGRectMake(0, 0, rowW, rowH));
CGContextFillPath(ctx);
// 画线
[[UIColor blueColor] set];
CGFloat dividerX = 10;// 分割线与屏幕两边的间距
CGFloat lineWidth = 2;
CGContextSetLineWidth(ctx, lineWidth);//设定线宽
CGFloat dividerY = rowH - lineWidth;// Y值为背景图高度减去线宽度
CGContextMoveToPoint(ctx, dividerX, dividerY);
CGContextAddLineToPoint(ctx, rowW - dividerX, dividerY);
CGContextStrokePath(ctx);
// 取图
UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
self.textViewsLine.backgroundColor = [UIColor colorWithPatternImage:newImg];
}



/**
*  画 类似通讯录分割线
*/
- (void)testBgLine
{
CGFloat rowW = self.view.frame.size.width;
CGFloat rowH = 44;
// 创建一行背景图片
UIGraphicsBeginImageContextWithOptions(CGSizeMake(rowW, rowH), NO, 0.0);
// 取出上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 画个矩形框
[[UIColor whiteColor] set];
CGContextAddRect(ctx, CGRectMake(0, 0, rowW, rowH));
CGContextFillPath(ctx);
// 画线
[[UIColor blueColor] set];
CGFloat dividerX = 10;// 分割线与屏幕两边的间距
CGFloat lineWidth = 2;
CGContextSetLineWidth(ctx, lineWidth);//设定线宽
CGFloat dividerY = rowH - lineWidth;// Y值为背景图高度减去线宽度
CGContextMoveToPoint(ctx, dividerX, dividerY);
CGContextAddLineToPoint(ctx, rowW - dividerX, dividerY);
CGContextStrokePath(ctx);
// 取图
UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:newImg];
}




/**
*  将任意一张图 在不修改尺寸情况下 拉伸平铺到屏幕上
*/
- (void)testsBg
{
UIImage *oldImg = [UIImage imageNamed:@"金币啊"];
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
[oldImg drawInRect:self.view.bounds];
UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:newImg];
}
@end

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: