简体   繁体   English

Xcode中字符的宽度(以像素为单位)

[英]width in pixels of a character in Xcode

I want to write a character to an iPad screen beginning at any pixel. 我想从任何像素开始将字符写入iPad屏幕。 The problem I'm having is determining the width of the last character so that I can position the next character. 我遇到的问题是确定最后一个字符的宽度,以便可以定位下一个字符。 This is the code I have. 这是我的代码。

NSString *mathText = @"#h23#D45 73#";
//const char *mathTextStr = "#F23#D45   73#";
UIFont * font = [UIFont fontWithName:@"Times" size:15];
CGContextSetTextPosition( myContext, 100, 100 );
for (int i = 0; i < mathText.length; i++) {
    len = 1;
    NSString *subString = [mathText substringWithRange:NSMakeRange(i, 1)];
    const char *oneChar = [subString UTF8String];
    //CGContextShowTextAtPoint(myContext, x, y,oneChar , 1);
    CGContextShowText(myContext, oneChar, 1);
    CGSize charWidth = [subString sizeWithFont:font];

    x=x+charWidth.width+3;
     NSLog(@"x coordinate = %f", x);
    CGContextSetTextPosition( myContext, x, y );
    }

The problem is the characters seems to be with CGSize charWidth. 问题在于字符似乎与CGSize charWidth有关。

Any help appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

create a category for UILabel with this function: 使用以下功能为UILabel创建类别:

-(CGSize)expectedLabelSize{
    return [self.text sizeWithFont:self.font
                 constrainedToSize:self.frame.size
                     lineBreakMode:self.lineBreakMode];
}

call it like: [myLabel expectedLabelSize]; 像这样调用:[myLabel ExpectedLabelSize];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM