// for (NSString * family in [UIFont familyNames]) {
// NSLog(@”familyNames:%@”, family);
// for (NSString * name in [UIFont fontNamesForFamilyName:family]) {
// NSLog(@” name: %@”,name);
// }
// }
// UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
// label.font = [UIFont fontWithName:@”iconfont” size:35];
// label.text = @”\U0000E627”;
// label.textColor = [UIColor greenColor];
// [self.view addSubview:label];
+(UIImage )imageWithIcon:(NSString )iconCode inFont:(NSString )fontName size:(NSUInteger)size color:(UIColor )color{
CGSize imageSize = CGSizeMake(size, size);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, [[UIScreen mainScreen] scale] );
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, size, size)];
label.font = [UIFont fontWithName:fontName size:size];
label.text = iconCode;
if (color) {
label.backgroundColor = color;
}
[label.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return retImage;
}
这段代码展示了如何在iOS应用中创建SVG图标并将其显示在webView上。首先,遍历字体家族以查找特定字体名称,然后创建UILabel并设置字体、颜色和内容。接着,定义一个方法用于将图标代码转换为UIImage,通过设置UILabel的属性并渲染到上下文来实现。最后,从当前图形上下文获取图像并返回。
1324

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



