ios开发之 改变文本行间距和字符间距
2016-08-02 10:02
323 查看
ios开发之 改变文本行间距和字符间距
#import <UIKit/UIKit.h>@interface LineSpaceLabel : UILabel{
CGFloat charSpace_;
CGFloat lineSpace_;
}
@property(nonatomic, assign) CGFloat charSpace;
@property(nonatomic, assign) CGFloat lineSpace;
@end
#import "LineSpaceLabel.h"
#import <CoreText/CoreText.h>
#import<Foundation/Foundation.h>
@implementation LineSpaceLabel
@synthesize lineSpace = lineSpace_;
@synthesize charSpace = charSpace_;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
lineSpace_ = 5.0;
charSpace_ = 2.0;
}
return self;
}
-(void)awakeFromNib{
[super awakeFromNib];
lineSpace_ = 5.0;
charSpace_ = 2.0;
}
-(void)setCharSpace:(CGFloat)charSpace{
charSpace_ = charSpace;
[self setNeedsDisplay];
}
-(void)setLineSpace:(CGFloat)lineSpace{
lineSpace_ = lineSpace;
[self setNeedsDisplay];
}
-(void) drawTextInRect:(CGRect)requestedRect
{
//创建AttributeString
NSMutableAttributedString *string =[[NSMutableAttributedString alloc]initWithString:self.text];
//设置字体及大小
CTFontRef helveticaBold = CTFontCreateWithName((CFStringRef)self.font.fontName,self.font.pointSize,NULL);
[string addAttribute:(id)kCTFontAttributeName value:(id)helveticaBold range:NSMakeRange(0,[string length])];
//设置字间距
if(self.charSpace)
{
long number = self.charSpace;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[string addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(0,[string length])];
CFRelease(num);
}
//设置字体颜色
[string addAttribute:(id)kCTForegroundColorAttributeName value:(id)(self.textColor.CGColor) range:NSMakeRange(0,[string length])];
//创建文本对齐方式
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.lineSpace;
CTParagraphStyleSetting lineSpaceStyle;
lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
lineSpaceStyle.valueSize = sizeof(lineSpace);
lineSpaceStyle.value =&lineSpace;
//设置文本段间距
CGFloat paragraphSpacing = 5.0;
CTParagraphStyleSetting paragraphSpaceStyle;
paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
paragraphSpaceStyle.valueSize = sizeof(CGFloat);
paragraphSpaceStyle.value = ¶graphSpacing;
//创建设置数组
CTParagraphStyleSetting settings[ ] ={alignmentStyle,lineSpaceStyle,paragraphSpaceStyle};
CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,3);
//给文本添加设置
[string addAttribute:(id)kCTParagraphStyleAttributeName value:(id)style range:NSMakeRange(0 , [string length])];
//排版
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
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);
CFRelease(helveticaBold);
[string release];
UIGraphicsPushContext(context);
}
@end
相关文章推荐
- ios开发笔记之如何让tableView根据文本内容动态改变cell的高度
- iOS开发小知识之改变UIlabel的行间距和字间距
- iOS开发——改变UITextField的样式:占位符颜色、字符起始位置等
- IOS开发之改变UIWebView文本字体的样式和大小
- iOS开发小知识之改变UIlabel的行间距和字间距
- ios开发之改变UIWebView文本字体的样式和大小
- IOS开发之改变UIWebView文本字体的样式和大小
- iOS开发小知识之改变UIlabel的行间距和字间距
- iOS 当UILabel多行时,改变文本的行间距
- ios开发之改变UIWebView文本字体的样式和大小
- 改变文本行间距和字符间距
- iOS开发UILabel多行文本设置行间距
- iOS NSString 文本不同的颜色 标题+文本字体大小 行间距/删除不需要的字符 /以及自适应高度
- ios开发之改变UIAlertView背景图片
- iOS开发那些事-iOS应用本地化-文本信息本地化
- IOS开发(78)之绘制文本
- iOS开发那些事-iOS应用本地化-文本信息本地化
- iOS开发那些事-iOS应用本地化-文本信息本地化 推荐
- IOS开发之带格式的文本
- iOS开发 UITableViewCell的背景改变问题