代码拉伸
UIImage *image = [UIImage imageNamed:@"img"];
// 设置端盖的值
CGFloat top = image.size.height * 0.5;
CGFloat left = image.size.width * 0.5;
CGFloat bottom = image.size.height * 0.5;
CGFloat right = image.size.width * 0.5;
// 设置端盖的值
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
// 设置拉伸的模式
UIImageResizingMode mode = UIImageResizingModeStretch;
// 拉伸图片
UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode];
// 设置按钮的背景图片
self.imageView.image = newImage;
图片加代码
1.设置图片拉伸区域
WeChat75400313a4005eaec7657510a80fbd03.png
WeChat0c5430d152950909e9f945054a08398a.png
2.代码设置图片大小
UIImage *image = [UIImage imageNamed:@"img"];
// 设置按钮的背景图片
self.imageView.image = image;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//最大宽度为300,最大高度为200
CGSize size = [itemModel.name boundingRectWithSize:CGSizeMake(ScreenW - px_scale(40), 200) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18] ,NSParagraphStyleAttributeName:paragraphStyle} context:nil].size;
[self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.contentView.mas_top).offset(px_scale(66));
make.centerX.mas_equalTo(self.contentView.mas_centerX).offset(0);
make.width.mas_equalTo(@(size.width + px_scale(120)));
make.height.equalTo(@(px_scale(68)));
}];










网友评论