iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)
摘要:获得屏幕图像,label的动态size,时间戳转化为时间,RGB转化成颜色,加边框,压缩图片,textfield的placeholder,图片做灰度处理
1.获得屏幕图像
-(UIImage*)imageFromView:(UIView*)theView
{
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRefcontext=UIGraphicsGetCurrentContext();
[theView.layerrenderInContext:context];
UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returntheImage;
}
2.label的动态size
-(CGSize)labelAutoCalculateRectWith:(NSString*)textFontSize:(CGFloat)fontSizeMaxSize:(CGSize)maxSize
{
NSMutableParagraphStyle*paragraphStyle=[[NSMutableParagraphStylealloc]init];paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
NSDictionary*attributes=@{NSFontAttributeName:[UIFontfontWithName:@"MicrosoftYaHei"size:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSizelabelSize=[textboundingRectWithSize:maxSizeoptions:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLineattributes:attributescontext:nil].size;
labelSize.height=ceil(labelSize.height);
returnlabelSize;
}
3.时间戳转化为时间
-(NSString*)TimeTrasformWithDate:(NSString*)dateString
{
NSDateFormatter*formatter=[[NSDateFormatteralloc]init];
[formattersetDateFormat:@"YY-MM-ddHH:mm"];
[formattersetTimeZone:[NSTimeZonetimeZoneWithName:@"Asia/Beijing"]];
NSString*date=[formatterstringFromDate:[NSDatedateWithTimeIntervalSince1970:dateString.integerValue]];
//NSLog(@"date1:%@",date);
returndate;
}
4.RGB转化成颜色
+(UIColor*)colorFromHexRGB:(NSString*)inColorString
{
UIColor*result=nil;
unsignedintcolorCode=0;
unsignedcharredByte,greenByte,blueByte;
if(nil!=inColorString)
{
NSScanner*scanner=[NSScannerscannerWithString:inColorString];
(void)[scannerscanHexInt:&colorCode];//ignoreerror
}
redByte=(unsignedchar)(colorCode>>16);
greenByte=(unsignedchar)(colorCode>>8);
blueByte=(unsignedchar)(colorCode);//masksoffhighbits
result=[UIColor
colorWithRed:(float)redByte/0xff
green:(float)greenByte/0xff
blue:(float)blueByte/0xff
alpha:1.0];
returnresult;
}
5.加边框
UIRectCornercorners=UIRectCornerTopLeft|UIRectCornerTopRight; UIBezierPath*maskPath=[UIBezierPathbezierPathWithRoundedRect:view.boundsbyRoundingCorners:corners cornerRadii:CGSizeMake(4,0)]; CAShapeLayer*maskLayer=[CAShapeLayerlayer]; maskLayer.frame=view.bounds; maskLayer.path=maskPath.CGPath; view.layer.mask=maskLayer;
6.//压缩图片
+(UIImage*)imageWithImageSimple:(UIImage*)imagescaledToSize:(CGSize)newSize
{
//创建一个图形上下文形象
UIGraphicsBeginImageContext(newSize);
//告诉旧图片画在这个新的环境,所需的
//newsize
[imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
//获取上下文的新形象
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
//结束上下文
UIGraphicsEndImageContext();
returnnewImage;
}
7.textfield的placeholder
[textFsetValue:[UIColorwhiteColor]forKeyPath:@"_placeholderLabel.textColor"]; [textFsetValue:[UIFontboldSystemFontOfSize:15]forKeyPath:@"_placeholderLabel.font"];
8.布局
butLeft.imageEdgeInsets=UIEdgeInsetsMake(7,5,7,25); butLeft.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
9.//调用此方法改变label最后2个字符的大小
-(void)label:(UILabel*)labelBehindTextSize:(NSInteger)integer
{
NSMutableAttributedString*mutaString=[[NSMutableAttributedStringalloc]initWithString:label.text];
[mutaStringaddAttribute:NSFontAttributeNamevalue:[UIFontboldSystemFontOfSize:16]range:NSMakeRange(label.text.length-2,2)];
label.attributedText=mutaString;
}
10.
-(void)ChangeLabelTextColor:(UILabel*)label
{
NSMutableAttributedString*mutaString=[[NSMutableAttributedStringalloc]initWithString:label.text];
[mutaStringaddAttribute:NSForegroundColorAttributeNamevalue:[UIColorcolorWithRed:207/255.0green:34/255.0blue:42/255.0alpha:1]range:NSMakeRange(0,5)];
label.attributedText=mutaString;
}
if([tableViewrespondsToSelector:@selector(setSeparatorInset:)]){
[tableViewsetSeparatorInset:UIEdgeInsetsZero];
}
if([[UIDevicecurrentDevice].systemVersionfloatValue]>=8.0){
if([tableViewrespondsToSelector:@selector(setLayoutMargins:)]){
[tableViewsetLayoutMargins:UIEdgeInsetsZero];
}
}
//Doanyadditionalsetupafterloadingtheview.
}
-(void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath
{
if([cellrespondsToSelector:@selector(setSeparatorInset:)]){
[cellsetSeparatorInset:UIEdgeInsetsZero];
}
if([[UIDevicecurrentDevice].systemVersionfloatValue]>=8.0){
if([cellrespondsToSelector:@selector(setLayoutMargins:)]){
[cellsetLayoutMargins:UIEdgeInsetsZero];
}
}
}
11.图片变灰度
-(UIImage*)grayscaleImage:(UIImage*)image
{
CGSizesize=image.size;
CGRectrect=CGRectMake(0.0f,0.0f,image.size.width,
image.size.height);
//Createamono/graycolorspace
CGColorSpaceRefcolorSpace=CGColorSpaceCreateDeviceGray();
CGContextRefcontext=CGBitmapContextCreate(nil,size.width,
size.height,8,0,colorSpace,kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
//Drawtheimageintothegrayscalecontext
CGContextDrawImage(context,rect,[imageCGImage]);
CGImageRefgrayscale=CGBitmapContextCreateImage(context);
CGContextRelease(context);
//Recovertheimage
UIImage*img=[UIImageimageWithCGImage:grayscale];
CFRelease(grayscale);
returnimg;
}
13.16进制转rgb
#defineUIColorFromRGB(rgbValue)[UIColorcolorWithRed:((float)((rgbValue&0xFF0000)>>16))/255.0green:((float)((rgbValue&0xFF00)>>8))/255.0blue:((float)(rgbValue&0xFF))/255.0alpha:1.0]
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!