kCTVerticalFormsAttributeName的竖排如何绘制竖排文本框在ios11上为什么无效

1添加评论分享收藏感谢收起赞同 1添加评论分享收藏感谢收起写回答blog因你而精彩。
IOS CoreText.framework --- 基本用法
API接口文档。
https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CoreText_Framework_Ref/_index.html
CoreText 框架中最常用的几个类:
先来了解一下该框架的整体视窗组合图:
CTFrame 作为一个整体的画布(Canvas),其中由行(CTLine)组成,而每行可以分为一个或多个小方块(CTRun)。
注意:你不需要自己创建CTRun,Core Text将根据NSAttributedString的属性来自动创建CTRun。每个CTRun对象对应不同的属性,正因此,你可以自由的控制字体、颜色、字间距等等信息。
通常处理步聚:
1.使用core text就是先有一个要显示的string,然后定义这个string每个部分的样式-&attributedString -& 生成 CTFramesetter -& 得到CTFrame -& 绘制(CTFrameDraw)其中可以更详细的设置换行方式,对齐方式,绘制区域的大小等。2.绘制只是显示,点击事件就需要一个判断了。CTFrame 包含了多个CTLine,并且可以得到各个line的其实位置与大小。判断点击处在不在某个line上。CTLine 又可以判断这个点(相对于ctline的坐标)处的文字范围。然后遍历这个string的所有NSTextCheckingResult,根据result的rang判断点击处在不在这个rang上,从而得到点击的链接与位置。
字体的基本知识:
字体(Font):是一系列字号、样式和磅值相同的字符(例如:10磅黑体Palatino)。现多被视为字样的同义词
字面(Face):是所有字号的磅值和格式的综合
字体集(Font family):是一组相关字体(例如:Franklin family包括Franklin Gothic、Fran-klinHeavy和Franklin Compressed)
磅值(Weight):用于描述字体粗度。典型的磅值,从最粗到最细,有极细、细、book、中等、半粗、粗、较粗、极粗
样式(Style):字形有三种形式:Roman type是直体;oblique type是斜体;utakuc type是斜体兼曲线(比Roman type更像书法体)。
x高度(X height):指小写字母的平均高度(以x为基准)。磅值相同的两字母,x高度越大的字母看起来比x高度小的字母要大
Cap高度(Cap height):与x高度相似。指大写字母的平均高度(以C为基准)
下行字母(Descender):例如在字母q中,基线以下的字母部分叫下伸部分
上行字母(Ascender):x高度以上的部分(比如字母b)叫做上伸部分
基线(Baseline):通常在x、v、b、m下的那条线
描边(Stroke):组成字符的线或曲线。可以加粗或改变字符形状
衬线(Serif):用来使字符更可视的一条水平线。如字母左上角和下部的水平线。
无衬线(Sans Serif):可以让排字员不使用衬线装饰。
方形字(Block):这种字体的笔画使字符看起来比无衬线字更显眼,但还不到常见的衬线字的程度。例如Lubalin Graph就是方形字,这种字看起来好像是木头块刻的一样
手写体脚本(Calligraphic script):是一种仿效手写体的字体。例如Murray Hill或者Fraktur字体
艺术字(Decorative):像绘画般的字体
Pi符号(Pisymbol):非标准的字母数字字符的特殊符号。例如Wingdings和Mathematical Pi
连写(Ligature):是一系列连写字母如fi、fl、ffi或ffl。由于字些字母形状的原因经常被连写,故排字员已习惯将它们连写。
字符属性名称:
const CFStringRef kCTCharacterShapeAttributeN
//字体形状属性
必须是CFNumberRef对象默认为0,非0则对应相应的字符形状定义,如1表示传统字符形状
const CFStringRef kCTFontAttributeN
//字体属性
必须是CTFont对象
const CFStringRef kCTKernAttributeN
//字符间隔属性 必须是CFNumberRef对象
const CFStringRef kCTLigatureAttributeN
//设置是否使用连字属性,设置为0,表示不使用连字属性。标准的英文连字有FI,FL.默认值为1,既是使用标准连字。也就是当搜索到f时候,会把fl当成一个文字。必须是CFNumberRef 默认为1,可取0,1,2
const CFStringRef kCTForegroundColorAttributeN
//字体颜色属性
必须是CGColor对象,默认为black
const CFStringRef kCTForegroundColorFromContextAttributeN
//上下文的字体颜色属性 必须为CFBooleanRef 默认为False,
const CFStringRef kCTParagraphStyleAttributeN
//段落样式属性 必须是CTParagraphStyle对象 默认为NIL
const CFStringRef kCTStrokeWidthAttributeN
//笔画线条宽度 必须是CFNumberRef对象,默为0.0f,标准为3.0f
const CFStringRef kCTStrokeColorAttributeN
//笔画的颜色属性 必须是CGColorRef 对象,默认为前景色
const CFStringRef kCTSuperscriptAttributeN
//设置字体的上下标属性 必须是CFNumberRef对象 默认为0,可为-1为下标,1为上标,需要字体支持才行。如排列组合的样式Cn1
const CFStringRef kCTUnderlineColorAttributeN
//字体下划线颜色属性 必须是CGColorRef对象,默认为前景色
const CFStringRef kCTUnderlineStyleAttributeN
//字体下划线样式属性 必须是CFNumberRef对象,默为kCTUnderlineStyleNone 可以通过CTUnderlineStypleModifiers 进行修改下划线风格
const CFStringRef kCTVerticalFormsAttributeN
//文字的字形方向属性 必须是CFBooleanRef 默认为false,false表示水平方向,true表示竖直方向
const CFStringRef kCTGlyphInfoAttributeN
//字体信息属性 必须是CTGlyphInfo对象
const CFStringRef kCTRunDelegateAttributeName
//CTRun 委托属性 必须是CTRunDelegate对象
举例说明:
NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:@"This is a test of characterAttribute. 中文字符"];
//设置字体属性
CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"), 40, NULL);
[mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)]; //设置斜体字
CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL);
[mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];//下划线
[mabstring addAttribute:(id)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)]; //下划线颜色
[mabstring addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)];
//设置字体简隔 eg:test
long number = 10;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(10, 4)];//设置连字
long number = 1;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring addAttribute:(id)kCTLigatureAttributeName value:(id)num range:NSMakeRange(0, [str length])];连字还不会使用,未看到效果。//设置字体颜色
[mabstring addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)];//设置字体颜色为前影色
CFBooleanRef flag = kCFBooleanT
[mabstring addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(id)flag range:NSMakeRange(5, 10)];无明显效果。
//设置空心字
long number = 2;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];//设置空心字
long number = 2;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];
//设置空心字颜色
[mabstring addAttribute:(id)kCTStrokeColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [str length])];
在设置空心字颜色时,必须先将字体高为空心,否则设置颜色是没有效果的。
//对同一段字体进行多属性设置
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];
CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 40, NULL);
[attributes setObject:(id)font forKey:(id)kCTFontAttributeName];
[attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];
[mabstring addAttributes:attributes range:NSMakeRange(0, 4)];
最后是draw了。
-(void)characterAttribute
NSString *str = @"This is a test of characterAttribute. 中文字符";
NSMutableAttributedString *mabstring = [[NSMutableAttributedString alloc]initWithString:str];
[mabstring beginEditing];
long number = 1;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring addAttribute:(id)kCTCharacterShapeAttributeName value:(id)num range:NSMakeRange(0, 4)];
//设置字体属性
CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"), 40, NULL);
[mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
//设置字体简隔 eg:test
long number = 10;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring addAttribute:(id)kCTKernAttributeName value:(id)num range:NSMakeRange(10, 4)];
long number = 1;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring addAttribute:(id)kCTLigatureAttributeName value:(id)num range:NSMakeRange(0, [str length])];
//设置字体颜色
[mabstring addAttribute:(id)kCTForegroundColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 9)];
//设置字体颜色为前影色
CFBooleanRef flag = kCFBooleanT
[mabstring addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(id)flag range:NSMakeRange(5, 10)];
//设置空心字
long number = 2;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring addAttribute:(id)kCTStrokeWidthAttributeName value:(id)num range:NSMakeRange(0, [str length])];
//设置空心字颜色
[mabstring addAttribute:(id)kCTStrokeColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(0, [str length])];
long number = 1;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[mabstring addAttribute:(id)kCTSuperscriptAttributeName value:(id)num range:NSMakeRange(3, 1)];
//设置斜体字
CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL);
[mabstring addAttribute:(id)kCTFontAttributeName value:(id)font range:NSMakeRange(0, 4)];
[mabstring addAttribute:(id)kCTUnderlineStyleAttributeName value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] range:NSMakeRange(0, 4)];
//下划线颜色
[mabstring addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[UIColor redColor].CGColor range:NSMakeRange(0, 4)];
//对同一段字体进行多属性设置
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];
CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 40, NULL);
[attributes setObject:(id)font forKey:(id)kCTFontAttributeName];
[attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];
[mabstring addAttributes:attributes range:NSMakeRange(0, 4)];
NSRange kk = NSMakeRange(0, 4);
NSDictionary * dc = [mabstring attributesAtIndex:0 effectiveRange:&kk];
[mabstring endEditing];
NSLog(@"value = %@",dc);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)mabstring);
CGMutablePathRef Path = CGPathCreateMutable();
CGPathAddRect(Path, NULL ,CGRectMake(10 , 0 ,self.bounds.size.width-10 , self.bounds.size.height-10));
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), Path, NULL);
//获取当前(View)上下文以便于之后的绘画,这个是一个离屏。
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context , CGAffineTransformIdentity);
//压栈,压入图形状态栈中.每个图形上下文维护一个图形状态栈,并不是所有的当前绘画环境的图形状态的元素都被保存。图形状态中不考虑当前路径,所以不保存
//保存现在得上下文图形状态。不管后续对context上绘制什么都不会影响真正得屏幕。
CGContextSaveGState(context);
//x,y轴方向移动
CGContextTranslateCTM(context , 0 ,self.bounds.size.height);
//缩放x,y轴方向缩放,-1.0为反向1.0倍,坐标系转换,沿x轴翻转180度
CGContextScaleCTM(context, 1.0 ,-1.0);
CTFrameDraw(frame,context);
CGPathRelease(Path);
CFRelease(framesetter);
- (void)drawRect:(CGRect)rect
[self characterAttribute];
}CORETEXT框架图
另对于Context的了解可以参考:http://www.padovo.com/blog//study-coretext/
没有更多推荐了,&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
iOS Context 基础教程 (三)完整实现UILabel末尾 实现『省略号
摘要:直接上代码,实现限制UIlabel最多显示行数,超出后显示『。。。全文』效果实现:细节还待优化,给需要的人参考吧BDHKLabelAttribute.h//BDHKLabelAttribute.h#import/*!@enumBDHKVerticalTextAlignment@abstract垂直方式*/typedefNS_ENUM(NSInteger,BDHKVerticalTextAlignment){/*!@abstract顶部*/BDHKVerticalTextAli
直接上代码,实现限制UIlabel最多显示行数,超出后显示 『。。。全文』效果实现:
细节还待优化,给需要的人参考吧
BDHKLabelAttribute.h
//BDHKLabelAttribute.h
@enum BDHKVerticalTextAlignment
@abstract 垂直方式
typedef NS_ENUM(NSInteger, BDHKVerticalTextAlignment) {
@abstract 顶部
BDHKVerticalTextAlignmentTop,
@abstract 居中
BDHKVerticalTextAlignmentMiddle,
@abstract 底部
BDHKVerticalTextAlignmentBottom
@interface BDHKLabelAttribute : UILabel
@property verticalTextAlignment
@abstract 上下对齐方式
@property (nonatomic, assign) BDHKVerticalTextAlignment verticalTextA
@property linesSpace
@abstract 行间距
@property (nonatomic, assign) CGFloat linesS
@property margin
@abstract 边距
@property (nonatomic, assign) UIEdgeI
@property lastLineRightIndent
@abstract 尾行右缩进
//@property (nonatomic, assign) CGFloat lastLineRightI
@property truncationEndAttributedString
@abstract 尾行结束字符串
@property (nonatomic, strong) NSAttributedString *truncationEndAttributedS
@property linesSpace
@abstract 实际绘制的行数,当numberOfLines==0时
@property (nonatomic, assign, readonly) NSInteger drawOfL
@interface NSString(BDHKLabelAttribute)
@method sizeWithFont:width:linesSpace:numberOfLines:
@abstract 测量字符串所要的大小
@param font 字体
@param width 宽度
@param linesSpace 行高
@param numberOfLines 行数
- (CGSize)sizeWithFont:(UIFont *)font width:(CGFloat)width linesSpace:(CGFloat)linesSpace numberOfLines:(NSInteger)numberOfL
BDHKLabelAttribute.m
//BDHKLabelAttribute.m
#import &BDHKLabelAttribute.h&
#import &UIView+BDHKUtil.h&
@interface BDHKLabelAttribute()
@property (nonatomic, copy) NSMutableAttributedString *attributedS
@property (nonatomic, assign) BOOL isD
@property (nonatomic, assign) BOOL isT
@implementation BDHKLabelAttribute
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self) {
_margin = UIEdgeInsetsZ
_verticalTextAlignment = BDHKVerticalTextAlignmentT
_isDisplay = YES;
- (id)initWithCoder:(NSCoder *)aDecoder
self = [super initWithCoder:aDecoder];
if (self) {
_margin = UIEdgeInsetsZ
_verticalTextAlignment = BDHKVerticalTextAlignmentT
_isDisplay = YES;
-(void)dealloc
_attributedString =
_truncationEndAttributedString =
- (void)drawTextInRect:(CGRect)rect
if (self.text.length == 0 &;&; self.attributedText == nil) {
if (_isDisplay) {
if (_isText) {
self.attributedString = [self attributedStringAddStyle:self.text];
self.attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
_isDisplay = NO;
if (!_attributedString) {
CGRect drawRect = CGRectMake(_margin.left, _margin.top, self.bounds.size.width - _margin.left - _margin.right, self.bounds.size.height - _margin.top - _margin.bottom);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)_attributedString);
CGMutablePathRef forecastPath = CGPathCreateMutable();
//CGPathAddRect(forecastPath, NULL ,self.bounds);
CGPathAddRect(forecastPath, NULL ,drawRect);
CTFrameRef forecastFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0, 0), forecastPath , NULL);
CFArrayRef forecastLines = CTFrameGetLines(forecastFrame);
long forecastMaxLineNumber = (long)CFArrayGetCount(forecastLines);
//CGRect drawingRect = CGRectMake(0, 0, self.bounds.size.width, CGFLOAT_MAX);
CGRect drawingRect = CGRectMake(drawRect.origin.x, drawRect.origin.y, drawRect.size.width, );
CGMutablePathRef textpath = CGPathCreateMutable();
CGPathAddRect(textpath, NULL, drawingRect);
CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), textpath, NULL);
CFArrayRef textlines = CTFrameGetLines(textFrame);
long textMaxLineNumber = (long)CFArrayGetCount(textlines);
long minLinesNumber = MIN(forecastMaxLineNumber, textMaxLineNumber);
_drawOfLines = (int)(self.numberOfLines == 0 ? minLinesNumber : MIN(minLinesNumber, self.numberOfLines));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context , CGAffineTransformIdentity);
CGContextTranslateCTM(context , 0 ,self.bounds.size.height);
CGContextScaleCTM(context, 1.0 ,-1.0);
CGPoint lineOrigins[_drawOfLines];
CTFrameGetLineOrigins(forecastFrame,CFRangeMake(0,_drawOfLines), lineOrigins);
for(int lineIndex = 0;lineIndex & _drawOfLlineIndex++) {
CTLineRef line = CFArrayGetValueAtIndex(forecastLines,lineIndex);
CGPoint lineO
if (forecastMaxLineNumber &= _drawOfLines &;&; _verticalTextAlignment != BDHKVerticalTextAlignmentTop) {
if (_verticalTextAlignment == BDHKVerticalTextAlignmentMiddle) {
float topMargin = lineOrigins[_drawOfLines - 1].y / 2.0;
lineOrigin = lineOrigins[lineIndex];
lineOrigin = CGPointMake(lineOrigin.x,lineOrigin.y - floorf(topMargin));
else if(_verticalTextAlignment == BDHKVerticalTextAlignmentBottom){
CTLineGetTypographicBounds(line, &;ascent, &;descent, ≤ading);
lineOrigin = lineOrigins[_drawOfLines - 1 - lineIndex];
lineOrigin = CGPointMake(lineOrigin.x, (self.height - floorf(lineOrigin.y + ascent - descent)));
lineOrigin = lineOrigins[lineIndex];
CTLineRef lastLine =
if (_drawOfLines & textMaxLineNumber) {
if (lineIndex == _drawOfLines - 1) {
CFRange range= CTLineGetStringRange(line);
NSDictionary *attributes = [_attributedString attributesAtIndex:range.location + range.length - 1 effectiveRange:NULL];
NSAttributedString *token = [[NSAttributedString alloc] initWithString:@&/u2026& attributes:attributes];
if (_truncationEndAttributedString != nil) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:token];
[attributedString appendAttributedString:_truncationEndAttributedString];
token = attributedS
CFAttributedStringRef tokenRef = (__bridge CFAttributedStringRef)
CTLineRef truncationToken = CTLineCreateWithAttributedString(tokenRef);
NSRange lastLineRange = NSMakeRange(range.location, 0);
lastLineRange.length = [_attributedString length] - lastLineRange.
CFAttributedStringRef longString = (__bridge CFAttributedStringRef)[_attributedString attributedSubstringFromRange:lastLineRange];
CTLineRef endLine = CTLineCreateWithAttributedString(longString);
//lastLine = CTLineCreateTruncatedLine(endLine, self.width - _lastLineRightIndent, kCTLineTruncationEnd, truncationToken);
lastLine = CTLineCreateTruncatedLine(endLine, self.width, kCTLineTruncationEnd, truncationToken);
if (truncationToken) {
CFRelease(truncationToken);
if (endLine) {
CFRelease(endLine);
if (lastLine) {
CGContextSetTextPosition(context,lineOrigin.x,lineOrigin.y);
CTLineDraw(lastLine,context);
//从一行中得到CTRun数组,最后一个字的位置
//CFArrayRef runs = CTLineGetGlyphRuns(lastLine);
//long runCount = (long)CFArrayGetCount(runs);
//CTLineGetOffsetForStringIndex(line, runCount - 1, NULL);
CFRelease(lastLine);
CGContextSetTextPosition(context,lineOrigin.x,lineOrigin.y);
CTLineDraw(line,context);
UIGraphicsPushContext(context);
CFRelease(textpath);
CFRelease(textFrame);
CFRelease(forecastFrame);
CFRelease(forecastPath);
CFRelease(framesetter);
#pragma mark --
- (NSMutableAttributedString *)attributedStringAddStyle:(NSString *)string
if (!string) {
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = (_linesSpace & 0) ? _linesSpace : 4 ;
style.paragraphSpacing = 0;
style.alignment = self.textA
NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc]initWithString:string];
NSDictionary *attributes = @{NSForegroundColorAttributeName:self.textColor,NSFontAttributeName:self.font,NSParagraphStyleAttributeName:style};
[attributedString addAttributes:attributes range:NSMakeRange(0, [attributedString length])];
return attributedS
#pragma mark --
- (void)setText:(NSString *)text
self.isDisplay = YES;
self.isText = YES;
[super setText:text];
- (void)setAttributedText:(NSAttributedString *)attributedText
self.isDisplay = YES;
self.isText = NO;
[super setAttributedText:attributedText];
- (void)setTextColor:(UIColor *)textColor
self.isDisplay = YES;
[super setTextColor:textColor];
-(void)setTextAlignment:(NSTextAlignment)textAlignment
self.isDisplay = YES;
[super setTextAlignment:textAlignment];
-(void)setFont:(UIFont *)font
self.isDisplay = YES;
[super setFont:font];
-(void)setLinesSpace:(CGFloat)linesSpace
self.isDisplay = YES;
_linesSpace = linesS
[self setNeedsDisplay];
-(void)setVerticalTextAlignment:(BDHKVerticalTextAlignment)verticalTextAlignment
_verticalTextAlignment = verticalTextA
[self setNeedsDisplay];
-(void)setMargin:(UIEdgeInsets)margin
[self setNeedsDisplay];
@implementation NSString(BDHKLabelAttribute)
- (CGSize)sizeWithFont:(UIFont *)font width:(CGFloat)width linesSpace:(CGFloat)linesSpace numberOfLines:(NSInteger)numberOfLines
if (self.length &= 0) {
return CGSizeZ
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = linesS
style.paragraphSpacing = 0;
NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:self];
NSDictionary *attributes = @{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:font,NSParagraphStyleAttributeName:style};
[attributedString addAttributes:attributes range:NSMakeRange(0, [attributedString length])];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
if (numberOfLines == 0) {
CGSize size = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(width, ), NULL);
size = CGSizeMake(ceilf(size.width), ceilf(size.height));
CFRelease(framesetter);
CGRect drawingRect = CGRectMake(0, 0, width, );
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, drawingRect);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);
CFArrayRef lines = CTFrameGetLines(frameRef);
long linesNumber = (long)CFArrayGetCount(lines);
if (linesNumber & 1) {//没有内容
CGPathRelease(path);
CFRelease(frameRef);
CFRelease(framesetter);
return CGSizeZ
numberOfLines = (numberOfLines == 0) ? linesNumber : MIN(linesNumber, numberOfLines);
CGPoint lineOrigins[numberOfLines];
CTFrameGetLineOrigins(frameRef,CFRangeMake(0,numberOfLines), lineOrigins);
long lastLineNumber = MAX(numberOfLines - 1, 0);
CGPoint lineOrigin = lineOrigins[lastLineNumber];//最后一行line的位置
CTLineRef line = CFArrayGetValueAtIndex(lines, lastLineNumber);
CTLineGetTypographicBounds(line, &;ascent, &;descent, ≤ading);
CGFloat height = drawingRect.size.height - floorf(lineOrigin.y) + ceilf(lineDescent);
if ([[UIDevice currentDevice].systemVersion floatValue] &= 10.0) {
height += ceilf(lineAscent / 2.0) + 2;
CGFloat height = ceilf(drawingRect.size.height - lineOrigin.y + descent + leading);
if ([[UIDevice currentDevice].systemVersion floatValue] &= 10.0) {
height += ceilf(descent);
CGPathRelease(path);
CFRelease(frameRef);
CFRelease(framesetter);
return CGSizeMake(drawingRect.size.width, height);
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
邮箱低至5折
推荐购买再奖现金,最高25%
&200元/3月起
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
iOS Context 基础教程 (三)完整实现UILabel末尾 实现『省略号相关信息,包括
的信息,所有iOS Context 基础教程 (三)完整实现UILabel末尾 实现『省略号相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
International

我要回帖

更多关于 iphone竖排锁定失效 的文章

 

随机推荐