ios - 以编程方式确定渲染的 NSString 尺寸和偏移量

标签 ios objective-c uilabel uifont

尽可能直接地和尽可能多地提出这个问题,以便给出明确的答案。竟然还有图!

我有一个 UILabel 的简单子(monad)类(子类化以允许渲染描边文本)。

标题:

//  svCustomNumeralLabel.h

#import <UIKit/UIKit.h>

@interface svCustomNumeralLabel : UILabel

@property UIFont *customFont;
@property CGPoint drawPoint;

@end

完整实现:
#import "svCustomNumeralLabel.h"

@implementation svCustomNumeralLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}


-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}


-(void)initialize{
    self.customFont =  [UIFont fontWithName:@"Forgotten Futurist" size:200.0];
    self.drawPoint = CGPointMake(-10.0,-39.0);

    float capHeight = self.customFont.capHeight;
    NSLog(@"Cap height: %f", capHeight);
}


- (void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);//RGBA
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
    CGContextSetTextDrawingMode(context, kCGTextFillStroke);

    float descender = self.customFont.descender;
    float ascender = self.customFont.ascender;

    NSLog(@"Ascender: %f descender: %f", ascender, descender);

    CGSize frameSize = self.frame.size;
    NSLog(@"Frame width: %f height: %f", frameSize.width, frameSize.height);

    CGSize unDrawnSize = [self.text sizeWithFont:self.customFont];
    NSLog(@"Undrawn width: %f height: %f", unDrawnSize.width, unDrawnSize.height);

    CGSize drawnSize = [self.text drawAtPoint:self.drawPoint withFont:self.customFont];
    NSLog(@"Drawn width: %f height: %f", drawnSize.width, drawnSize.height);

}

我已将此类的一个实例放入 UIView在界面生成器中。 svCustomNumeralLabel的 View 已在 IB 中手动调整为 80.0 宽、127.0 高。

我设置了 self.drawPoint 的值在 initialize()通过反复试验的方法,将其当前值 (-10.0, -39.0) .

结果,通过这些手动设置和硬连线的值,是一个很好的数字,准确地定位在它的容器框架内(为了清楚起见,它被设置为灰色背景):svCustomNumeralLabel screengrab
NSLog上面运行的输出:
Cap height: 126.600006
Ascender: 165.601562 descender: -58.400002
Frame width: 80.000000 height: 127.000000
Undrawn width: 100.000000 height: 229.000000
Drawn width: 99.200005 height: 229.000000

我的问题

我怎么能以编程方式确定 self.drawPoint 的值来实现这个结果,即在左上角呈现的“0”字符?

第二个问题:我怎样才能为字符的精确宽度和高度计算一个正确的值,就像实际渲染的那样,以方便在它的 View 中很好地贴合它?

NSLog 可以看出输出,似乎很少有可用的信息与绘制的实际数字非常相似。与框架尺寸相关的唯一指标是 UIFont.capHeight .似乎没有我可以可靠地用来确定 (-10.0, -39.0) 的信息。 self.drawPoint 的偏移值.

最佳答案

AppKit 为 Foundation Framework 的 NSString 提供了一些不错的补充。 : NSString UIKit Additions

特别感兴趣:- (CGSize)sizeWithFont:(UIFont *)font及相关方法。

关于ios - 以编程方式确定渲染的 NSString 尺寸和偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16043293/

相关文章:

iphone - TableView 组标题 View

iphone - CATransform3DRotate 旋转 360 度

ios - boundingRectWithSize为UILabel添加了额外的底部填充

xcode - 为什么我不能将值发送到另一个 ViewController 上的 UILabel?

iphone - iOS如何确定收到的通知是针对MDM的?

iphone - IOS - 意外崩溃,没有崩溃信息

ios - 接受电子邮件地址和 10 位电话号码的正则表达式

ios - UIButton UILabel 背景 View 不采用传递的背景颜色

ios - 无法在 Golang 中解析来自 iOS 的 base64 DER 编码的 ASN.1 公钥

ios - UILabel 上的 UIImage