//
// UIGroupBox.swift
// groupBox
// Created by 开发 on 17/4/27.
// copyright © 2017年 黄涛. All rights reserved.
//
import UIKit
class UIGroupBox :UIVIEw {
func drawInfo(wIDth:CGfloat,txt:Nsstring){
drawText = txt;
drawStart = 30;
drawEnd = wIDth + 30;
}
var drawText:Nsstring ="";
var drawStart:CGfloat =0;
var drawEnd:CGfloat =0;
overrIDe func drawRect(rect:CGRect) {
self.backgroundcolor =UIcolor.graycolor();
let context =UIGraphicsGetCurrentContext()
//创建path
let path =CGPathCreateMutable()
let w:CGfloat =self.frame.wIDth;
let h:CGfloat =self.frame.height;
let space:CGfloat =50;
let Font2 = UIFont.boldSystemFontOfSize(16)
let dic:[String:AnyObject]? = [NSForegroundcolorAttributename:UIcolor.whitecolor(),NSFontAttributename :Font2];
drawText.drawAtPoint(CGPoint(x:drawStart + space + 10,y: space - 10),withAttributes:dic);
//1 x = 开始位置 + 间隔长度, y = 间隔长度
CGPathMovetoPoint(path,nil,drawStart + space,space)
//2 x = 间隔长度, y = 间隔长度
CGPathAddlinetoPoint(path,space,0)"> //3 x = 间隔长度, y = 总高度 - 间隔长度
CGPathAddlinetoPoint(path,h - space)
//4 x = 总宽度 - 间隔长度, y = 总高度 - 间隔长度
CGPathAddlinetoPoint(path,w - space,0)"> //5 x = 总宽度 - 间隔长度, y = 间隔长度
CGPathAddlinetoPoint(path,0)"> //6 x = 总宽度 - 间隔长度, y = 间隔长度
CGPathAddlinetoPoint(path,space + drawEnd,0)"> //添加到context中
CGContextAddpath(context,path)
CGContextSetRGBstrokecolor(context,1,0,1)
CGContextstrokePath(context)
}
}
// VIEwController.swift
//
import UIKit
class VIEwController: UIVIEwController {
@IBOutlet weak var mainVIEw: UIVIEw!
overrIDe func vIEwDIDLoad() {
super.vIEwDIDLoad()
mainVIEw.layoutIfNeeded();
let Box = UIGroupBox();
Box.drawInfo(120,txt: "标题测试应用")
Box.frame = CGRectMake(0,0,mainVIEw.frame.wIDth,mainVIEw.frame.height);
mainVIEw.addSubvIEw(Box);
}
overrIDe func dIDReceiveMemoryWarning() {
super.dIDReceiveMemoryWarning()
// dispose of any resources that can be recreated.
}
}
3 效果图
总结
以上是内存溢出为你收集整理的Swift 开发:自定义 GroupBox 案例全部内容,希望文章能够帮你解决Swift 开发:自定义 GroupBox 案例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)