我正在使用NSAttributedString将图像包含在字符串中。但是,图像有时会模糊,就像在非整数帧上绘制一样。
我试图确保每个NSTextAttachment的边界是整数大小,但这似乎没有帮助。关于如何确保它不模糊的任何提示?
参见附页截图,第一辆公交车并不模糊,但第二辆公交车是模糊的。
答案 0 :(得分:1)
我通过在NSAttributedString上添加一个类别来解决这个问题。
基本上,您需要获取NSTextAttachment的框架,并添加其X坐标的缺失小数部分,以使其很好地对齐。
- (void)applyBlurrinessFixToAttachments {
[self enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, self.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (![value isKindOfClass:[NSTextAttachment class]]) {
return;
}
NSTextAttachment *attachment = (NSTextAttachment*)value;
CGRect bounds = attachment.bounds;
CGRect attributedStringRect = [self boundingRectForCharacterRange:range];
double integral;
double fractional = modf(attributedStringRect.origin.x, &integral);
if (fractional > 0) {
double roundedXOrigin = 1.0 - fractional;
// If X coordinate starts at 0.7, add 0.3 to it
bounds.origin.x += roundedXOrigin;
attachment.bounds = bounds;
}
}];
}
- (CGRect)boundingRectForCharacterRange:(NSRange)range {
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:self];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
textContainer.lineFragmentPadding = 0;
[layoutManager addTextContainer:textContainer];
NSRange glyphRange;
[layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange];
return [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
}
答案 1 :(得分:0)
您可以使用以下代码进行图像轮次
UIGraphicsBeginImageContextWithOptions(CGSizeMake(14,14),NO,[UIScreen mainScreen] .scale);
-(UIImage *)makeRoundedImage:(UIImage *) image radius: (float) radius {
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, 14, 14);
imageLayer.contents = (id) image.CGImage;
imageLayer.masksToBounds = YES;
imageLayer.cornerRadius = radius;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(14, 14), NO, [UIScreen mainScreen].scale);
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundedImage;
}