1: swift 富文本使用
(1) location : 开始的下标
(2) length : 长度
let attributeText =NSMutableAttributedString.init(string: "")
attributeText.addAttributes([NSAttributedString.Key.foregroundColor:UIColorFormRGB(rgbValue:0x97D5FF)], range:NSRange.init(location:0, length: user_name.count+1))
cell?.contentLabel.attributedText= attributeText
2: swift 动态计算字符串的宽 高 把下面的方法 复制到你的PCH 文件中
extension String {
funcboundingRect(with constrainedSize:CGSize, font:UIFont, lineSpacing:CGFloat? =nil) ->CGSize{
letattritube =NSMutableAttributedString(string:self)
letrange =NSRange(location:0, length: attritube.length)
attritube.addAttributes([NSAttributedString.Key.font: font], range: range)
iflineSpacing!=nil{
letparagraphStyle =NSMutableParagraphStyle()
paragraphStyle.lineSpacing= lineSpacing!
attritube.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: range)
}
letrect = attritube.boundingRect(with: constrainedSize, options: [.usesLineFragmentOrigin, .usesFontLeading], context:nil)
varsize = rect.size
ifletcurrentLineSpacing = lineSpacing {
// 文本的高度减去字体高度小于等于行间距,判断为当前只有1行
letspacing = size.height-font.lineHeight
ifspacing<=currentLineSpacing&&spacing>0{
size =CGSize(width: size.width, height: font.lineHeight)
}
}
returnsize
}
funcboundingRect(with constrainedSize:CGSize, font:UIFont, lineSpacing:CGFloat? =nil, lines:Int) ->CGSize{
iflines<0{
return.zero
}
letsize =boundingRect(with: constrainedSize, font: font, lineSpacing: lineSpacing)
iflines==0{
returnsize
}
letcurrentLineSpacing = (lineSpacing==nil) ? (font.lineHeight-font.pointSize) : lineSpacing!
letmaximumHeight = font.lineHeight*CGFloat(lines)+currentLineSpacing*CGFloat(lines-1)
ifsize.height>=maximumHeight {
returnCGSize(width: size.width, height: maximumHeight)
}
returnsize
}
}
3 : 颜色渐变 把下面的方法 复制到你的PCH 文件中
// 设置渐变颜色
funcsetGradientLayer(view :UIView, colors : [CGColor]) {
letgradientLayer =CAGradientLayer()
gradientLayer.colors= colors
gradientLayer.locations= [0.0,1.0]
gradientLayer.frame= view.bounds
letrect = view.bounds
letradio =CGSize(width:20, height:20)// 圆角尺寸
let corner = UInt8(UIRectCorner.bottomRight.rawValue) | UInt8(UIRectCorner.bottomLeft.rawValue) | UInt8(UIRectCorner.topLeft.rawValue) | UInt8(UIRectCorner.topRight.rawValue) // 这只圆角位置
letpath =UIBezierPath(roundedRect: rect, byRoundingCorners:UIRectCorner(rawValue:UIRectCorner.RawValue(corner)), cornerRadii: radio)
letmasklayer =CAShapeLayer()// 创建shapelayer
masklayer.frame= rect
masklayer.path= path.cgPath// 设置路径
gradientLayer.mask= masklayer
view.layer.insertSublayer(gradientLayer, at:0)
}
4 : JSON字符串与字典转换
//json 字符串转字典
func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{
letjsonData:Data= jsonString.data(using: .utf8)!
letdict =try?JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
ifdict!=nil{
returndictas!NSDictionary
}
return NSDictionary()
}
//json 字典转字符串
func getJSONStringFromDictionary(dictionary:NSDictionary) -> String {
if(!JSONSerialization.isValidJSONObject(dictionary)) {
print("无法解析出JSONString")
return""
}
letdata :NSData! =try?JSONSerialization.data(withJSONObject: dictionary, options: [])asNSData?
letJSONString =NSString(data:dataasData,encoding:String.Encoding.utf8.rawValue)
returnJSONString!asString
}
5 : 字符串下面加横线
func setAttrString(oldStr:String,foregroundColor:UIColor,strikethroughColor:UIColor) ->NSMutableAttributedString{
letattrString =NSMutableAttributedString(string: oldStr)
letattr: [NSAttributedString.Key:Any] = [.foregroundColor: foregroundColor, .strikethroughStyle:NSUnderlineStyle.single.rawValue, .strikethroughColor: strikethroughColor]
attrString.addAttributes(attr, range:NSRange(location:0, length: attrString.length))
returnattrString
}
6 : 颜色 16 进制 转换
///16进制转颜色
func UIColorFormRGB(rgbValue:UInt) ->UIColor{
return UIColor(red:CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green:CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue:CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha:CGFloat(1.0))
}
7 : 秒数 转换为 00:00:00
//传入秒数 转换成 00:00:00
func secondChangeTypeFormatDate(second:Int) ->String{
let formatter = DateComponentsFormatter.init()
//.pad为0:00:00格式 其他的我测试为 当时分秒任意为0时省略
formatter.zeroFormattingBehavior = .pad
formatter.allowedUnits = NSCalendar.Unit(rawValue: NSCalendar.Unit.hour.rawValue | NSCalendar.Unit.minute.rawValue | NSCalendar.Unit.second.rawValue)
//.positional 是数字:数字:数字格式 其他选项我测试为中文格式
formatter.unitsStyle=DateComponentsFormatter.UnitsStyle.positional
returnformatter.string(from:TimeInterval(second)) ??""
}
8 : UIWindow 上面添加控件
func addFullScreenView(sureView:UIView){
let view =UIApplication.shared.keyWindow!
view .addSubview(sureView)
}
网友评论