Xcode更新过后,有些方法都进行了改变,Demo中有变化的都进行了简单的标记,具体以后遇见再说
创建一个UIView类,用init方法创建两种类型,显示多标签,创建静态方法进行调用,创建类方法进行调用
首先代码:用init方法创建两种类型,显示多标签
//初始化数组中是NSString类型的标签视图
init(frame: CGRect,textArray aArray:NSArray,textFont aFont:UIFont){
super.init(frame: frame);
//首先获取宽、高,确定显示的位置
let widthF:CGFloat = frame.size.width;
//然后创建标签视图,注意:这个方法传过来的数组中就是NSString类型,可以直接显示
var labelWidthF:CGFloat = 0.0;
var labelHeightF:CGFloat = 0.0;
for i in 0...aArray.count-1 {
//获取内容大小
let str = aArray[i] as! String;
let strSize:CGSize = self.sizeWithFont(content: str as NSString , font: aFont, maxSize: CGSize.init(width: CGFloat(MAXFLOAT), height:CGFloat(MAXFLOAT)));
//判断宽、高
if widthF < labelWidthF+strSize.width+20 {
labelWidthF = 0.0;
labelHeightF = labelHeightF+strSize.height+20;
}
//创建Lable
let labelL = UILabel.init(frame: CGRect.init(x: labelWidthF, y: labelHeightF, width: strSize.width+20, height: strSize.height+20));//CG系列变化了,没有了make,而是init
labelL.text = str as String;
labelL.textAlignment = NSTextAlignment.center;
labelL.backgroundColor = UIColor.lightGray;//UIColor变化了,少了color
labelL.font = aFont;
self.addSubview(labelL);
labelWidthF = labelWidthF+strSize.width+20;
}
}
//初始化数组中是NSDictionary类型的标签视图
init(frame: CGRect, textArray aArray:NSArray, keyStr aKeyStr:String, textFont aFont:UIFont){
super.init(frame: frame);
//首先获取宽、高,确定显示的位置
let widthF:CGFloat = frame.size.width;
//然后创建标签视图,注意:这个方法传过来的数组中是NSDictionary
var labelWidthF:CGFloat = 0.0;
var labelHeightF:CGFloat = 0.0;
for i in 0...aArray.count-1 {
//获取内容大小
let dic:NSDictionary = aArray[i] as! NSDictionary;
let str = dic[aKeyStr] as! String;
let strSize:CGSize = self.sizeWithFont(content: str as NSString, font: aFont, maxSize: CGSize.init(width: CGFloat(MAXFLOAT), height:CGFloat(MAXFLOAT)));
//判断宽、高
if widthF < labelWidthF+strSize.width+20 {
labelWidthF = 0.0;
labelHeightF = labelHeightF+strSize.height+20;
}
//创建Lable
let labelL = UILabel.init(frame: CGRect.init(x: labelWidthF, y: labelHeightF, width: strSize.width+20, height: strSize.height+20));//CG系列变化了,没有了make,而是init
labelL.text = str as String;
labelL.textAlignment = NSTextAlignment.center;
labelL.backgroundColor = UIColor.lightGray;//UIColor变化了,少了color
labelL.font = aFont;
self.addSubview(labelL);
labelWidthF = labelWidthF+strSize.width+20;
}
}
创建静态方法获取字符串大小
//创建静态方法
func sizeWithFont(content:NSString,font aFont:UIFont,maxSize aMaxSize:CGSize) -> CGSize {
let attrs:NSDictionary = [NSFontAttributeName : aFont];
return content.boundingRect(with: aMaxSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attrs as? [String : Any], context: nil).size;
}
创建一个类方法
//创建类方法
class func oneLabel(myLabel:UILabel) {
myLabel.layer.cornerRadius = 5;
myLabel.layer.borderColor = UIColor.blue.cgColor;
myLabel.layer.borderWidth = 1;
myLabel.clipsToBounds = true;
myLabel.adjustsFontSizeToFitWidth = true;
}
利用类方法创建一个Label
//创建一个Label
let myLabel = UILabel.init(frame: CGRect.init(x: 50, y: 320, width: 200, height: 50));
myLabel.text = "这是一个类方法设计的Lable";
BHMoreLabelView.oneLabel(myLabel: myLabel);
self.view.addSubview(myLabel);
有问题请留言。。。。。。。