0
CoreText基礎からの属性付き文字列の行数が間違っています。私は、次のコードを使用して、属性付き文字列に存在する行数を抽出しました。CoreTextのattributedStringのLineCountが間違っています
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"\nMe and Me\n" attributes:nil];
CGPathRef path = CGPathCreateWithRect(CGRectMake(0, 0, 335, 1000000), nil);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributedString);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0), path, NULL);
NSLog(@"%ld", numberOfLines(frame));
}
static CFIndex numberOfLines(CTFrameRef frame) {
CGRect bounds = CGRectNull;
CFArrayRef lines = CTFrameGetLines(frame);
CFIndex numLines = CFArrayGetCount(lines);
CGPoint *lineOrigins = malloc((numLines + 1) * sizeof(CGPoint));
CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), lineOrigins);
return numLines;
}
私は、返される行数が3になると予想しました。 1つは最初の改行、もう1つは "Me and Me"というテキスト、最後の改行は1つです。代わりに2を返す理由は何ですか?
にとどまり、それは改行文字と三行、その後の代わりに、2だと指定する方法はありません? – MichaelGofron
3つの改行を使用してください: '@" \ nMeとMe \ n \ n " – CRD