

根据数字,转成对应的图片
- (void)viewDidLoad {
[super viewDidLoad];
[self testNum2String:10086];
}
/// 根据数字,显示对应的图片 数字用特定的图片显示
- (void)testNum2String:(NSInteger)num {
UIView *numContentView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 0, 20)];
numContentView.backgroundColor = UIColor.redColor;
NSString *str = [NSString stringWithFormat:@"%zd", num];
NSInteger length = str.length;
for (int i = 0; i < length; i++) {
// 取到每一位数字 这一行是关键代码
char ch = [str characterAtIndex:i];
NSLog(@"word is %c", ch);
NSString *imgName = [NSString stringWithFormat:@"icon_num_%c", ch];
// 根据数字,取对应的图片
UIImage *img = [UIImage imageNamed:imgName];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = CGRectMake(numContentView.frame.size.width, 0, 10, 20);
[numContentView addSubview:imgView];
numContentView.frame = CGRectMake(100, 100, CGRectGetMaxX(imgView.frame), 20);
}
[self.view addSubview:numContentView];
}
数字转图像:iOS中的字符映射图片显示,
该篇文章介绍了如何在iOS中,根据传入的数字将每个数字字符转换为对应的图片,并在视图上显示出来,使用了NSString和UIImageView等类进行操作。
716

被折叠的 条评论
为什么被折叠?



