iOS常用小功能(獲得屏幕圖像、壓縮圖片、加邊框、調(diào)整label的size)
摘要:獲得屏幕圖像,label的動(dòng)態(tài)size,時(shí)間戳轉(zhuǎn)化為時(shí)間,RGB轉(zhuǎn)化成顏色,加邊框,壓縮圖片,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的動(dòng)態(tài)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.時(shí)間戳轉(zhuǎn)化為時(shí)間
-(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轉(zhuǎn)化成顏色
+ (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
{
//創(chuàng)建一個(gè)圖形上下文形象
UIGraphicsBeginImageContext(newSize);
// 告訴舊圖片畫在這個(gè)新的環(huán)境,所需的
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
//獲取上下文的新形象
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束上下文
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.//調(diào)用此方法改變label最后2個(gè)字符的大小
- (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進(jìn)制轉(zhuǎn)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]
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
iOS逆向工程使用dumpdecrypted工具給App脫殼
這篇文章主要介紹了iOS逆向工程使用dumpdecrypted工具給App脫殼的相關(guān)資料,本文圖文并茂給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
iOS 頁面滑動(dòng)與標(biāo)題切換顏色漸變的聯(lián)動(dòng)效果實(shí)例
本篇文章主要介紹了iOS 頁面滑動(dòng)與標(biāo)題切換顏色漸變的聯(lián)動(dòng)效果實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04
Objective-C的NSOperation多線程類基本使用指南
這篇文章主要介紹了Objective-C的NSOperation多線程類基本使用指南,談到了Operations的執(zhí)行順序和并發(fā)量等設(shè)置操作,需要的朋友可以參考下2016-02-02
如何使用IOS自動(dòng)化測試工具UIAutomation
這篇文章主要介紹了UIAutomation使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值2021-04-04
iOS實(shí)現(xiàn)萌貨貓頭鷹登錄界面動(dòng)畫
本文介紹的動(dòng)畫效果仿自國外網(wǎng)站readme.io的登錄界面,超萌可愛的貓頭鷹,感興趣的朋友們可以參考學(xué)習(xí)。2016-08-08
iOS中實(shí)現(xiàn)imageView任意角度旋轉(zhuǎn)的方法
這篇文章主要給大家介紹了關(guān)于iOS中實(shí)現(xiàn)imageView任意角度旋轉(zhuǎn)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
iOS評(píng)分(評(píng)價(jià))星星圖打分功能
這篇文章主要介紹了iOS評(píng)分(評(píng)價(jià))星星圖打分功能,評(píng)分視圖分為展示和評(píng)分兩種,具體詳情大家可以通過本文詳細(xì)學(xué)習(xí)2016-11-11

