string64位编码
func base64EncodedString()->String{
let utf8EncodeData:NSData! = self.data(using: String.Encoding.utf8, allowLossyConversion: true) as NSData!
return utf8EncodeData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
}
string64解码
func base64DecodedString()->String {
// 将base64字符串转换成NSData
let base64EncodedData = NSData(base64Encoded:self,options:NSData.Base64DecodingOptions(rawValue: 0))
// 对NSData数据进行UTF8解码
return String.init(data: base64EncodedData as! Data, encoding: String.Encoding.utf8)!
}
json字符串转字典
func dictionaryWithJsonString() -> [String:AnyObject]{
let data = self.data(using: String.Encoding.utf8)
let dic:[String:AnyObject] = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String : AnyObject]
return dic
}
改变文字之间的间距(左右)
/// - string: 转化的文字
/// - space: 间距大小
/// - Returns:
func getAttributedKernStringWithString(string:String ,space:CGFloat) -> NSAttributedString{
let attString = NSMutableAttributedString(string: string)
attString.addAttribute(NSKernAttributeName, value: space, range: NSRange(location:0,length:string.characters.count))
return attString
}
改变文字的行间距(纵向)
func getAttributedStringWithString(string:String ,lineSpace:CGFloat) -> NSAttributedString{
let attString = NSMutableAttributedString(string: string)
let style = NSMutableParagraphStyle()
style.lineSpacing = lineSpace
let range = NSMakeRange(0, string.characters.count)
attString.addAttribute(NSParagraphStyleAttributeName, value: style, range: range)
return attString
}
获取文字的宽度
/// - textStr: 传入的文字
/// - font: 字体
/// - height: 最大高度
/// - Returns: 文字宽度
static func getTexWidth(textStr:String,font:UIFont,height:CGFloat) -> CGFloat {
let normalText: NSString = textStr as NSString
let size = CGSize(width:1000, height:height)
let dic = NSDictionary(object: font, forKey: NSFontAttributeName as NSCopying)
let stringSize = normalText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? [String : AnyObject], context:nil).size
return stringSize.width
}
获取文字的高度
/// - text: 要计算的文字
/// - font: 字体大小
/// - maxWid: label宽度
/// - Returns: 字体高度
func caculateTextHeight(text:String,font:UIFont,maxWid:CGFloat) -> CGFloat {
let textSize = CGSize(width:maxWid,height:CGFloat.greatestFiniteMagnitude)
let string = text as NSString
let sizeFont = string.boundingRect(with: textSize, options: [.usesLineFragmentOrigin , .usesFontLeading , .usesDeviceMetrics] , attributes: [NSFontAttributeName:font], context: nil).size
return ceil(sizeFont.height)
}
一行文字多种颜色,字体大小
/// - string: 要转换的文本
/// - fromIndex: 开始的位置
/// - length: 长度
/// - font: 字体
/// - color: 颜色
/// - Returns: 文本
func changeTextColor(string:String,fromIndex:NSInteger,length:NSInteger,font:UIFont,color:UIColor) -> NSAttributedString {
let attrubuteStr = NSMutableAttributedString.init(string: string)
attrubuteStr.addAttributes([NSFontAttributeName:font,NSForegroundColorAttributeName:color], range: NSRange(location:fromIndex,length:length))
return attrubuteStr
}
判断电话号码
func isTelNumber(num:String)->Bool{
let mobile = "^1((3[0-9]|4[57]|5[0-35-9]|7[0678]|8[0-9])\\d{8}$)"
let CM = "(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)"
let CU = "(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)"
let CT = "(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)"
let regextestmobile = NSPredicate(format: "SELF MATCHES %@",mobile)
let regextestcm = NSPredicate(format: "SELF MATCHES %@",CM )
let regextestcu = NSPredicate(format: "SELF MATCHES %@" ,CU)
let regextestct = NSPredicate(format: "SELF MATCHES %@" ,CT)
if ((regextestmobile.evaluate(with: num) == true)
|| (regextestcm.evaluate(with: num) == true)
|| (regextestct.evaluate(with: num) == true)
|| (regextestcu.evaluate(with: num) == true))
{
return true
}
else
{
return false
}
}
网友评论