gpt4 book ai didi

ios - UIImage resizableImageWithCapInsets - 这可以使用 Nib 指定吗?

转载 作者:可可西里 更新时间:2023-11-01 05:31:07 24 4
gpt4 key购买 nike

我有一个 UIImage 使用 resizableImageWithCapInsets 完美调整大小来制作包含文本标签的角标(Badge)。我很想知道有没有办法在 Storyboard或使用 Nib 创建的 View 中执行此操作?

没有技术理由要求这样做。大部分 UI 是在 Nib 中完成的,我想尽可能多地保留在那里,而不是加载图像、使其可调整大小、查找文本尺寸、调整 UIImageView 大小,然后在代码中应用标签。

我还不能使这个项目仅限 iOS6(部署目标 ios5+),所以使用布局约束不是一个选项。

最佳答案

是的,有。

诀窍是覆盖 -initWithCoder: 和图像 setter (setImage:forState:setBackgroundImage:forState:)来创建您在 xib 中设置的相同图像的可调整大小版本。显然,它们仍然不会在 xib 本身中正确调整大小,但会在初始化后正确调整大小。不要忘记将 NAResizableButton 设置为 UIButton 的自定义类。

当然胜过以编程方式进行。 :)

接口(interface):

#import <UIKit/UIKit.h>

@interface NAResizableButton : UIButton

@end

实现:

#import "NAResizableButton.h"

@implementation NAResizableButton

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self setImage:[self imageForState:UIControlStateNormal] forState:UIControlStateNormal];
[self setImage:[self imageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
[self setImage:[self imageForState:UIControlStateSelected] forState:UIControlStateSelected];
[self setImage:[self imageForState:UIControlStateDisabled] forState:UIControlStateDisabled];

[self setBackgroundImage:[self backgroundImageForState:UIControlStateNormal] forState:UIControlStateNormal];
[self setBackgroundImage:[self backgroundImageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
[self setBackgroundImage:[self backgroundImageForState:UIControlStateSelected] forState:UIControlStateSelected];
[self setBackgroundImage:[self backgroundImageForState:UIControlStateDisabled] forState:UIControlStateDisabled];
}
return self;
}

- (void)setImage:(UIImage *)inImage forState:(UIControlState)inState
{
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2), ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2));
if ([inImage respondsToSelector:@selector(resizableImageWithCapInsets:)])
{
// iOS 5
inImage = [inImage resizableImageWithCapInsets:edgeInsets];
}
else
inImage = [inImage stretchableImageWithLeftCapWidth:edgeInsets.left topCapHeight:edgeInsets.top];

[super setImage:inImage forState:inState];
}

- (void)setBackgroundImage:(UIImage *)inImage forState:(UIControlState)inState
{
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2), ceilf(inImage.size.height / 2), ceilf(inImage.size.width / 2));
if ([inImage respondsToSelector:@selector(resizableImageWithCapInsets:)])
{
// iOS 5
inImage = [inImage resizableImageWithCapInsets:edgeInsets];
}
else
inImage = [inImage stretchableImageWithLeftCapWidth:edgeInsets.left topCapHeight:edgeInsets.top];

[super setBackgroundImage:inImage forState:inState];
}

@end

关于ios - UIImage resizableImageWithCapInsets - 这可以使用 Nib 指定吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12612216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com