iOS often has small functions (get screen image compress picture add border adjust size of label)

  • 2021-12-12 10:05:05
  • OfStack

ABSTRACT: Get screen image, label dynamic size, time stamp into time, RGB into color, add border, compress picture, textfield placeholder, picture do gray processing

1. Get the screen image


- (UIImage *)imageFromView: (UIView *) theView
{
  UIGraphicsBeginImageContext(theView.frame.size);
  CGContextRef context = UIGraphicsGetCurrentContext();
  [theView.layer renderInContext:context];
  UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return theImage;
}

2. Dynamic size of label


- (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. Convert timestamp to time


-(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 to Color


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

Step 5 Add a border


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.//Compress pictures


+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
  // Create 1 Graphic context image 
  UIGraphicsBeginImageContext(newSize);
  //  Tell old pictures to be painted in this new environment , Required 
  // new size
  [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  // Get a new image of the context 
  UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  //  End context 
  UIGraphicsEndImageContext();
  return newImage;
}

7. placeholder of textfield


[textF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[textF setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];

8. Layout


butLeft. imageEdgeInsets = UIEdgeInsetsMake (7 , 5 , 7 , 25 );
butLeft.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

9.//Call this method to change the last 2 characters of label


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

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

11. The picture goes gray


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

13.16 to rgb


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

Related articles: