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

2020-01-18 22:14:13王旭
+ (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;
}