iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
来源:互联网 发布:装修论坛 淘宝 编辑:程序博客网 时间:2024/05/01 03:24
摘要:获得屏幕图像,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]
1 0
- iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
- iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
- iOS - 常用到的图片功能:截取当前屏幕/选择本地图片/图片压缩
- iOS 获得指定范围内的屏幕图像(截屏)
- iOS动态计算Label的size
- (图片显示加边框)
- iOS 常用的图片压缩函数
- ios 调整Label上文字的行距
- iOS根据屏幕的宽高等比压缩图片
- iOS 常用小功能
- iOS常用小功能
- iOS开发常用的小功能(持续更新)
- iOS常用小功能的实现
- 一个图片压缩并导入服务器的小功能
- ios开发小知识点--显示label上加删除线
- iOS UI_APPEARANCE_SELECTOR、appearence 和UIButton加图片并调整图片和字体的位置
- IOS 获取图片的size
- 图片加边框 (css)
- 自定义Notification 方法
- 笔记练习:《Javascript入门经典(第5版)》page185_15.10_Practice
- StackView的基本功能与用法(叠在一起的图片)
- 交互设计师的五大误区
- redis结构分析——ziplist
- iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
- popupwindow在不同theme时候的bug
- 秒杀系统架构分析与实战
- JayRock:JSON and JSON_RPC for .Net
- ViewCompat的作用
- 使用Jenkins搭建持续集成服务
- BZOJ 3576: [Hnoi2014]江南乐
- 《统计学习方法》-KNN笔记和python源码
- Eclipse打包时出现export aborted because fatal lint errors