首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自NSAttributedString的“逆”NSAttributedString

来自NSAttributedString的“逆”NSAttributedString
EN

Stack Overflow用户
提问于 2014-05-13 00:12:55
回答 2查看 1.2K关注 0票数 0

我从一个UIImage创建了一个NSAttributedString

代码语言:javascript
复制
UIFont *font = [UIFont systemFontOfSize:12.0f];
NSDictionary *attributes = @{NSFontAttributeName:font,
                             NSForegroundColorAttributeName:[UIColor greenColor]};

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:title attributes:attributes];

// Create the image here
UIImage *image = [self imageFromAttributedString:attributedString];


#pragma mark - Get image from attributed string

- (UIImage *)imageFromAttributedString:(NSAttributedString *)text
{
    UIGraphicsBeginImageContextWithOptions(text.size, NO, 0.0);

    // draw in context
    [text drawAtPoint:CGPointMake(0.0, 0.0)];

    // transfer image
    UIImage *image = [UIGraphicsGetImageFromCurrentImageContext() imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIGraphicsEndImageContext();

    return image;
}

我的问题是,用相同的文本创建一个图像的最佳方法是什么,但是“反转”,即UIColor greenColor与白色字体文本?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-13 00:44:54

使用NSBackgroundColorAttributeName将背景设置为绿色,并将文本设置为白色。

这些文件包括:

代码语言:javascript
复制
NSString *const NSForegroundColorAttributeName;
NSString *const NSBackgroundColorAttributeName;

ref/doc/uid/TP40011688

票数 1
EN

Stack Overflow用户

发布于 2014-05-13 01:15:29

一旦您有了图像,您可以使用核心图像过滤器反转颜色,或将蓝色更改为粉红色或红色或任何您想要的:有一个名为CIColorInvert的CI过滤器,它将简单地反转颜色,但就我个人而言,我更喜欢使用colorMatrix过滤器,它为我提供了更多的控制代码来逆转如下:

注意,这个例程假设您的起始UIImage是beginImage,输出是endImage

使用ColorMatrix:

代码语言:javascript
复制
 CIFilter *filterci = [CIFilter filterWithName:@"CIColorMatrix"  //rgreen
                                keysAndValues: kCIInputImageKey, beginImage, nil];
[filterci setValue:[CIVector vectorWithX:-1 Y:0 Z:0 W:0] forKey:@"inputRVector"]; // 5
[filterci setValue:[CIVector vectorWithX:0 Y:-1 Z:0 W:0] forKey:@"inputGVector"]; // 6
[filterci setValue:[CIVector vectorWithX:0 Y:0 Z:-1 W:0] forKey:@"inputBVector"]; // 7
[filterci setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"]; // 8
[filterci setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:0] forKey:@"inputBiasVector"];     
 CIImage *boutputImagei = [filterci outputImage];

  CGImageRef cgimgi = [context createCGImage:boutputImagei fromRect:[boutputImage extent]];
UIImage *endImage = [UIImage imageWithCGImage:cgimgi];

 CGImageRelease(cgimgi);

使用CIColorInvert:

代码语言:javascript
复制
CIFilter *filterci = [CIFilter filterWithName:@"CIColorInvert"  //rgreen
                                keysAndValues: kCIInputImageKey, beginImage, nil];
    CIImage *boutputImagei = [filterci outputImage];

  CGImageRef cgimgi = [context createCGImage:boutputImagei fromRect:[boutputImage extent]];
UIImage *endImage = [UIImage imageWithCGImage:cgimgi];

 CGImageRelease(cgimgi);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23620902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档