1、UITextView中打开或禁用复制、剪切、选择、全选等功能:
//继承UITextView重写这个方法
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
//返回NO为禁用,YES为开启
//粘贴
if(action == @selector(paste:)) return NO;
//剪切
if(action == @selector(cut:)) return NO;
//复制
if(action == @selector(copy:)) return NO;
//选择
if(action == @selector(select:)) return NO;
//选中全部
if(action == @selector(selectAll:)) return NO;
//删除
if(action == @selector(delete:)) return NO;
//分享
if(action == @selector(share)) return NO;
return [super canPerformAction:action withSender:sender];
}
2、为一个View添加虚线边框
CAShapeLayer * border = [CAShapeLayer layer];
border.strokeColor = [UIColor colorWithRed:67/255.0f green:37/255.0f
blue:83/255.0f alpha:1].CGColor;
border.fillColor = nil;
border.lineDashPattern = @[@4,@2];
border.path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
border.frame = view.bounds;
[view.layer addSublayer:border];
3、修改cell.imageView的大小
UIImage * icon = [UIImage imageNamed:@""];
CGSize itemSize = CGSizeMake(30,30);
UIGraphicsBeginImageContextWithOptions(itemSize,NO,0.0);
CGRect imageRect = CGRectMake(0.0,0.0,itemSize.width,itemSize.height);
[icon drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
4、在指定的宽度下,让UILabel自动设置最佳font
label.adjustsFontSizeToFitWith = YES;
5、在状态栏增加网络请求的菊花,类似safari加载网页的时候状态栏菊花
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
6、tableViewCell分割线顶到头
-(void)tableView:(UITableView *)tableView willDisplayerCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setSeparatorInset:UIEdgeInsetsZero];
[cell setLayoutMargins:UIEdgeInsetsZero];
cell.preservesSuperviewLayoutMargins = NO;
}
- (void)viewDidLayoutSubviews{
[self.tableView setSepatatorInset:UIEdgeInsetsZero];
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
7、设置tableView分割线颜色
[self.tableView setSeparatorColor:[UIColor myColor]];
8、设置屏幕方向
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft)
forKey:@"orientation"];
9、在非ViewController的地方弹出UIAlertController对话框
//最好抽成一个分类
UIAlertController * alertController = [UIAlertController
alertControllerWithTitle:@"Title" message:@"message"
preferredStyle:UIAlertControllerStyleAlert];
id rootViewController = [UIApplication
sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController = ((UINavigationController
*)rootViewController).viewController.firstObject;
}
if([rootViewController isKindOfClass:[UITabBarController class]])
{
rootViewController = ((UITabBarController
*)rootViewController).selectedViewController;
}
[rootViewController presentViewController:alertController animated:YES
completetion:nil];
10、禁用系统滑动返回功能
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if([self.navigationController respondsToSelector:@selector
(interactivePopGestureRecognizer)]){
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
if([self.navigationController respondsToSelector:@selector
(interactivePopGestureRecognizer)]){
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer
{
return NO;
}
11、合并两个图片
+ (UIImage*)mergeImage:(UIImage*)firstImage withImage:(UIImage*)secondImage {
CGImageRef firstImageRef = firstImage.CGImage;
CGFloat firstWidth = CGImageGetWidth(firstImageRef);
CGFloat firstHeight = CGImageGetHeight(firstImageRef);
CGImageRef secondImageRef = secondImage.CGImage;
CGFloat secondWidth = CGImageGetWidth(secondImageRef);
CGFloat secondHeight = CGImageGetHeight(secondImageRef);
CGSize mergedSize = CGSizeMake(MAX(firstWidth, secondWidth), MAX(firstHeight,
secondHeight));
UIGraphicsBeginImageContext(mergedSize);
[firstImage drawInRect:CGRectMake(0, 0, firstWidth, firstHeight)];
[secondImage drawInRect:CGRectMake(0, 0, secondWidth, secondHeight)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
12、AFNetworking监听网络状态
// 监听网络状况
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
break;
case AFNetworkReachabilityStatusNotReachable: {
[SVProgressHUD showInfoWithStatus:@"当前设备无网络"];
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
[SVProgressHUD showInfoWithStatus:@"当前Wi-Fi网络"];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
[SVProgressHUD showInfoWithStatus:@"当前蜂窝移动网络"];
break;
default:
break;
}
}];
[mgr startMonitoring];
13、获取设备mac地址
+ (NSString *)macAddress {
int mib[6];
size_t len;
char *buf;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
if((mib[5] = if_nametoindex("en0")) == 0) {
printf("Error: if_nametoindex error\n");
return NULL;
}
if(sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 1\n");
return NULL;
}
if((buf = malloc(len)) == NULL) {
printf("Could not allocate memory. Rrror!\n");
return NULL;
}
if(sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 2");
return NULL;
}
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
ptr = (unsigned char *)LLADDR(sdl);
NSString *outstring = [NSString stringWithFormat:@"X:X:X:X:X:X",
*ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
free(buf);
return outstring;
}