1、系统消息的获取
- (NSString *)defaultUserAgentString
{
NSBundle *bundle = [NSBundlebundleForClass:[selfclass]];
NSString *appName = [bundleobjectForInfoDictionaryKey:@"CFBundleDisplayName"];
if (!appName) {
appName = [bundleobjectForInfoDictionaryKey:@"CFBundleName"];
}
NSData *latin1Data = [appNamedataUsingEncoding:NSUTF8StringEncoding];
appName = [[NSStringalloc]initWithData:latin1Dataencoding:NSISOLatin1StringEncoding];
if (!appName) {
returnnil;
}
NSString *appVersion =nil;
NSString *marketingVersionNumber = [bundleobjectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString *developmentVersionNumber = [bundleobjectForInfoDictionaryKey:@"CFBundleVersion"];
if (marketingVersionNumber && developmentVersionNumber) {
if ([marketingVersionNumberisEqualToString:developmentVersionNumber]) {
appVersion = marketingVersionNumber;
}else {
appVersion = [NSStringstringWithFormat:@"%@ rv:%@",marketingVersionNumber,developmentVersionNumber];
}
}else {
appVersion = (marketingVersionNumber ? marketingVersionNumber : developmentVersionNumber);
}
NSString *deviceName;
NSString *OSName;
NSString *OSVersion;
NSString *locale = [[NSLocalecurrentLocale]localeIdentifier];
UIDevice *device = [UIDevicecurrentDevice];
deviceName = [devicemodel];
OSName = [devicesystemName];
OSVersion = [devicesystemVersion];
NSString * defaultUserAgentString = [NSStringstringWithFormat:@"%@ %@ (%@; %@ %@; %@)", appName, appVersion, deviceName, OSName, OSVersion, locale];
return defaultUserAgentString;
}
2、
func drawSimpleLine(startPoint:CGPoint, endPoint:CGPoint) -> () {
let aPath = UIBezierPath()
aPath.move(to: startPoint)
aPath.addLine(to: endPoint)
aPath.close()
aPath.lineWidth = 20
UIColor.black.set()
aPath.stroke()
aPath.fill()
}
func drawSimpleLine2(startPoint:CGPoint, endPoint:CGPoint) -> () {
let aPath = UIBezierPath()
aPath.move(to: startPoint)
aPath.addLine(to: endPoint)
aPath.close()
aPath.lineWidth = 20
UIColor.red.set()
aPath.stroke()
aPath.fill()
}
画线代码
3、aes加密
h文件
NSString * aesEncryptString(NSString *content,NSString *key);
NSString * aesDecryptString(NSString *content,NSString *key);
NSData * aesEncryptData(NSData *data,NSData *key);
NSData * aesDecryptData(NSData *data,NSData *key);
m 文件
#import <CommonCrypto/CommonCryptor.h>
NSString const *kInitVector =@"1234567890123456";
size_t const kKeySize =kCCKeySizeAES128;
NSData * cipherOperation(NSData *contentData,NSData *keyData,CCOperation operation) {
NSUInteger dataLength = contentData.length;
voidconst *initVectorBytes = [kInitVectordataUsingEncoding:NSUTF8StringEncoding].bytes;
void const *contentBytes = contentData.bytes;
void const *keyBytes = keyData.bytes;
size_t operationSize = dataLength +kCCKeySizeAES128;
void *operationBytes = malloc(operationSize);
size_t actualOutSize = 0;
CCCryptorStatus cryptStatus =CCCrypt(operation,
kCCAlgorithmAES,
kCCOptionPKCS7Padding,
keyBytes,
kKeySize,
initVectorBytes,
contentBytes,
dataLength,
operationBytes,
operationSize,
&actualOutSize);
if (cryptStatus == kCCSuccess) {
return [NSDatadataWithBytesNoCopy:operationByteslength:actualOutSize];
}
free(operationBytes);
return nil;
}
NSString * aesEncryptString(NSString *content,NSString *key) {
NSData *contentData = [contentdataUsingEncoding:NSUTF8StringEncoding];
NSData *keyData = [keydataUsingEncoding:NSUTF8StringEncoding];
NSData *encrptedData = aesEncryptData(contentData, keyData);
return [encrptedDatabase64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}
NSString * aesDecryptString(NSString *content,NSString *key) {
NSData *contentData = [[NSDataalloc]initWithBase64EncodedString:contentoptions:NSDataBase64DecodingIgnoreUnknownCharacters];
NSData *keyData = [keydataUsingEncoding:NSUTF8StringEncoding];
NSData *decryptedData = aesDecryptData(contentData, keyData);
return [[NSStringalloc]initWithData:decryptedDataencoding:NSUTF8StringEncoding];
}
NSData * aesEncryptData(NSData *contentData,NSData *keyData) {
NSString *hint = [NSStringstringWithFormat:@"The key size of AES-%lu should be %lu bytes!",kKeySize *8,kKeySize];
NSCAssert(keyData.length ==kKeySize, hint);
return cipherOperation(contentData, keyData,kCCEncrypt);
}
NSData * aesDecryptData(NSData *contentData,NSData *keyData) {
NSString *hint = [NSStringstringWithFormat:@"The key size of AES-%lu should be %lu bytes!",kKeySize *8,kKeySize];
NSCAssert(keyData.length ==kKeySize, hint);
return cipherOperation(contentData, keyData,kCCDecrypt);
}
4、设置导航栏颜色和阴影线条
//设置背景颜色
- (void)js_setBackgroundColor:(UIColor *)backgroundColor
{
if (!self.customView) {
[selfsetBackgroundImage:[[UIImagealloc]init]forBarMetrics:UIBarMetricsDefault];
[selfsetShadowImage:[[UIImagealloc]init]];
self.customView = [[UIViewalloc]initWithFrame:CGRectMake(0, -20, [UIScreenmainScreen].bounds.size.width,64)];
self.customView.backgroundColor = [UIColorredColor];
[selfinsertSubview:self.customViewatIndex:0];
}
self.customView.backgroundColor = backgroundColor;
}
//设置阴影颜色
[self setShadowImage:[UIImage imageWithColor:[UIColor redColor] size:CGSizeMake(320,0.5)]];
这样不行
self.navigationController.navigationBar.backgroundColor = [UIColorredColor];
4、
self.imagePicker.allowsEditing = NO;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
选择相机只有拍照
5、创建一个单例
1、#define SHARED_INST_NAME(cls_name) shared_##cls_name##_instance
#define MAKE_SINGLETON(cls_name, shared_fname) \
static cls_name* SHARED_INST_NAME(cls_name) = NULL;\
+ (instancetype)shared_fname{@synchronized(self){if (!SHARED_INST_NAME(cls_name)){SHARED_INST_NAME(cls_name)=[[cls_name alloc]init];}}return SHARED_INST_NAME(cls_name);}\
+ (id)allocWithZone:(NSZone *)zone{@synchronized(self){if (!SHARED_INST_NAME(cls_name)){SHARED_INST_NAME(cls_name)=[super allocWithZone:zone];}}return SHARED_INST_NAME(cls_name);}
2、
#define singleH(name) +(instancetype)share##name;
#if __has_feature(objc_arc)
#define singleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)share##name\
{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}
#else
#define singleM static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)shareTools\
{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(oneway void)release\
{\
}\
\
-(instancetype)retain\
{\
return _instance;\
}\
\
-(NSUInteger)retainCount\
{\
return MAXFLOAT;\
}
6、+ (void)requestImagePickerAuthorization:(void(^)(WTAuthorizationStatus status))callback {
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ||
[UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
ALAuthorizationStatus authStatus = [ALAssetsLibraryauthorizationStatus];
if (authStatus ==ALAuthorizationStatusNotDetermined) {// 未授权
if ([UIDevicecurrentDevice].systemVersion.floatValue <8.0) {
[selfexecuteCallback:callbackstatus:WTAuthorizationStatusAuthorized];
} else {
[PHPhotoLibraryrequestAuthorization:^(PHAuthorizationStatus status) {
if (status ==PHAuthorizationStatusAuthorized) {
[selfexecuteCallback:callbackstatus:WTAuthorizationStatusAuthorized];
} else if (status == PHAuthorizationStatusDenied) {
[selfexecuteCallback:callbackstatus:WTAuthorizationStatusDenied];
} else if (status == PHAuthorizationStatusRestricted) {
[selfexecuteCallback:callbackstatus:WTAuthorizationStatusRestricted];
}
}];
}
} elseif (authStatus ==ALAuthorizationStatusAuthorized) {
[selfexecuteCallback:callbackstatus:WTAuthorizationStatusAuthorized];
} else if (authStatus == ALAuthorizationStatusDenied) {
[selfexecuteCallback:callbackstatus:WTAuthorizationStatusDenied];
} elseif (authStatus ==ALAuthorizationStatusRestricted) {
[selfexecuteCallback:callbackstatus:WTAuthorizationStatusRestricted];
}
} else {
[selfexecuteCallback:callbackstatus:WTAuthorizationStatusNotSupport];
}
}
7、
+ (NSString *)lz_RMBFormatterWithPrice:(CGFloat)price
{
NSNumberFormatter *numberFormatter = [[NSNumberFormatteralloc]init];
[numberFormatter setMinimumIntegerDigits:1];
numberFormatter.numberStyle =NSNumberFormatterNoStyle;
[numberFormatter setMaximumFractionDigits:2];
NSString *numberString = [numberFormatterstringFromNumber:@(price)];
return [@" ¥"stringByAppendingString:numberString];
}
8、增加属性图片
var attributedString = NSMutableAttributedString(string: "like after")
var textAttachment = NSTextAttachment()
textAttachment.image = UIImage(named: "a")
var attrStringWithImage = NSAttributedString.attributedStringWithAttachment(textAttachment)
attributedString.replaceCharactersInRange(NSMakeRange(4, 1), withAttributedString: attrStringWithImage)
var attributedString = NSMutableAttributedString(string: "like after")
var textAttachment = NSTextAttachment()
textAttachment.image = UIImage(named: "attention")
var attrStringWithImage = NSAttributedString.init(attachment: textAttachment)
// textAttachment.image =
attributedString.append(attrStringWithImage)
let label = UILabel.init(frame: CGRect(x:20,y:60,width:100,height:60))
label.attributedText = attributedString
view.addSubview(label)
9、x适配
if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0) {
make.edges.equalTo(self.view.safeAreaInsets);
} else {
make.edges.equalTo(self.view);
}
10、计算文本的行数
static func _numberOfLinesOfString(string: String, size: CGSize) -> Int {
let paraStyle = NSMutableParagraphStyle()
paraStyle.lineHeightMultiple = 1.5
paraStyle.alignment = .justified
let textStorage = NSTextStorage(string: string, attributes: [NSAttributedStringKey.paragraphStyle: paraStyle, NSAttributedStringKey.font: UIFont.pingFangLight(ofSize: 14)])
let textContainer = NSTextContainer(size: size)
textContainer.lineBreakMode = .byWordWrapping
textContainer.maximumNumberOfLines = 0
textContainer.lineFragmentPadding = 0
let layoutManager = NSLayoutManager()
layoutManager.textStorage = textStorage
layoutManager.addTextContainer(textContainer)
var numberOfLines = 0
var lineRange : NSRange = NSMakeRange(0, 0)
var index = 0
while index < layoutManager.numberOfGlyphs {
layoutManager.lineFragmentRect(forGlyphAt: index, effectiveRange: &lineRange)
index = NSMaxRange(lineRange)
numberOfLines += 1
}
return numberOfLines
}