UILabel - 自定义行间距,字间距及段间距[并动态调节高度]

2 篇文章 0 订阅
1 篇文章 0 订阅

1.如若各项参数无需动态调节则可无视characterSpacing_linesSpacing_.


#import<Foundation/Foundation.h>

#import<UIKit/UIKit.h>


@interface MartinCustomLabel :UILabel

{

@private

   CGFloat characterSpacing_;       //字间距

   long    linesSpacing_;           //行间距

}

@property(nonatomic,assign)CGFloat characterSpacing;

@property(nonatomic,assign)long    linesSpacing;


/*

 *绘制前获取label高度

 */

- (int)getAttributedStringHeightWidthValue:(int)width;


@end

2.因为我无需动态调节行,,.所以我在代码中是写的常量.可按自身需求修改.

#import "MartinCustomLabel.h"

#import<CoreText/CoreText.h>


@interface MartinCustomLabel()

{

@private

    NSMutableAttributedString *attributedString;

}

- (void) initAttributedString;

@end


@implementation MartinCustomLabel


@synthesize characterSpacing =characterSpacing_;


@synthesize linesSpacing =linesSpacing_;


-(id) initWithFrame:(CGRect)frame

{

    //初始化字间距、行间距

   if(self =[superinitWithFrame:frame])

        

    {

        self.characterSpacing =1.5f;

       self.linesSpacing =4.0f;

    }

    

    return self;

}


//外部调用设置字间距

-(void)setCharacterSpacing:(CGFloat)characterSpacing

{

   characterSpacing_ = characterSpacing;

    [selfsetNeedsDisplay];

}


//外部调用设置行间距

-(void)setLinesSpacing:(long)linesSpacing

{

   linesSpacing_ = linesSpacing;

    [selfsetNeedsDisplay];

}


/*

 *初始化AttributedString并进行相应设置

 */

- (void) initAttributedString

{

    if(attributedString==nil){

       //去掉空行

       NSString *labelString = self.text;

        NSString *myString = [labelStringstringByReplacingOccurrencesOfString:@"\r\n"withString:@"\n"];

        

        //创建AttributeString

       attributedString =[[NSMutableAttributedStringalloc]initWithString:myString];

        

        //设置字体及大小

        CTFontRef helveticaBold =CTFontCreateWithName((CFStringRef)self.font.fontName,self.font.pointSize,NULL);

        [attributedStringaddAttribute:(id)kCTFontAttributeNamevalue:(id)helveticaBoldrange:NSMakeRange(0,[attributedStringlength])];

        

       //设置字间距

       long number = 1.5f;

        CFNumberRef num =CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);

        [attributedStringaddAttribute:(id)kCTKernAttributeNamevalue:(id)numrange:NSMakeRange(0,[attributedStringlength])];

       CFRelease(num);

       /*

         if(self.characterSpacing)

         {

         long number = self.characterSpacing;

         CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);

         [attributedString addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(0,[attributedString length])];

         CFRelease(num);

         }

         */

        

       //设置字体颜色

        [attributedStringaddAttribute:(id)kCTForegroundColorAttributeNamevalue:(id)(self.textColor.CGColor)range:NSMakeRange(0,[attributedStringlength])];

        

        //创建文本对齐方式

       CTTextAlignment alignment = kCTLeftTextAlignment;

        if(self.textAlignment == UITextAlignmentCenter)

        {

            alignment =kCTCenterTextAlignment;

        }

        if(self.textAlignment == UITextAlignmentRight)

        {

            alignment =kCTRightTextAlignment;

        }

        

       CTParagraphStyleSetting alignmentStyle;

        

        alignmentStyle.spec =kCTParagraphStyleSpecifierAlignment;

        

        alignmentStyle.valueSize =sizeof(alignment);

        

        alignmentStyle.value = &alignment;

        

        //设置文本行间距

       /*

         CGFloat lineSpace = self.linesSpacing;

         */

       CGFloat lineSpace = 4.0f;

       CTParagraphStyleSetting lineSpaceStyle;

        lineSpaceStyle.spec =kCTParagraphStyleSpecifierLineSpacingAdjustment;

        lineSpaceStyle.valueSize =sizeof(lineSpace);

        lineSpaceStyle.value =&lineSpace;

        

        //设置文本段间距

       CGFloat paragraphSpacing = 15.0;

       CTParagraphStyleSetting paragraphSpaceStyle;

        paragraphSpaceStyle.spec =kCTParagraphStyleSpecifierParagraphSpacing;

        paragraphSpaceStyle.valueSize =sizeof(CGFloat);

        paragraphSpaceStyle.value = &paragraphSpacing;

        

       //创建设置数组

       CTParagraphStyleSetting settings[ ] ={alignmentStyle,lineSpaceStyle,paragraphSpaceStyle};

       CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,3);

        

        //给文本添加设置

        [attributedStringaddAttribute:(id)kCTParagraphStyleAttributeNamevalue:(id)stylerange:NSMakeRange(0 , [attributedStringlength])];

       CFRelease(helveticaBold);

    }

}


/*

 *覆写setText方法

 */

- (void) setText:(NSString *)text

{

    [supersetText:text];

    [selfinitAttributedString];

}


/*

 *开始绘制

 */

-(void) drawTextInRect:(CGRect)requestedRect

{

    [selfinitAttributedString];

    

   //排版

    

    CTFramesetterRef framesetter =CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);

    

   CGMutablePathRef leftColumnPath = CGPathCreateMutable();

    

   CGPathAddRect(leftColumnPath, NULL ,CGRectMake(0 ,0 ,self.bounds.size.width ,self.bounds.size.height));

    

   CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), leftColumnPath , NULL);

    

    //翻转坐标系统(文本原来是倒的要翻转下)

    

    CGContextRef context =UIGraphicsGetCurrentContext();

    

    CGContextSetTextMatrix(context ,CGAffineTransformIdentity);

    

   CGContextTranslateCTM(context , 0 ,self.bounds.size.height);

    

   CGContextScaleCTM(context, 1.0 ,-1.0);

    

    //画出文本

    

   CTFrameDraw(leftFrame,context);

    

   //释放

    

   CGPathRelease(leftColumnPath);

    

   CFRelease(framesetter);

    

    UIGraphicsPushContext(context);

}


/*

 *绘制前获取label高度

 */

- (int)getAttributedStringHeightWidthValue:(int)width

{

    [selfinitAttributedString];

    

   int total_height = 0;

    

    CTFramesetterRef framesetter =CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);    //string 为要计算高度的NSAttributedString

   CGRect drawingRect = CGRectMake(0, 0, width, 100000);  //这里的高要设置足够大

    CGMutablePathRef path =CGPathCreateMutable();

   CGPathAddRect(path, NULL, drawingRect);

   CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path,NULL);

    CGPathRelease(path);

   CFRelease(framesetter);

    

   NSArray *linesArray = (NSArray *)CTFrameGetLines(textFrame);

    

   CGPoint origins[[linesArray count]];

    CTFrameGetLineOrigins(textFrame,CFRangeMake(0,0), origins);

    

   int line_y = (int) origins[[linesArraycount] -1].y//最后一行line的原点y坐标

    

   CGFloat ascent;

   CGFloat descent;

   CGFloat leading;

    

   CTLineRef line = (CTLineRef) [linesArrayobjectAtIndex:[linesArray count]-1];

   CTLineGetTypographicBounds(line, &ascent, &descent, &leading);

    

    total_height =100000 - line_y + (int) descent +1;//+1为了纠正descent转换成int小数点后舍去的值

    

   CFRelease(textFrame);

    

   return total_height;

    

}


@end



 3.创建调用步骤:

 MartinCustomLabel *readNewsLable =[[MartinCustomLabel alloc] initWithFrame:CGRectZero];

 readNewsLable.textColor = textCor;

 readNewsLable.lineBreakMode = UILineBreakModeWordWrap;

 readNewsLable.backgroundColor = [UIColor clearColor];

 readNewsLable.font = TEXT_FONT_NAME([AccessData getNewsConnectFont]);

 [readNewsLable setText:text];

 /*设置labelframe*/

[readNewsLable setFrame:CGRectMake(10, aboveY, WINDOW_W-20, [readNewsLable getAttributedStringHeightWidthValue:WINDOW_W-20])];

readNewsLable.numberOfLines =0;

[readNewsLable setTag:TEXT_LABEL_TAG+tag];

[self addSubview:readNewsLable];





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值