string的Base64加密/解密
oc 加密
- (NSString *)base64EncodedString {
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
return [data base64EncodedStringWithOptions:0];
}
oc解密
- (NSString *)base64DecodedString {
NSData *data = [[NSData alloc]initWithBase64EncodedString:self options:0];
return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}
swift 加密
// extension String
func base64EncodedString()->String{
let utf8EncodeData:NSData! = self.data(using: String.Encoding.utf8, allowLossyConversion: true) as NSData!
return utf8EncodeData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue:0))
}
swift 解密
// extension String
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)!
}
oc字典转json字符串
+ (NSString *)dictionaryToJson:(NSDictionary *)dic {
NSError *parseError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
swift字典转json字符串
func dictionaryToJson(dic:[String:Any])->String{
let data = try? JSONSerialization.data(withJSONObject: dic, options: [])
return String(data:data!, encoding: String.Encoding.utf8)!
}
修改文字之间的间距
/// 修改文字之间的间距
///
/// - Parameters:
/// - string: 转化的文字
/// - space: 间距大小
/// - Returns: NSMutableAttributedString
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
}
计算文字宽度
/// 计算文字宽度
///
/// - Parameters:
/// - 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
}
计算文字高度
/// 计算文字高度
///
/// - Parameters:
/// - 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)
}
label设置有行距的AttributedString
/// label设置有行距的AttributedString
///
/// - Parameters:
/// - string: 要显示的string
/// - lineSpace: 行距
/// - Returns: NSMutableAttributedString
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
}
多种颜色文本
/// 多种颜色文本
///
/// - Parameters:
/// - 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
}
}