的UILabel的背景图像iOS8上
问题描述:
我想这样一个背景图片设置为一个UILabel的UILabel的背景图像iOS8上
label.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"img.png"]];
但似乎没有在Xcode 6(iOS版SDK 8)工作,因为我没有得到背景图像当我运行应用程序!
有没有办法用UILabel做到这一点?
- 注: 我不改变类型的UIImageView的原因是,我花了很长一段时间固定的限制,以适应在4个& 3.5屏幕,而且我不想再重复工作
感谢您的帮助。
答
我只是检查它,一切都应该在Xcode6(测试版7)和iOS8中工作。确保您使用的图像确实存在,并且您的名称正确写入。也许最好使用Images.xcassets,所以你不必照顾文件名和路径。
我的代码:
UILabel* label = [UILabel new];
label.frame = self.view.bounds;
label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"icon"]];
[self.view addSubview:label];
答
我使用这个代码来设置的UILabel的背景图像。测试XCode 6 - iOS 7和iOS 8.确保图像存在。
负载从应用程序包中的图像:
label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName.png"]];
或文件目录:
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/imageName.png", docDirectory];
label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:filePath]];
它应该工作。您是不是让标签太小而无法显示图像? – Malloc 2014-09-06 11:59:13
@Malloc即使标签尺寸比它应该小,它不应该显示图像的一部分? //你在iOS8 Xcode6上试过了吗? – iShaalan 2014-09-06 12:07:58
是的,我在Xcode6中试过,是的它应该,但只是为了确保标签符合图像大小。 – Malloc 2014-09-06 12:09:30