相关疑难解决方法(0)

核心文本比NSString drawInRect小一点:?

从理论上讲,这些应该是相同的大小,但它们不是:

蓝色文本来自Core Text,黑色来自-[NSString drawInRect:].这是代码:

//Some formatting to get the correct frame
int y = MESSAGE_FRAME.origin.y + 8;

    if (month) y = y + 27;

    int height = [JHomeViewCellContentView heightOfMessage:self.entry.message];

    CGRect rect = CGRectMake(MESSAGE_FRAME.origin.x + 8, y, MESSAGE_FRAME.size.width - 16, height);


    //Draw in rect method
    UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f];

    [[UIColor colorWithWhite:0.2 alpha:1.0] setFill];

    [self.entry.message drawInRect:rect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];



    //Frame change to suit CoreText
    CGRect rect2 = CGRectMake(MESSAGE_FRAME.origin.x + 8, self.bounds.size.height - y - height, MESSAGE_FRAME.size.width - 16, …
Run Code Online (Sandbox Code Playgroud)

core-graphics objective-c core-text ios

10
推荐指数
1
解决办法
1635
查看次数

CTLineGetTypographicBounds的问题

代码是从SimpleTextInput sampe代码中提取的,稍作修改.

创建框架:

self.font = [UIFont systemFontOfSize:18.f];
CTFontRef ctFont = CTFontCreateWithName((CFStringRef) self.font.fontName, self.font.pointSize, NULL);        
self.attributes = [[NSDictionary dictionaryWithObject:(id)ctFont forKey:(NSString *)kCTFontAttributeName] retain];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.text attributes:self.attributes];    

_framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);

// Create the Core Text frame using our current view rect bounds
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
_frame =  CTFramesetterCreateFrame(_framesetter, CFRangeMake(0, 0), [path CGPath], NULL);
Run Code Online (Sandbox Code Playgroud)

线下面是帧(由获得的任何线CTFrameGetLines(_frame)):

CGFloat ascent, descent, leading;
CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
CGPoint origin;
CTFrameGetLineOrigins(_frame, CFRangeMake(i, 1), &origin);
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 从CTLineGetTypographicBounds或ctFont获得的上升是13.860352,而self.font.ascender是16.860352.这3点差异来自哪里? …

core-text ios

6
推荐指数
1
解决办法
2874
查看次数

NSString boundingRectWithSize使用CoreText Framesetter缩短剪裁高度?iOS版

我有一个自定义的UIView,它通过CoreText绘制一个NSString:

- (NSMutableAttributedString *)getAttributedString : (NSString *)displayText {
string = [[NSMutableAttributedString alloc]
                                     initWithString:displayText];

helvetica = CTFontCreateWithName(CFSTR("Helvetica"), 20.0, NULL);

[string addAttribute:(id)kCTFontAttributeName
               value:(__bridge id)helvetica
               range:NSMakeRange(0, [string length])];

return string;
}

- (void)drawRect:(CGRect)rect
{

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(
                                                                       (__bridge CFAttributedStringRef)string);
// left column form
leftColumnPath = CGPathCreateMutable();
CGPathAddRect(leftColumnPath, NULL,
              CGRectMake(0, 0,
                         self.bounds.size.width,
                         self.bounds.size.height));

// left column frame
textleftFrame = CTFramesetterCreateFrame(framesetter,
                                     CFRangeMake(0, 0),
                                     leftColumnPath, NULL);

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // right column form
    rightColumnPath = CGPathCreateMutable();
    CGPathAddRect(rightColumnPath, NULL,
                  CGRectMake(self.bounds.size.width/2.0, 0,
                             self.bounds.size.width/2.0,
                             self.bounds.size.height));

    NSInteger …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsstring core-text ios

5
推荐指数
1
解决办法
5447
查看次数

标签 统计

core-text ×3

ios ×3

objective-c ×2

core-graphics ×1

iphone ×1

nsstring ×1