核心文本具有不同高度的多列
我需要创建一个视图,该视图在列中呈现文本,其第一列高度比其余列短,并且所有列都位于底部。当我尝试更改第一个列的高度时,所有列都会缩小到相同的大小并在顶部对齐。如果我尝试为 y 值添加负数,它会降低列,但会切断矩形底部的一些文本。任何帮助将不胜感激。
这是代码:
#import "StoryCoreTextView.h"
#import <CoreText/CoreText.h>
@implementation StoryCoreTextView
@synthesize story;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
NSString *myLongText = [self getLongText]; /* ... */
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:myLongText];
// make a few words bold
CTFontRef georgia = CTFontCreateWithName(CFSTR("Georgia"), 14.0, NULL);
CTFontRef georgiaBold = CTFontCreateWithName(CFSTR("Georgia-Bold"), 14.0, NULL);
[string addAttribute:(id)kCTFontAttributeName
value:(id)georgia
range:NSMakeRange(0, [string length])];
range:NSMakeRange(223, 6)];
// create paragraph style and assign text alignment to it
CTTextAlignment alignment = kCTJustifiedTextAlignment;
CTParagraphStyleSetting _settings[] = { {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment} };
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(_settings, sizeof(_settings) / sizeof(_settings[0]));
// set paragraph style attribute
CFAttributedStringSetAttribute(string, CFRangeMake(0, CFAttributedStringGetLength(string)), kCTParagraphStyleAttributeName, paragraphStyle);
// layout master
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
// column 1 form
CGMutablePathRef column1Path = CGPathCreateMutable();
CGPathAddRect(column1Path, NULL,
CGRectMake(10, -200, self.bounds.size.width / 6.0 - 20, self.bounds.size.height - 200));
// column 1 frame
CTFrameRef column1Frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), column1Path, NULL);
// column 2 form
CGMutablePathRef column2Path = CGPathCreateMutable();
CGPathAddRect(column2Path, NULL,
CGRectMake(self.bounds.size.width/6.0 + 10, 10,
self.bounds.size.width/6.0 - 20,
self.bounds.size.height - 20));
NSInteger colum2Start = CTFrameGetVisibleStringRange(column1Frame).length;
// column 2 frame
CTFrameRef column2Frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(colum2Start - 250, 0), column2Path, NULL);
// column 3 form
CGMutablePathRef column3Path = CGPathCreateMutable();
CGPathAddRect(column3Path, NULL,
CGRectMake(2 * self.bounds.size.width/6.0+10, 10,
self.bounds.size.width/6.0 -20,
self.bounds.size.height-20));
NSInteger colum3Start = CTFrameGetVisibleStringRange(column2Frame).length;
// column 3 frame
CTFrameRef column3Frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(colum3Start, 0), column3Path, NULL);
// column 4 form
CGMutablePathRef column4Path = CGPathCreateMutable();
CGPathAddRect(column4Path, NULL,
CGRectMake(3 *self.bounds.size.width/6.0+10, 10,
self.bounds.size.width/6.0-20,
self.bounds.size.height-20));
NSInteger colum4Start = CTFrameGetVisibleStringRange(column3Frame).length;
// column 4 frame
CTFrameRef column4Frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(colum4Start, 0), column4Path, NULL);
// flip the coordinate system
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// draw
CTFrameDraw(column1Frame, context);
CTFrameDraw(column2Frame, context);
CTFrameDraw(column3Frame, context);
CTFrameDraw(column4Frame, context);
CTFrameDraw(column5Frame, context);
CTFrameDraw(column6Frame, context);
// cleanup
CFRelease(column1Frame);
CGPathRelease(column1Path);
CFRelease(column2Frame);
CGPathRelease(column2Path);
CFRelease(column3Frame);
CGPathRelease(column3Path);
CFRelease(column4Frame);
CGPathRelease(column4Path);
CFRelease(framesetter);
CFRelease(georgia);
CFRelease(georgiaBold);
[string release];
}
- (void)dealloc {
[super dealloc];
}
- (NSString *)getLongText {
NSString *longText = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum facilisis imperdiet sem, eu fermentum metus rhoncus ac. Nunc luctus ligula at erat varius at laoreet dolor iaculis. Nulla facilisi. Vivamus vestibulum massa tristique magna convallis ut suscipit dolor aliquet. Aliquam mollis porttitor tortor a venenatis. In tincidunt ornare posuere. In varius augue ut orci interdum in hendrerit tortor volutpat. Integer nec libero vel eros dignissim porta in nec nibh. Aenean sollicitudin justo vel augue pulvinar tincidunt. Phasellus dictum convallis dui in semper. Nam eros leo, dapibus eu porta non, aliquet id velit. Integer felis nisl, fermentum facilisis rutrum eget, consequat non nisl.";
return longText;
}
@end
I need to create a view that renders text in columns with a first column height that is shorter than the rest of the columns and all of the columns are even at the bottom. When I try to change the first column height all of the columns shrink to the same size and are aligned at the top. If I try to add negative numbers for the y value it lowers the column but cuts off some of the text at the bottom of the rect. Any help would be greatly appreciated.
Here is the code:
#import "StoryCoreTextView.h"
#import <CoreText/CoreText.h>
@implementation StoryCoreTextView
@synthesize story;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
NSString *myLongText = [self getLongText]; /* ... */
NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:myLongText];
// make a few words bold
CTFontRef georgia = CTFontCreateWithName(CFSTR("Georgia"), 14.0, NULL);
CTFontRef georgiaBold = CTFontCreateWithName(CFSTR("Georgia-Bold"), 14.0, NULL);
[string addAttribute:(id)kCTFontAttributeName
value:(id)georgia
range:NSMakeRange(0, [string length])];
range:NSMakeRange(223, 6)];
// create paragraph style and assign text alignment to it
CTTextAlignment alignment = kCTJustifiedTextAlignment;
CTParagraphStyleSetting _settings[] = { {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment} };
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(_settings, sizeof(_settings) / sizeof(_settings[0]));
// set paragraph style attribute
CFAttributedStringSetAttribute(string, CFRangeMake(0, CFAttributedStringGetLength(string)), kCTParagraphStyleAttributeName, paragraphStyle);
// layout master
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
// column 1 form
CGMutablePathRef column1Path = CGPathCreateMutable();
CGPathAddRect(column1Path, NULL,
CGRectMake(10, -200, self.bounds.size.width / 6.0 - 20, self.bounds.size.height - 200));
// column 1 frame
CTFrameRef column1Frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), column1Path, NULL);
// column 2 form
CGMutablePathRef column2Path = CGPathCreateMutable();
CGPathAddRect(column2Path, NULL,
CGRectMake(self.bounds.size.width/6.0 + 10, 10,
self.bounds.size.width/6.0 - 20,
self.bounds.size.height - 20));
NSInteger colum2Start = CTFrameGetVisibleStringRange(column1Frame).length;
// column 2 frame
CTFrameRef column2Frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(colum2Start - 250, 0), column2Path, NULL);
// column 3 form
CGMutablePathRef column3Path = CGPathCreateMutable();
CGPathAddRect(column3Path, NULL,
CGRectMake(2 * self.bounds.size.width/6.0+10, 10,
self.bounds.size.width/6.0 -20,
self.bounds.size.height-20));
NSInteger colum3Start = CTFrameGetVisibleStringRange(column2Frame).length;
// column 3 frame
CTFrameRef column3Frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(colum3Start, 0), column3Path, NULL);
// column 4 form
CGMutablePathRef column4Path = CGPathCreateMutable();
CGPathAddRect(column4Path, NULL,
CGRectMake(3 *self.bounds.size.width/6.0+10, 10,
self.bounds.size.width/6.0-20,
self.bounds.size.height-20));
NSInteger colum4Start = CTFrameGetVisibleStringRange(column3Frame).length;
// column 4 frame
CTFrameRef column4Frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(colum4Start, 0), column4Path, NULL);
// flip the coordinate system
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// draw
CTFrameDraw(column1Frame, context);
CTFrameDraw(column2Frame, context);
CTFrameDraw(column3Frame, context);
CTFrameDraw(column4Frame, context);
CTFrameDraw(column5Frame, context);
CTFrameDraw(column6Frame, context);
// cleanup
CFRelease(column1Frame);
CGPathRelease(column1Path);
CFRelease(column2Frame);
CGPathRelease(column2Path);
CFRelease(column3Frame);
CGPathRelease(column3Path);
CFRelease(column4Frame);
CGPathRelease(column4Path);
CFRelease(framesetter);
CFRelease(georgia);
CFRelease(georgiaBold);
[string release];
}
- (void)dealloc {
[super dealloc];
}
- (NSString *)getLongText {
NSString *longText = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum facilisis imperdiet sem, eu fermentum metus rhoncus ac. Nunc luctus ligula at erat varius at laoreet dolor iaculis. Nulla facilisi. Vivamus vestibulum massa tristique magna convallis ut suscipit dolor aliquet. Aliquam mollis porttitor tortor a venenatis. In tincidunt ornare posuere. In varius augue ut orci interdum in hendrerit tortor volutpat. Integer nec libero vel eros dignissim porta in nec nibh. Aenean sollicitudin justo vel augue pulvinar tincidunt. Phasellus dictum convallis dui in semper. Nam eros leo, dapibus eu porta non, aliquet id velit. Integer felis nisl, fermentum facilisis rutrum eget, consequat non nisl.";
return longText;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我宁愿建议您从多个矩形创建路径:
I'd rather suggest you to make path from multiple rectangles: