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

 更新时间:2017年03月29日 14:30:31   作者:BearsG  
本文主要介绍了iOS常用小功能:获得屏幕图像,label的动态size,时间戳转化为时间,RGB转化成颜色,加边框,压缩图片,textfield的placeholder,图片做灰度处理的方法。下面跟着小编一起来看下吧

摘要:获得屏幕图像,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]

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

相关文章

  • Dispatch Source Timer的使用及注意事项介绍

    Dispatch Source Timer的使用及注意事项介绍

    这篇文章主要给大家介绍了关于Dispatch Source Timer使用和一些注意事项的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习下吧。
    2017-09-09
  • 详解ios中scrollView上使用masonry

    详解ios中scrollView上使用masonry

    本篇文章主要给大家详细分析了ios开发中scrollView上使用masonry的详细知识内容,需要的朋友参考下吧。
    2018-02-02
  • iOS仿微信摇一摇动画效果加震动音效实例

    iOS仿微信摇一摇动画效果加震动音效实例

    这篇文章主要介绍了iOS仿微信摇一摇动画效果加震动音效实例,详细介绍了微信摇一摇功能的实现原理,非常具有实用价值,需要的朋友可以参考下。
    2017-03-03
  • iOS 9无法访问HTTP的解决方法

    iOS 9无法访问HTTP的解决方法

    这篇文章主要为大家详细介绍了iOS 9无法访问HTTP的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Objective-C优雅使用KVO观察属性值变化

    Objective-C优雅使用KVO观察属性值变化

    这篇文章主要为大家介绍了Objective-C优雅使用KVO观察属性值变化示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • iOS中多网络请求的线程安全详解

    iOS中多网络请求的线程安全详解

    这篇文章主要给大家介绍了关于iOS中多网络请求的线程安全的相关资料文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-10-10
  • IOS开发之路--C语言构造类型

    IOS开发之路--C语言构造类型

    在第一节中我们就提到C语言的构造类型,分为:数组、结构体、枚举、共用体,当然前面数组的内容已经说了很多了,这一节将会重点说一下其他三种类型。
    2014-08-08
  • iOS 简单的操作杆旋转实现示例详解

    iOS 简单的操作杆旋转实现示例详解

    这篇文章主要为大家介绍了iOS 简单的操作杆旋转实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • iOS中的音频服务和音频AVAudioPlayer音频播放器使用指南

    iOS中的音频服务和音频AVAudioPlayer音频播放器使用指南

    这里我们要介绍的是AVAudio ToolBox框架中的AudioServicesPlaySystemSound函数创建的服务,特别适合用来制作铃声,下面就简单整理一下iOS中的音频服务和音频AVAudioPlayer音频播放器使用指南:
    2016-06-06
  • iOS多线程开发——NSThread浅析

    iOS多线程开发——NSThread浅析

    这篇文章主要介绍了 iOS多线程开发——NSThread浅析,需要的朋友可以参考下
    2016-05-05

最新评论