代码之家  ›  专栏  ›  技术社区  ›  hfossli

核心文本/NSStringDrawing-CFAttributedStringRef/nsattributestring-更改字体大小

  •  2
  • hfossli  · 技术社区  · 16 年前

    大小写:属性字符串已创建。如何改变字符串的大小?

    我想我们也可以

    A) 更新属性字符串中所有字体的pointSize

    B) 通过一些变换绘制属性化字符串

    3 回复  |  直到 12 年前
        1
  •  2
  •   hfossli    16 年前

    - (void)recalculateSizeChangeInAttributedString {
    
        if(self.attributedStringOriginal == nil) {
            self.attributedStringOriginal = [self.attributedString copy];
        }
    
        CFMutableAttributedStringRef tempString = CFAttributedStringCreateMutableCopy(CFAllocatorGetDefault(), self.attributedStringOriginal.length, (CFMutableAttributedStringRef)self.attributedStringOriginal);
    
        int lastIndex = 0;
        int limit = CFAttributedStringGetLength(tempString);
        for (int index = 0; index < limit;) {
    
            CFRange inRange = CFRangeMake(0, limit - index);
            CFRange longestEffective;
            CTFontRef font = (CTFontRef)CFAttributedStringGetAttribute(tempString, index, kCTFontAttributeName, &longestEffective);
    
            if(font != nil) {
    
                // log for testing
                NSLog(@"index: %i, range: %i - %i, longest: %i - %i, attribute: %@", 
                      index, inRange.location, 
                      inRange.location + inRange.length, 
                      longestEffective.location, longestEffective.location + longestEffective.length, 
                      @"..." 
                      );
    
    
                // alter the font and set the altered font/attribute
                int rangeEnd = longestEffective.length != 0 ? longestEffective.length : 1;
                CTFontRef modifiedFont = CTFontCreateCopyWithAttributes(font, CTFontGetSize((CTFontRef)font) * sizeFactor, NULL, NULL); 
                CFAttributedStringSetAttribute(tempString, CFRangeMake(index, rangeEnd), kCTFontAttributeName, modifiedFont); 
                CFRelease(modifiedFont);
    
            }
    
            // make next loop continue where current attribute ended
            index += longestEffective.length; 
    
            if(index == lastIndex)
                index ++;
            lastIndex = index;
    
        }
    
        self.attributedString = (NSMutableAttributedString *)tempString;
    
        CFRelease(tempString); 
    
     }
    
        2
  •  2
  •   hfossli    12 年前

    这在macos和iOS上都可以正常工作

    @implementation NSAttributedString (Scale)
    
    - (NSAttributedString *)attributedStringWithScale:(double)scale
    {
        if(scale == 1.0)
        {
            return self;
        }
    
        NSMutableAttributedString *copy = [self mutableCopy];
        [copy beginEditing];
    
        NSRange fullRange = NSMakeRange(0, copy.length);
    
        [self enumerateAttribute:NSFontAttributeName inRange:fullRange options:0 usingBlock:^(UIFont *oldFont, NSRange range, BOOL *stop) {
            double currentFontSize = oldFont.pointSize;
            double newFontSize = currentFontSize * scale;
    
            // don't trust -[UIFont fontWithSize:]
            UIFont *scaledFont = [UIFont fontWithName:oldFont.fontName size:newFontSize];
    
            [copy removeAttribute:NSFontAttributeName range:range];
            [copy addAttribute:NSFontAttributeName value:scaledFont range:range];
        }];
    
        [self enumerateAttribute:NSParagraphStyleAttributeName inRange:fullRange options:0 usingBlock:^(NSParagraphStyle *oldParagraphStyle, NSRange range, BOOL *stop) {
    
            NSMutableParagraphStyle *newParagraphStyle = [oldParagraphStyle mutableCopy];
            newParagraphStyle.lineSpacing *= scale;
            newParagraphStyle.paragraphSpacing *= scale;
            newParagraphStyle.firstLineHeadIndent *= scale;
            newParagraphStyle.headIndent *= scale;
            newParagraphStyle.tailIndent *= scale;
            newParagraphStyle.minimumLineHeight *= scale;
            newParagraphStyle.maximumLineHeight *= scale;
            newParagraphStyle.paragraphSpacing *= scale;
            newParagraphStyle.paragraphSpacingBefore *= scale;
    
            [copy removeAttribute:NSParagraphStyleAttributeName range:range];
            [copy addAttribute:NSParagraphStyleAttributeName value:newParagraphStyle range:range];
        }];
    
        [copy endEditing];
        return copy;
    }
    
    @end
    

    快乐编码

        3
  •  0
  •   Gobra    16 年前

    但是,如果需要这种技巧进行渲染,可以避免字符串解析—使用缩放变换来增加文本大小。

    推荐文章