iOS开发小技巧总汇(不定时增添)

一、点击屏幕得时候隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[Touches anyObject];
    if (touch.tapCount >=1) {
        [chi resignFirstResponder];
        [meter resignFirstResponder];
        [feet resignFirstResponder];
    }

}


二、UILabel的行间距

UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];

        [label setBackgroundColor:[UIColor clearColor]];

        [label setTextColor:textColor];

        [label setNumberOfLines:0];

        label.font = textFont;

        NSString *labelText = @"我是内容,我是内容,我是内容,我是内容,我是内容。";
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:LINESPACE];//调整行间距 
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
        label.attributedText = attributedString;
        [contentView addSubview:label];

        [label sizeToFit];


三、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;   
}


四、判断设备


1、设备名称

return [UIDevice currentDevice].name;

2、设备型号,只可得到是何设备,无法得到是第几代设备

return [UIDevice currentDevice].model;

3、系统版本型号,iPhone OS

return [UIDevice currentDevice].systemVersion;

4、系统版本名称,如6.1.3

return [UIDevice currentDevice].systemName;


5、判断是否为iPhone

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

6、判断是否为iPad

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

7、判断是否为ipod

#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])

8、判断是否为iPhone5

#define IS_IPHONE_5_SCREEN [[UIScreen mainScreen] bounds].size.height >= 568.0f && [[UIScreen mainScreen] bounds].size.height < 1024.0f


<span style="font-size:14px;">- (void)viewWillAppear:(BOOL)animated</span><span style="font-size:12px;">
{</span>
<span style="white-space: pre;"><span style="font-size:12px;"><span style="color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px; font-family: Menlo;">1、</span><span style="color: rgb(66, 176, 92); font-family: 'Heiti SC Light'; letter-spacing: 0.5px; line-height: 22.5px;">设备名称</span></span></span><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#333333;">   NSLog(@"%@",[UIDevice currentDevice].name);</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#333333;"><span style="font-family: Menlo; color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px;">2、</span><span style="color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px; font-family: 'Heiti SC Light';">设备型号,只可得到是何设备,无法得到是第几代设备</span>
</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#333333;"><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">   NSLog</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">(</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">@"%@"</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">,</span><span style="color: rgb(0, 204, 204); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">[UIDevice currentDevice].model</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);"></span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">);</span>
</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#333333;"><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);"><span style="font-family: Menlo; color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px;">3、</span><span style="color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px; font-family: 'Heiti SC Light';">系统版本型号</span><span style="font-family: Menlo; color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px;">,</span><span style="color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px; font-family: 'Heiti SC Light';">如</span><span style="font-family: Menlo; color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px;">iPhone OS</span>
</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#333333;"><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);"><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">   NSLog</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">(</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">@"%@"</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">,</span><span style="color: rgb(0, 204, 204); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">[UIDevice currentDevice].systemVersion</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);"></span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">);</span>
</span></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#333333;"><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);"><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);"><span style="color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px; font-family: Menlo;">4、</span><span style="color: rgb(66, 176, 92); font-family: 'Heiti SC Light'; letter-spacing: 0.5px; line-height: 22.5px;">系统版本名称,如</span><span style="color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px; font-family: Menlo;">6.1.3</span>
</span></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#333333;"><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);"><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);"><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">   NSLog</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">(</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">@"%@"</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">,</span><span style="color: rgb(0, 204, 204); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">[UIDevice currentDevice].systemName</span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);"></span><span style="color: rgb(51, 51, 51); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px; white-space: pre; background-color: rgb(240, 240, 240);">);</span>
</span></span></span></p>
<span style="font-size:12px;">
</span>
<span style="font-size:12px;"><span style="color: rgb(66, 176, 92); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">5、</span><span style="color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px; font-family: 'Heiti SC Light';">判断是否为</span><span style="color: rgb(66, 176, 92); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">iPhone</span>
    NSLog(@"%d",(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone));</span>
<span style="font-size:12px;"><span style="color: rgb(66, 176, 92); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">6、</span><span style="color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px; font-family: 'Heiti SC Light';">判断是否为</span><span style="color: rgb(66, 176, 92); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">iPad</span>
</span><pre name="code" class="objc" style="letter-spacing: 0.5px; line-height: 22.5px;"><span style="font-size:12px;">    NSLog(@"%d",<span style="font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;"><span style="color:#666666;">(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)</span></span>);</span>
<span style="font-size:12px;"><span style="color: rgb(66, 176, 92); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">7、</span><span style="color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px; font-family: 'Heiti SC Light';">判断是否为</span><span style="color: rgb(66, 176, 92); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">ipod</span>
</span>
<pre name="code" class="objc" style="letter-spacing: 0.5px; line-height: 22.5px;"><span style="font-size:12px;">    NSLog(@"%d",<span style="color:#333333;"><span style="font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">([[[UIDevice currentDevice] model] isEqualToString:@</span><span style="font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">"iPod touch"</span><span style="font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">])</span></span>);</span>
<span style="font-size:12px;"><span style="color: rgb(66, 176, 92); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">8、</span><span style="color: rgb(66, 176, 92); letter-spacing: 0.5px; line-height: 22.5px; font-family: 'Heiti SC Light';">判断是否为</span><span style="color: rgb(66, 176, 92); font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">iPhone5</span>
</span>
 
<pre name="code" class="objc" style="letter-spacing: 0.5px; line-height: 22.5px;"><span style="font-size:12px;">    NSLog(@"%d",<span style="color:#333333;"><span style="font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">[[UIScreen mainScreen] bounds].size.height >= </span><span style="font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">568.0f</span><span style="font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;"> && [[UIScreen mainScreen] bounds].size.height < </span><span style="font-family: Menlo; letter-spacing: 0.5px; line-height: 22.5px;">1024.0f</span></span>);</span>
 
} 
 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值