1、获取当前类名
[NSString stringWithUTF8String:object_getClassName(self)];
2、获取一些基本信息
+ (NSString *) getOsType {
return [[UIDevice currentDevice] model];
}
+ (float) getOsVersion {
return [[[UIDevice currentDevice] systemVersion] floatValue];
}
+ (NSString *) getDeviceId {
return [[UIDevice currentDevice].identifierForVendor UUIDString];
}
+ (NSString *) getAppVersion {
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}
+ (NSString *) getChannel {
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"ChannelId"];
}
3.1、查看某个对象是否遵循某种协议
if ([popController conformsToProtocol:@protocol(UIPopoverControllerDelegate)]) {
// 遵循某种协议,popController遵循了UIPopoverControllerDelegate协议
}
3.2、是否实现了某个方法
if (self.delegate && [self.delegate respondsToSelector:@selector(pageView:didScrollToView:forIndex:)]) {
[self.delegate pageView:self didScrollToView:self.viewArr[currentIndex] forIndex:currentIndex];
}
4、设置UILabel自动换行
+ (void) setAutoBreakLine: (UILabel *) label {
label.numberOfLines = 10;
CGSize size = CGSizeMake(300, 1000);
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, labelSize.width, labelSize.height);
}
5、ImageView 响应点击事件
- (void) awakeFromNib {
[super awakeFromNib];
UITapGestureRecognizer*tapRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickEventOnImage:)];
self.imageView.userInteractionEnabled=YES;
[self.imageView addGestureRecognizer:tapRecognizer];
}
- (void) clickEventOnImage: (UIImageView *) sender {
NSLog(@"Click Image View......");
}
6、IOS Webview 背景透明
self.webView.backgroundColor = [UIColor clearColor];
self.webView.opaque = NO;
7、IOS 屏幕大小
[[UIScreen mainScreen] bounds]
8、有时使用long会出现数据错误的问题,使用int64_t代替即可
9、json字符串转换成dictionary
NSString *jsonString = [userInfo valueForKey:@"notification"];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:nil];
10、UIImage和UIColor相互转换
+ (UIImage*) createImageWithColor: (UIColor*) color{
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
+ (UIColor *) createColorWithImage: (UIImage *) image {
return [UIColor colorWithPatternImage:image];
}
11、消除navigation和tabbar上得那条横线,createImageWithColor参考上一条
//如果我们的高度是高于49的话,可以使用
[self.tabBar setShadowImage: [ImageUtil createImageWithColor:[UIColor clearColor]]];
[self.tabBar setBackgroundImage: [ImageUtil createImageWithColor:[UIColor clearColor]]];
12、ios7设置状态栏的字体为白色
1、在info.plist中设置 View controller-based status bar appearance 为NO
2、在程序代码中设置前景色,我的是在viewDidLoad中
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
13、ios设置navigationbar的背景图片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_bg"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.clipsToBounds = YES;
14、设置navigationbar文字颜色
[self.navigationController.navigationBar setTitleTextAttributes:@{
NSForegroundColorAttributeName :[UIColor whiteColor],
UITextAttributeTextColor: [UIColor whiteColor]
}];
可以设置如下内容:
使用下面的文本属性键:
UITextAttributeFont - 字体
UITextAttributeTextColor - 文字颜色
UITextAttributeTextShadowColor - 文字阴影颜色
UITextAttributeTextShadowOffset - 偏移用于文本阴影
15、设置图片为圆角
self.thumbnailImageView.layer.masksToBounds = YES;
self.thumbnailImageView.layer.cornerRadius = 10.0;
16、遍历所有字体
int i = 0;
for(NSString *fontfamilyname in [UIFont familyNames]) {
NSLog(@"family:'%@'",fontfamilyname);
for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname]){
NSLog(@"\tfont:'%@'",fontName);
}
NSLog(@"-------------%d",i++);
}