iOS小结

上传图片


iOS小结_第1张图片
图片发自App
gif图片的显示
NSString *filePath = [[NSBundle    mainBundle] pathForResource:@"railway" ofType:@"gif"];
NSData *gif = [NSData     dataWithContentsOfFile:filePath];

UIWebView *webViewBG = [[UIWebView alloc] initWithFrame:self.view.frame];

[webViewBG loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];

webViewBG.userInteractionEnabled = NO;

[self.view addSubview:webViewBG];

//后台播放音频设置

    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];

//让app支持接受远程控制事件

//设置app支持接受远程控制事件,其实就是在dock中可以显示应用程序图标,同时点击该图片时,打开app

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

TextField 编辑过程中的回调方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;{
if ([string isEqualToString:@"\\\\\\\\\\\\\\\\n"]){

return YES;

}

NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];

if (self.myTextField == textField)

{

if ([toBeString length] > 11) {

textField.text = [toBeString      substringToIndex:11];

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@"超过最大字数不能输入了" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] autorelease];

[alert show];

return NO;

}

}

return YES;

}

设置UILabel 和UITextField的行间距
// 设置行间距

- (void)setLineSpacing:(CGFloat)spacing label:(UILabel *)label{

NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];

NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];

[paragraphStyle setLineSpacing:spacing];

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [label.text length])];

[label setAttributedText:attributedString];

[label sizeToFit];
}
#define UILABEL_LINE_SPACE 6
#define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height
//给UILabel设置行间距和字间距
-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStylealloc] init];
    paraStyle.lineBreakMode =NSLineBreakByCharWrapping;
    paraStyle.alignment =NSTextAlignmentLeft;
    paraStyle.lineSpacing = UILABEL_LINE_SPACE; //设置行间距
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent =0.0;
    paraStyle.paragraphSpacingBefore =0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    //设置字间距 NSKernAttributeName:@1.5f
    NSDictionary *dic =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@1.5f
};
    
    NSAttributedString *attributeStr = [[NSAttributedStringalloc] initWithString:strattributes:dic];
    label.attributedText = attributeStr;
}

//计算UILabel的高度(带有行间距的情况)
-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStylealloc] init];
    paraStyle.lineBreakMode =NSLineBreakByCharWrapping;
    paraStyle.alignment =NSTextAlignmentLeft;
    paraStyle.lineSpacing = UILABEL_LINE_SPACE;
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent =0.0;
    paraStyle.paragraphSpacingBefore =0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    NSDictionary *dic =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@1.5f
};
    
    CGSize size = [strboundingRectWithSize:CGSizeMake(width,HEIGHT) options:NSStringDrawingUsesLineFragmentOriginattributes:dic context:nil].size;
    return size.height;
}

你可能感兴趣的:(iOS小结)