欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

分享一些iOS开发实用的小技巧

程序员文章站 2023-12-17 21:42:46
1.设置navigationbar title颜色 uicolor *whitecolor = [uicolor whitecolor]; nsdiction...

1.设置navigationbar title颜色

 uicolor *whitecolor = [uicolor whitecolor];
nsdictionary *dic = [nsdictionary dictionarywithobject:whitecolor forkey:nsforegroundcolorattributename];
[self.navigationcontroller.navigationbar settitletextattributes:dic];

2.获取uicolor rgb

 uicolor *color = [uicolor colorwithred:0.0 green:0.0 blue:1.0 alpha:1.0];
const cgfloat *components = cgcolorgetcomponents(color.cgcolor);
nslog(@"red: %f", components[0]);
nslog(@"green: %f", components[1]);
nslog(@"blue: %f", components[2]);
nslog(@"alpha: %f", components[3]);

3.修改textfield的placeholder的字体颜色、大小

 [self.textfield setvalue:[uicolor redcolor] forkeypath:@"_placeholderlabel.textcolor"];
[self.textfield setvalue:[uifont boldsystemfontofsize:16] forkeypath:@"_placeholderlabel.font"];

4.将color转为uiimage

 - (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;
}

5.加载启动图的时候隐藏statusbar

在info.plist中加入status bar is initially hidden 设置为yes

分享一些iOS开发实用的小技巧

6.获取按钮title的size

/**
 * 获取按钮title的size
 */
- (cgfloat)getbtntitlewidth:(uibutton*)btn {
 cgsize titlesize = [btn.titlelabel.text sizewithattributes:@{nsfontattributename:btn.titlelabel.font}];
 return titlesize;
}

7.设置status bar颜色

uiview *view = [[uiview alloc] initwithframe:cgrectmake(0, -20, screenwidth, 20)];[view setbackgroundcolor:color_app_main];
[viewcontroller.navigationcontroller.navigationbar addsubview:view];

8.json转dictionary,dictionary转json

+ (nsstring*)dictionarytojson:(nsdictionary *)dic {

 nserror *parseerror = nil;

 nsdata *jsondata = [nsjsonserialization datawithjsonobject:dic options:nsjsonwritingprettyprinted error:&parseerror];

 return [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding];

}
+(nsdictionary *)jsontodic:(nsstring*)jsonstr {
  nsdata *jsondata = [jsonstr datausingencoding:nsutf8stringencoding];
  nserror *err;
  nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:jsondata
               options:nsjsonreadingmutablecontainers
                error:&err];
  return dic;
}

9.是否允许推送

+(bool)isallowednotification{

 if ([[uidevice currentdevice].systemversion floatvalue] >= 8.0) {

  uiusernotificationsettings *setting = [[uiapplication sharedapplication] currentusernotificationsettings];

  if(uiusernotificationtypenone != setting.types) {

   return yes;
  }
 }
 nslog(@"不允许推送");
 return no;
}

10.磁盘空间相关

+ (nsstring *)memoryformatter:(long long)diskspace {
 nsstring *formatted;
 double bytes = 1.0 * diskspace;
 double megabytes = bytes / mb;
 double gigabytes = bytes / gb;
 if (gigabytes >= 1.0)
  formatted = [nsstring stringwithformat:@"%.2f gb", gigabytes];
 else if (megabytes >= 1.0)
  formatted = [nsstring stringwithformat:@"%.2f mb", megabytes];
 else
  formatted = [nsstring stringwithformat:@"%.2f bytes", bytes];
 nslog(@"fotmatted=%@",formatted);

 return formatted;
}

+ (nsstring *)totaldiskspace {

 long long space = [[[[nsfilemanager defaultmanager] attributesoffilesystemforpath:nshomedirectory() error:nil] objectforkey:nsfilesystemsize] longlongvalue];
 return [self memoryformatter:space];
}

+ (nsstring *)freediskspace {

 long long freespace = [[[[nsfilemanager defaultmanager] attributesoffilesystemforpath:nshomedirectory() error:nil] objectforkey:nsfilesystemfreesize] longlongvalue];
 return [self memoryformatter:freespace];
}

11.修改了leftbarbuttonitem如何恢复系统侧滑返回功能

 //设置代理
self.interactivepopgesturerecognizer.delegate = self;
#pragma mark - <uigesturerecognizerdelegate>
//实现代理方法:return yes :手势有效, no :手势无效
- (bool)gesturerecognizershouldbegin:(uigesturerecognizer *)gesturerecognizer
{
 //当导航控制器的子控制器个数 大于1 手势才有效
 return self.childviewcontrollers.count > 1;
}

或者用第三方 uinavigationcontroller+fdfullscreenpopgesture

12.使用uiappearance在某个状态下设置颜色,字体等不好使

只需要在对应的位置用layoutifneeded刷新一下就可以了

13.设置圆形图片

/** 设置圆形图片(放到分类中使用) */
- (uiimage *)cutcircleimage {
 uigraphicsbeginimagecontextwithoptions(self.size, no, 0.0);
 // 获取上下文
 cgcontextref ctr = uigraphicsgetcurrentcontext();
 // 设置圆形
 cgrect rect = cgrectmake(0, 0, self.size.width, self.size.height);
 cgcontextaddellipseinrect(ctr, rect);
 // 裁剪
 cgcontextclip(ctr);
 // 将图片画上去
 [self drawinrect:rect];
 uiimage *image = uigraphicsgetimagefromcurrentimagecontext();
 uigraphicsendimagecontext();
 return image;
}

14.如果在xib中有一个控件, 已经明确设置尺寸了,输出的frame也是对的, 但是显示出来的效果不一样(比如尺寸变大了), 如果是这种情况一般就是autoresizingmask自动伸缩属性在搞鬼!

解决办法如下:

//xib的awakefromnib方法中设置uiviewautoresizingnone进行清空

 - (void)awakefromnib {
 self.autoresizingmask = uiviewautoresizingnone;
}

15.通过图片data数据第一个字节 来获取图片扩展名

- (nsstring *)contenttypeforimagedata:(nsdata *)data {
 uint8_t c;
 [data getbytes:&c length:1];
 switch (c) {
  case 0xff:
   return @"jpeg";
  case 0x89:
   return @"png";  
  case 0x47:
   return @"gif";  
  case 0x49: 
  case 0x4d:
   return @"tiff";  
  case 0x52: 
   if ([data length] < 12) {
    return nil;
   }
   nsstring *teststring = [[nsstring alloc] initwithdata:[data subdatawithrange:nsmakerange(0, 12)] encoding:nsasciistringencoding];
   if ([teststring hasprefix:@"riff"] && [teststring hassuffix:@"webp"]) {
    return @"webp";
   }
   return nil;
 }
 return nil;
}

16.用0补全的方法

nsinteger count = 5;
//02代表:如果count不足2位 用0在最前面补全(2代表总输出的个数)
nsstring *string = [nsstring stringwithformat:@"%02zd",count];
//输出结果是: 05
nslog(@"%@", string);

总结

以上就是这篇文章的全部内容,希望本文中的这些小技巧能给大家开发ios的时候提供一定的帮助,如果有疑问大家可以留言交流。

上一篇:

下一篇: