2014-12-03 24 views
5

Tôi đang sử dụng NSAttributedString để bao gồm hình ảnh trong chuỗi. Tuy nhiên, các hình ảnh đôi khi bị mờ như thể được vẽ trên một khung không phải số nguyên.Hình ảnh mờ (NSTextAttachment) trên NSAttributedString

Tôi đã cố gắng đảm bảo rằng giới hạn của mỗi NSTextAttachment là kích thước nguyên, nhưng điều đó dường như không giúp ích gì. Bất kỳ lời khuyên nào về cách đảm bảo nó không bị mờ?

Xem ảnh chụp màn hình được đính kèm, xe buýt đầu tiên không bị mờ nhưng thứ hai là.

enter image description here

+0

Làm cách nào để hiển thị chuỗi được phân bổ? ('UILabel',' CATextLayer', khác) –

+0

Đó là trên UILabel – gcamp

+0

Tôi sẽ thử với CATextLayer – gcamp

Trả lời

1

Tôi đã sửa lỗi này bằng cách thêm danh mục trên NSAttributedString.

Về cơ bản, bạn cần lấy khung của NSTextAttachment và thêm phần phân đoạn còn thiếu của tọa độ X của nó để làm tròn nó một cách độc đáo.

- (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]; 
} 
0

bạn có thể sử dụng dưới mã cho hình ảnh vòng

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; 
} 
+1

Có thể bạn giải thích tại sao và cách mã hoạt động? –

+0

để thay đổi kích thước hình ảnh, trước tiên, bạn phải xác định kích thước mục tiêu để chia tỷ lệ – amitg3

Các vấn đề liên quan