Unwanted Vertical Padding from iOS 6 on CATextLayer

前端 未结 5 1108
半阙折子戏
半阙折子戏 2021-02-06 15:37

Background: I started my project in iOS 5 and built out a beautiful button with layer. I added a textLayer onto the button and center it using the following cod

5条回答
  •  忘掉有多难
    2021-02-06 16:04

    t0rst's answer helps me. I think capHeight and xHeight are key.

        CATextLayer *mytextLayer = [CATextLayer layer];
        CGFloat fontSize = 30;
        UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
        mytextLayer.font = (__bridge CFTypeRef)(boldFont.fontName);
        mytextLayer.fontSize = fontSize;
    
        CGFloat offsetY = 0;
    
        //if system version is grater than 6
        if(([[[UIDevice currentDevice] systemVersion] compare:@"6" options:NSNumericSearch] == NSOrderedDescending)){
            offsetY = -(boldFont.capHeight - boldFont.xHeight);
        }
    
        //you have to set textX, textY, textWidth
        mytextLayer.frame = CGRectMake(textX, textY + offsetY, textWidth, fontSize);
    

提交回复
热议问题