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

iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)

程序员文章站 2024-02-13 19:28:28
摘要:获得屏幕图像,label的动态size,时间戳转化为时间,rgb转化成颜色,加边框,压缩图片,textfield的placeholder,图片做灰度处理 1.获得屏...

摘要:获得屏幕图像,label的动态size,时间戳转化为时间,rgb转化成颜色,加边框,压缩图片,textfield的placeholder,图片做灰度处理

1.获得屏幕图像

- (uiimage *)imagefromview: (uiview *) theview
{
  uigraphicsbeginimagecontext(theview.frame.size);
  cgcontextref context = uigraphicsgetcurrentcontext();
  [theview.layer renderincontext:context];
  uiimage *theimage = uigraphicsgetimagefromcurrentimagecontext();
  uigraphicsendimagecontext();
  return theimage;
}

2.label的动态size

- (cgsize)labelautocalculaterectwith:(nsstring*)text fontsize:(cgfloat)fontsize maxsize:(cgsize)maxsize
{
  nsmutableparagraphstyle* paragraphstyle = [[nsmutableparagraphstyle alloc]init]; paragraphstyle.linebreakmode=nslinebreakbywordwrapping;
  nsdictionary* attributes =@{nsfontattributename:[uifont fontwithname:@"microsoftyahei" size:fontsize],nsparagraphstyleattributename:paragraphstyle.copy};
  cgsize labelsize = [text boundingrectwithsize:maxsize options:nsstringdrawinguseslinefragmentorigin|nsstringdrawingusesfontleading|nsstringdrawingtruncateslastvisibleline attributes:attributes context:nil].size;
  labelsize.height=ceil(labelsize.height);
  return labelsize;
}

3.时间戳转化为时间

-(nsstring*)timetrasformwithdate:(nsstring *)datestring
{
  nsdateformatter *formatter = [[nsdateformatter alloc]init];
  [formatter setdateformat:@"yy-mm-dd hh:mm"];
  [formatter settimezone:[nstimezone timezonewithname:@"asia/beijing"]];

  nsstring *date = [formatter stringfromdate:[nsdate datewithtimeintervalsince1970:datestring.integervalue]];
  //nslog(@"date1:%@",date);
  return date;
}

4.rgb转化成颜色

+ (uicolor *)colorfromhexrgb:(nsstring *)incolorstring
{
  uicolor *result = nil;
  unsigned int colorcode = 0;
  unsigned char redbyte, greenbyte, bluebyte;
  if (nil != incolorstring)
  {
    nsscanner *scanner = [nsscanner scannerwithstring:incolorstring];
    (void) [scanner scanhexint:&colorcode]; // ignore error
  }
  redbyte = (unsigned char) (colorcode >> 16);
  greenbyte = (unsigned char) (colorcode >> 8);
  bluebyte = (unsigned char) (colorcode); // masks off high bits
  result = [uicolor
       colorwithred: (float)redbyte / 0xff
       green: (float)greenbyte/ 0xff
       blue: (float)bluebyte / 0xff
       alpha:1.0];
  return result;
}

5.加边框

uirectcorner corners=uirectcornertopleft | uirectcornertopright;
  uibezierpath *maskpath = [uibezierpath bezierpathwithroundedrect:view.bounds        byroundingcorners:corners
 cornerradii:cgsizemake(4, 0)];
cashapelayer *masklayer = [cashapelayer layer];
masklayer.frame     = view.bounds;
masklayer.path     = maskpath.cgpath;
view.layer.mask     = masklayer;

6.//压缩图片

+ (uiimage*)imagewithimagesimple:(uiimage*)image scaledtosize:(cgsize)newsize
{
  //创建一个图形上下文形象
  uigraphicsbeginimagecontext(newsize);
  // 告诉旧图片画在这个新的环境,所需的
  // new size
  [image drawinrect:cgrectmake(0,0,newsize.width,newsize.height)];
  //获取上下文的新形象
  uiimage* newimage = uigraphicsgetimagefromcurrentimagecontext();
  // 结束上下文
  uigraphicsendimagecontext();
  return newimage;
}

7.textfield的placeholder

[textf setvalue:[uicolor whitecolor] forkeypath:@"_placeholderlabel.textcolor"];
[textf setvalue:[uifont boldsystemfontofsize:15] forkeypath:@"_placeholderlabel.font"];

8.布局

butleft. imageedgeinsets = uiedgeinsetsmake (7 , 5 , 7 , 25 );
butleft.contenthorizontalalignment = uicontrolcontenthorizontalalignmentcenter;

9.//调用此方法改变label最后2个字符的大小

- (void)label:(uilabel *)label behindtextsize:(nsinteger)integer
{
  nsmutableattributedstring *mutastring = [[nsmutableattributedstring alloc] initwithstring:label.text];

  [mutastring addattribute:nsfontattributename value:[uifont boldsystemfontofsize:16] range:nsmakerange(label.text.length-2, 2)];
  label.attributedtext = mutastring;
}

10.

- (void)changelabeltextcolor:(uilabel *)label
{
  nsmutableattributedstring *mutastring = [[nsmutableattributedstring alloc] initwithstring:label.text];
  [mutastring addattribute:nsforegroundcolorattributename value:[uicolor colorwithred:207/255.0 green:34/255.0 blue:42/255.0 alpha:1] range:nsmakerange(0, 5)];
  label.attributedtext = mutastring;
}
if ([tableview respondstoselector:@selector(setseparatorinset:)]) {
    [tableview setseparatorinset:uiedgeinsetszero];

  }
  if ([[uidevice currentdevice].systemversion floatvalue] >= 8.0) {
    if ([tableview respondstoselector:@selector(setlayoutmargins:)]) {
    [tableview setlayoutmargins:uiedgeinsetszero];
  }
  }
  // do any additional setup after loading the view.
}
- (void)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath
{
  if ([cell respondstoselector:@selector(setseparatorinset:)]) {
    [cell setseparatorinset:uiedgeinsetszero];
  }
  if ([[uidevice currentdevice].systemversion floatvalue] >= 8.0) {
    if ([cell respondstoselector:@selector(setlayoutmargins:)]) {
    [cell setlayoutmargins:uiedgeinsetszero];
  }
  }  
}

11.图片变灰度

-(uiimage *) grayscaleimage: (uiimage *) image
{
  cgsize size = image.size;
  cgrect rect = cgrectmake(0.0f, 0.0f, image.size.width,
               image.size.height);
  // create a mono/gray color space
  cgcolorspaceref colorspace = cgcolorspacecreatedevicegray();
  cgcontextref context = cgbitmapcontextcreate(nil, size.width,
                         size.height, 8, 0, colorspace, kcgimagealphanone);
  cgcolorspacerelease(colorspace);
  // draw the image into the grayscale context
  cgcontextdrawimage(context, rect, [image cgimage]);
  cgimageref grayscale = cgbitmapcontextcreateimage(context);
  cgcontextrelease(context);
  // recover the image
  uiimage *img = [uiimage imagewithcgimage:grayscale];
  cfrelease(grayscale);
  return img;
}

13.16进制转rgb

#define uicolorfromrgb(rgbvalue) [uicolor colorwithred:((float)((rgbvalue & 0xff0000) >> 16))/255.0 green:((float)((rgbvalue & 0xff00) >> 8))/255.0 blue:((float)(rgbvalue & 0xff))/255.0 alpha:1.0]

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!