最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS常用小功能(獲得屏幕圖像、壓縮圖片、加邊框、調(diào)整label的size)

 更新時(shí)間:2017年03月29日 14:30:31   作者:BearsG  
本文主要介紹了iOS常用小功能:獲得屏幕圖像,label的動(dòng)態(tài)size,時(shí)間戳轉(zhuǎn)化為時(shí)間,RGB轉(zhuǎn)化成顏色,加邊框,壓縮圖片,textfield的placeholder,圖片做灰度處理的方法。下面跟著小編一起來(lái)看下吧

摘要:獲得屏幕圖像,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);
  // 告訴舊圖片畫(huà)在這個(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]

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • Dispatch Source Timer的使用及注意事項(xiàng)介紹

    Dispatch Source Timer的使用及注意事項(xiàng)介紹

    這篇文章主要給大家介紹了關(guān)于Dispatch Source Timer使用和一些注意事項(xiàng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)下吧。
    2017-09-09
  • 詳解ios中scrollView上使用masonry

    詳解ios中scrollView上使用masonry

    本篇文章主要給大家詳細(xì)分析了ios開(kāi)發(fā)中scrollView上使用masonry的詳細(xì)知識(shí)內(nèi)容,需要的朋友參考下吧。
    2018-02-02
  • iOS仿微信搖一搖動(dòng)畫(huà)效果加震動(dòng)音效實(shí)例

    iOS仿微信搖一搖動(dòng)畫(huà)效果加震動(dòng)音效實(shí)例

    這篇文章主要介紹了iOS仿微信搖一搖動(dòng)畫(huà)效果加震動(dòng)音效實(shí)例,詳細(xì)介紹了微信搖一搖功能的實(shí)現(xiàn)原理,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-03-03
  • iOS 9無(wú)法訪問(wèn)HTTP的解決方法

    iOS 9無(wú)法訪問(wèn)HTTP的解決方法

    這篇文章主要為大家詳細(xì)介紹了iOS 9無(wú)法訪問(wèn)HTTP的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Objective-C優(yōu)雅使用KVO觀察屬性值變化

    Objective-C優(yōu)雅使用KVO觀察屬性值變化

    這篇文章主要為大家介紹了Objective-C優(yōu)雅使用KVO觀察屬性值變化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • iOS中多網(wǎng)絡(luò)請(qǐng)求的線程安全詳解

    iOS中多網(wǎng)絡(luò)請(qǐng)求的線程安全詳解

    這篇文章主要給大家介紹了關(guān)于iOS中多網(wǎng)絡(luò)請(qǐng)求的線程安全的相關(guān)資料文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • IOS開(kāi)發(fā)之路--C語(yǔ)言構(gòu)造類型

    IOS開(kāi)發(fā)之路--C語(yǔ)言構(gòu)造類型

    在第一節(jié)中我們就提到C語(yǔ)言的構(gòu)造類型,分為:數(shù)組、結(jié)構(gòu)體、枚舉、共用體,當(dāng)然前面數(shù)組的內(nèi)容已經(jīng)說(shuō)了很多了,這一節(jié)將會(huì)重點(diǎn)說(shuō)一下其他三種類型。
    2014-08-08
  • iOS 簡(jiǎn)單的操作桿旋轉(zhuǎn)實(shí)現(xiàn)示例詳解

    iOS 簡(jiǎn)單的操作桿旋轉(zhuǎn)實(shí)現(xiàn)示例詳解

    這篇文章主要為大家介紹了iOS 簡(jiǎn)單的操作桿旋轉(zhuǎn)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • iOS中的音頻服務(wù)和音頻AVAudioPlayer音頻播放器使用指南

    iOS中的音頻服務(wù)和音頻AVAudioPlayer音頻播放器使用指南

    這里我們要介紹的是AVAudio ToolBox框架中的AudioServicesPlaySystemSound函數(shù)創(chuàng)建的服務(wù),特別適合用來(lái)制作鈴聲,下面就簡(jiǎn)單整理一下iOS中的音頻服務(wù)和音頻AVAudioPlayer音頻播放器使用指南:
    2016-06-06
  • iOS多線程開(kāi)發(fā)——NSThread淺析

    iOS多線程開(kāi)發(fā)——NSThread淺析

    這篇文章主要介紹了 iOS多線程開(kāi)發(fā)——NSThread淺析,需要的朋友可以參考下
    2016-05-05

最新評(píng)論

安图县| 渭源县| 福建省| 晋城| 长丰县| 许昌市| 宽城| 阿勒泰市| 灵山县| 浑源县| 西藏| 遂昌县| 邢台县| 淳化县| 赤壁市| 墨竹工卡县| 连江县| 商城县| 阿拉善左旗| 漯河市| 华容县| 恩施市| 庆安县| 龙井市| 丰城市| 阳西县| 定日县| 陵水| 舞钢市| 涡阳县| 澎湖县| 秦安县| 夏河县| 黎平县| 长宁县| 美姑县| 时尚| 道孚县| 峨山| 姚安县| 莱阳市|