iOS自己使用的一些小方法

weixin_34232744發表於2016-12-28

label的高度確定 計算label所佔的寬度

CGRect temp = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 21) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15.0]} context:nil];

NSLog(@"寬度 : %f",temp.size.width);

圖片按比例縮放

UIImageView *imgView = [[UIImageView alloc] init];

imgView.image = [UIImage imageNamed:@"流程圖1.png"];

CGFloat scale = imgView.image.size.width / (SCREENW / 2);

CGFloat imgViewHeight = imgView.image.size.height / scale;

imgView.frame = CGRectMake(0, 0, SCREENW / 2, imgViewHeight);

imgView.image = [UIImage imageWithCGImage:imgView.image.CGImage scale:scale orientation:UIImageOrientationUp];

通過顏色製作圖片 我是用在導航欄上了

///  通過顏色製作圖片

- (UIImage *)imageWithFrame:(CGRect)frame color:(UIColor *)color alphe:(CGFloat)alphe {

frame = CGRectMake(0, 0, frame.size.width, frame.size.height);

UIColor *redColor = color;

UIGraphicsBeginImageContext(frame.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetAlpha(context, alphe);

CGContextSetFillColorWithColor(context, [redColor CGColor]);

CGContextFillRect(context, frame);

UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return theImage;

}

按鈕的圖片和文字位置的調整 圖片在上 文字在下

-(void)initWithButton:(UIButton*)btn

{

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使圖片和文字水平居中顯示

[btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.height + 10,-btn.imageView.width, 0.0,0.0)];

[btn setImageEdgeInsets:UIEdgeInsetsMake(-btn.titleLabel.height - 10, 0.0,0.0, -btn.titleLabel.bounds.size.width)];

}

相關文章