计算屏幕上字体的宽度

时间:2015-06-05 04:28:45

标签: ios objective-c nsstring

---更正:---

我使用了正确的方法NSString sizeWithAttributes  看来我的错误是使用了错误的属性。  FontDescriptor属性与此方法使用的大小属性无关。 我应该一直在使用这些属性...... Attributes

我的目标是iOS 8.我在使用OS X 10.10的MacBook上

我看过无数堆栈溢出问题,问这个问题,并没有找到一个真正有用的问题,所以我不确定他们为什么会竖起大拇指。我已经尝试了折旧方法和当前方法。我已尝试使用NSString sizeWithAttributes的所有类型的属性组合和排列。

我怎么知道它不起作用?因为当我使用像素测量工具时,它返回的宽度几乎不是屏幕上实际渲染的宽度。

此外,无论我使用什么字体大小,返回的宽度都完全相同。

这是一些代码,我用...调用我的大小调整方法。

NSString *theString = @"Whose Your Daddy?";

CGFloat theResult = [self widthOfText:theString usingFontSize:[NSNumber numberWithFloat:12]];
NSLog(@"12 size = %f",theResult);

CGFloat theResult2 = [self widthOfText:theString usingFontSize:[NSNumber numberWithFloat:18]];
NSLog(@"18 size = %f",theResult2);

CGFloat theResult3 = [self widthOfText:theString usingFontSize:[NSNumber numberWithFloat:24]];
NSLog(@"24 size = %f",theResult3);

这是评估上述代码的方法。在我尝试过的许多变种中,我留下了2个注释变体...

-(CGFloat)widthOfText:(NSString *)string usingFontSize:(NSNumber *)fontSize
{
    // UIFont *font = [UIFont systemFontOfSize:[fontSize floatValue]];
    // UIFont *font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:[fontSize floatValue]];
    NSDictionary *attributesDictionary = @{UIFontDescriptorFaceAttribute:@"HelveticaNeue-BoldItalic", UIFontDescriptorSizeAttribute:fontSize};

    CGSize stringSize = [string sizeWithAttributes:attributesDictionary];

    return stringSize.width;
}

由于

2 个答案:

答案 0 :(得分:2)

这里是我用来计算文字宽度的代码,你只需要传递文本,字体和支持的最大高度作为方法中的参数,它将返回完美的宽度值.. < / p>

- (CGFloat)findWidthForText:(NSString *)text havingMaximumHeight:(CGFloat)heightValue andFont:(UIFont *)font {
    CGSize size = CGSizeZero;
    if (text) {
#ifdef __IPHONE_7_0
        CGRect frame = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, heightValue) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName:font } context:nil];
        size = CGSizeMake(frame.size.width, frame.size.height + 1);
#else
        size = [text sizeWithFont:font constrainedToSize:CGSizeMake(CGFLOAT_MAX, heightValue) lineBreakMode:NSLineBreakByWordWrapping];
#endif
    }
    return size.width;
}

尝试这种方法..它对我有用.. :))

答案 1 :(得分:0)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *OptName_length = @" Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0]};
CGRect rect = [OptName_length boundingRectWithSize:CGSizeMake(tableView.bounds.size.width-150.0, MAXFLOAT)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];

CGSize requiredSize = rect.size;
return requiredSize.height +5.0f;
}

这是用于tableview高度你也可以从这里得到宽度... 根据需要更改CGSizeMake(tableView.bounds.size.width-150.0, MAXFLOAT)值。

高度 CGSizeMake(LablTextConstantWidth_InView, MAXFLOAT) // 50.0某个值

宽度 CGSizeMake(MAXFLOAT,LablTextConstantHeight_InView)

相关问题