• 第2月第24天 coretext 行高


    1.NSMutableAttributedString 行高

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            
            [paragraphStyle setLineSpacing:LINESPACE];//调整行间距
            
            [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
            label.attributedText = attributedString;

    http://blog.sina.com.cn/s/blog_9256a1210101ku7o.html

    2.boundingRectWithSize 行高

    #define UILABEL_LINE_SPACE 6
    #define HEIGHT [ [ UIScreen mainScreen ] bounds ].size.height
    //给UILabel设置行间距和字间距
    -(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
        paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
        paraStyle.alignment = NSTextAlignmentLeft;
        paraStyle.lineSpacing = UILABEL_LINE_SPACE; //设置行间距
        paraStyle.hyphenationFactor = 1.0;
        paraStyle.firstLineHeadIndent = 0.0;
        paraStyle.paragraphSpacingBefore = 0.0;
        paraStyle.headIndent = 0;
        paraStyle.tailIndent = 0;
        //设置字间距 NSKernAttributeName:@1.5f
        NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
    };
        
        NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
        label.attributedText = attributeStr;
    }
    
    //计算UILabel的高度(带有行间距的情况)
    -(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {
        NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
        paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
        paraStyle.alignment = NSTextAlignmentLeft;
        paraStyle.lineSpacing = UILABEL_LINE_SPACE;
        paraStyle.hyphenationFactor = 1.0;
        paraStyle.firstLineHeadIndent = 0.0;
        paraStyle.paragraphSpacingBefore = 0.0;
        paraStyle.headIndent = 0;
        paraStyle.tailIndent = 0;
        NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
    };
        
        CGSize size = [str boundingRectWithSize:CGSizeMake(width, HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
        return size.height;
    }

    http://blog.csdn.net/luco2008/article/details/50977718

    3.CTFramesetterSuggestFrameSizeWithConstraints 行高

     

    1)CFMutableAttributedStringRef/NSAttributedString

    2)CTParagraphStyleRef/NSMutableParagraphStyle

     

    NSString  *text = @"This
    is
    some
    multi-line
    sample
    text."
    UIFont    *uiFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CTFontRef ctFont = CTFontCreateWithName((CFStringRef) uiFont.fontName, uiFont.pointSize, NULL);
    
    //  When you create an attributed string the default paragraph style has a leading 
    //  of 0.0. Create a paragraph style that will set the line adjustment equal to
    //  the leading value of the font.
    CGFloat leading = uiFont.lineHeight - uiFont.ascender + uiFont.descender;
    CTParagraphStyleSetting paragraphSettings[1] = { kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof (CGFloat), &leading };
    
    CTParagraphStyleRef  paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 1);
    CFRange textRange = CFRangeMake(0, text.length);
    
    //  Create an empty mutable string big enough to hold our test
    CFMutableAttributedStringRef string = CFAttributedStringCreateMutable(kCFAllocatorDefault, text.length);
    
    //  Inject our text into it
    CFAttributedStringReplaceString(string, CFRangeMake(0, 0), (CFStringRef) text);
    
    //  Apply our font and line spacing attributes over the span
    CFAttributedStringSetAttribute(string, textRange, kCTFontAttributeName, ctFont);
    CFAttributedStringSetAttribute(string, textRange, kCTParagraphStyleAttributeName, paragraphStyle);
    
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(string);
    CFRange fitRange;
    
    CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, textRange, NULL, bounds, &fitRange);
    
    CFRelease(framesetter);
    CFRelease(string);

    http://stackoverflow.com/questions/3374591/ctframesettersuggestframesizewithconstraints-sometimes-returns-incorrect-size

    http://blog.sina.com.cn/s/blog_6308b98c0101byh9.html

    4.yytext

    http://www.cnblogs.com/lujianwenance/p/5716804.html

    5.nsattributedstring 转nsstring

    6.UILabel和Scrollview结合用,label高度自适应

    https://my.oschina.net/langzhouzhou1/blog/648748

    7.计算高度

    - (CGFloat)boundingRectWithSize:(CGFloat)width
               withAttributedString:(NSMutableAttributedString *)attrt {
      CGFloat height = 0;
      NSStringDrawingOptions options =
          NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
      CGRect rect = [attrt boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                        options:options
                                        context:nil];
      height = ceilf(rect.size.height);
      return height;
    }
    
    - (CGFloat)stringWidthWithSize:(CGSize)size font:(UIFont *)font {
      CGFloat width;
    
      if ([PAUntils isAtLeastIOS_7_0]) {
        CGRect rect =
            [self boundingRectWithSize:size
                               options:NSStringDrawingUsesLineFragmentOrigin
                            attributes:@{
                              NSFontAttributeName : font
                            }
                               context:nil];
        width = rect.size.width;
      } else {
        CGSize strSize = [self sizeWithFont:font constrainedToSize:size];
        width = strSize.width;
      }
    
      return width;
    }
  • 相关阅读:
    Java-死锁
    Java使用Redis
    MySQL如何开启慢查询
    VGG
    如何使用Soft-NMS实现目标检测并提升准确率
    非极大值抑制(NMS)
    迁移学习与fine-tuning有什么区别
    Keras-在预训练好网络模型上进行fine-tune
    Kotlin——初级篇(六):空类型、空安全、非空断言、类型转换等特性总结
    Kotlin——初级篇(五):操作符与操作符重载一
  • 原文地址:https://www.cnblogs.com/javastart/p/6097513.html
Copyright © 2020-2023  润新知