ios代码小技巧

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 [email protected]{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;
}

11.

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

时间: 2024-09-30 14:51:34

ios代码小技巧的相关文章

ios开发小技巧-用宏化简代码

在IOS开发中,要做字典转模型一般情况如下: 1 /** 2 * 声明方法 3 */ 4 - (instancetype) initWithDictionary:(NSDictionary *)dict; 5 + (instancetype) carWithDictionary:(NSDictionary *)dict; 6 7 /** 8 * 实现方法 9 */ 10 - (instancetype)initWithDictionary:(NSDictionary *)dict 11 { 12

27个iOS开发小技巧

<span style="word-wrap: normal; word-break: normal; line-height: 1.5em; font-size: 14px; outline: none; color: rgb(51, 51, 51); font-family: 'Helvetica neue', Helvetica, sTheiti, 微软雅黑, 黑体, Arial, Tahoma, sans-serif, serif;"><span style=

iOS开发小技巧之--WeakSelf宏的进化

我们都知道在防止如block的循环引用时,会使用__weak关键字做如下定义: __weak typeof(self) weakSelf = self; 后来,为了方便,不用每次都要写这样一句固定代码,我们定义了宏: #define WeakSelf __weak typeof(self) weakSelf = self; 之后,我们可以比较方便的在需要的地方: WeakSelf; ... [weakSelf doSomething]; 再后来,我们发现不止self需要使用weak,可能有部分变

iOS开发小技巧-修改SliderBar指针的样式(牢记这个方法,只能通过代码来修改)

代码: // 修改进度条的指针图片 [self.progressSlider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal]; 原来的样子: 现在的样子:

iOS开发小技巧--学会包装控件(有些view的位置由于代码或系统原因,位置或者尺寸不容易修改或者容易受外界影响)

一.百思项目中遇到了两处这样的问题, 第一处 - 是评论界面的headerView,由于直接把自己搞的xib加载了放在了那里,xib中setFrame写了好多-=  +=,每次滚动的时候,会频繁调用xib中的setFrame方法,导致了最后xib消失或者越来越大.下图是,headerView高度被挡了,怎么设置也不管用.包装后完美解决. 解决办法: 第二处 - tableView中的header文本调整左间距,如图:                                        

iOS:小技巧

记录下一些不常用技巧,以防忘记,复制用. 1.获取当前的View在Window的frame: 1 2 UIWindow * window=[[[UIApplication sharedApplication] delegate] window];   CGRect rect=[_myButton convertRect:_myButton.bounds toView:window]; 2.UIImageView 和UILabel 等一些控件,需要加这句才能setCorn 1 _myLabel.l

【解决方法】iOS 开发小技巧

1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [[self.searchBar.subviews objectAtIndex:0] removeFromSuperview]; 2,NSDate: [java] view plaincopy 字母  日期或时间元素    表示     示例 G     Era   标志符     Text     

【转】IOS开发小技巧

1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [[self.searchBar.subviews objectAtIndex:0] removeFromSuperview]; 2,NSDate: [java] view plaincopy 字母  日期或时间元素    表示     示例 G     Era   标志符     Text     

IOS开发小技巧

1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [[self.searchBar.subviews objectAtIndex:0] removeFromSuperview]; 2,NSDate: [java] view plaincopy 字母  日期或时间元素    表示     示例 -)     Number -)      Number