比较来自世界各地的卖家的域名和 IT 服务价格

计算文本的大小 UILabel

我在画画
UILabels

软件。 他们从数据库中获得尺寸。 所以我不能只是使用
sizeToFit

. 我已经实现了一个redraws的函数
UILabels

具有传输系数。 所以我需要找到的一切 - 这是文字B.
UILabel

从我的角度来看,这将需要重新绘制的最大比率
UILabels

.
所以,最后,我需要做这样的事情:


double ratio = 1.00;
for /UILabel* labels in sec.subviews/ {

float widthLabel = labels.frame.size.width;
float heightLabel = labels.frame.size.height;
float heightText = //get the text height here
float widthText = //get the text width here
if /widthLabel < widthText/ {
ratio = MAX/widthText/widthLabel,ratio/;
}
if /heightLabel < heightText/ {
ratio = MAX/heightText/heightLabel, ratio/;
}
}
//redraw UILabels with the given ratio here


那么,我如何获得文本的高度和宽度的大小,因为我的一些文本不适合标签,我不能只使用标签边界? 我用 Xcode 5 和 iOS 7.
已邀请:

裸奔

赞同来自:

所有方法
[NSString sizeWithFont...]

过时的B. iOS 7. 使用它。


CGRect labelRect = [text
boundingRectWithSize:labelSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{
NSFontAttributeName : [UIFont systemFontOfSize:14]
}
context:nil];



https://developer.apple.com/do ... hfont
.

更新示例输出 boundingRectWithSize

根据您的评论,我做了一个简单的测试。 代码和输出如下所示。


// code to generate a bounding rect for text at various font sizes
NSString *text = @"This is a long sentence. Wonder how much space is needed?";
for /NSNumber *n in @[@/12.0f/, @/14.0f/, @/18.0f/]/ {
CGFloat fontSize = [n floatValue];
CGRect r = [text boundingRectWithSize:CGSizeMake/200, 0/
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}
context:nil];
NSLog/@"fontSize = %f\tbounds = /%f x %f/",
fontSize,
r.size.width,
r.size.height/;
}


这导致以下结果。 /请注意,正如字体大小的增加,边界更改/:


fontSize = 12.000000 bounds = /181.152008 x 28.632000/
fontSize = 14.000000 bounds = /182.251999 x 50.105999/
fontSize = 18.000000 bounds = /194.039993 x 64.421997/

裸奔

赞同来自:

长度接收字符数。 如果要获取文本宽度:

Objective-C


CGSize textSize = [label.text sizeWithAttributes:@{NSFontAttributeName:[label font]}];


Swift 4


let size = label.text?.size/withAttributes: [.font: label.font]/ ?? .zero


它给了你尺寸。 你可以比较
textSize.width

每个标签。

裸奔

赞同来自:

另一种简单的方法,我没有提到?


CGSize textSize = [label intrinsicContentSize];


/当然,只有在安装文本和字体标签后才能正确工作。/

詹大官人

赞同来自:

这是一个选择 swift.


let font = UIFont/name: "HelveticaNeue", size: 25/!
let text = "This is some really long text just to test how it works for calculating heights in swift of string sizes. What if I add a couple lines of text?"

let textString = text as NSString

let textAttributes = [NSFontAttributeName: font]

textString.boundingRectWithSize/CGSizeMake/320, 2000/, options: .UsesLineFragmentOrigin, attributes: textAttributes, context: nil/

快网

赞同来自:

小建议伙计们,如果你,喜欢我,用途
boundingRectWithSize


[UIFont systemFontOFSize:14]


如果你的行有两行的长度,那么返回高度 rect 等于大约 33,4 观点。

不要像我一样犯错,把它扔进去
int

, 因为 33,4 成为 33, 和 33 标签高度点从两个到一行!

郭文康

赞同来自:

一个问题


CGRect r = [text boundingRectWithSize:CGSizeMake/200, 0/
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}
context:nil];


这是
boundingRectWithSize

, 这决定了可能具有的最大值 CGRect.

我解决这个问题的解决方案是检查它是否超过了,那么文本可以适合标签。 我在循环的帮助下做到了。


NSString *text = @"This is a long sentence. Wonder how much space is needed?";
CGFloat width = 100;
CGFloat height = 100;
bool sizeFound = false;
while /!sizeFound/ {
NSLog/@"Begin loop"/;
CGFloat fontSize = 14;
CGFloat previousSize = 0.0;
CGFloat currSize = 0.0;
for /float fSize = fontSize; fSize < fontSize+6; fSize++/ {
CGRect r = [text boundingRectWithSize:CGSizeMake/width, height/
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fSize]}
context:nil];
currSize =r.size.width*r.size.height;
if /previousSize >= currSize/ {
width = width*11/10;
height = height*11/10;
fSize = fontSize+10;
}
else {
previousSize = currSize;
}
NSLog/@"fontSize = %f\tbounds = /%f x %f/ = %f",
fSize,
r.size.width,
r.size.height,r.size.width*r.size.height/;
}
if /previousSize == currSize/ {
sizeFound = true;
}

}
NSLog/@"Size found with width %f and height %f", width, height/;


在每次迭代后,高度和宽度的尺寸增加 10% 你的价值。

我选择的原因 6, 正是我不希望标签太软。

解决不使用周期:


NSString *text = @"This is a long sentence. Wonder how much space is needed?";
CGFloat width = 100;
CGFloat height = 100;

CGFloat currentFontSize = 12;
CGRect r1 = [text boundingRectWithSize:CGSizeMake/width, height/
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:currentFontSize+6]}
context:nil];

CGRect r2 = [text boundingRectWithSize:CGSizeMake/width, height/
options:NSStringDrawingUsesFontLeading
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:currentFontSize+6]}
context:nil];

CGFloat firstVal =r1.size.width*r1.size.height;
CGFloat secondVal =r2.size.width*r2.size.height;

NSLog/@"First val %f and second val is %f", firstVal, secondVal/;

if /secondVal > firstVal/ {
float initRat = secondVal/firstVal;

float ratioToBeMult = sqrtf/initRat/;

width *= ratioToBeMult;
height *= ratioToBeMult;
}

NSLog/@"Final width %f and height %f", width, height/;

//for verifying
for /NSNumber *n in @[@/12.0f/, @/14.0f/, @/17.0f/]/ {
CGFloat fontSize = [n floatValue];
CGRect r = [text boundingRectWithSize:CGSizeMake/width, height/
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}
context:nil];
NSLog/@"fontSize = %f\tbounds = /%f x %f/ = %f",
fontSize,
r.size.width,
r.size.height,r.size.width*r.size.height/;
firstVal =r.size.width*r.size.height;
}


最后一个周期证明更大的字体可以给出更高尺寸的结果。

江南孤鹜

赞同来自:

适用于多线标签的解决方案 /Swift 4/, 要计算固定宽度的高度:


let label = UILabel/frame: .zero/
label.numberOfLines = 0 // multiline
label.font = UIFont.systemFont/ofSize: UIFont.labelFontSize/ // your font
label.preferredMaxLayoutWidth = width // max width
label.text = "This is a sample text.\nWith a second line!" // the text to display in the label

let height = label.intrinsicContentSize.height

窦买办

赞同来自:

使用此代码字符串,我们可以在标签上获取文本大小。


let str = "Sample text"
let size = str.sizeWithAttributes/[NSFontAttributeName:UIFont.systemFontOfSize/17.0/]/


因此,我们可以使用宽度和高度。

帅驴

赞同来自:

msgStr 字符串变大 :


let msgStr:NSString = Data["msg"]! as NSString
let messageSize = msgStr.boundingRect/with: CGSize/width: ChatTable.frame.width-116, height: CGFloat.infinity/, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont/name: "Montserrat-Light", size: 14/!], context: nil/.size

三叔

赞同来自:

Swift 3.0


func getLabelHeight// -> CGFloat {
let font = UIFont/name: "OpenSans", size: 15/!
let textString = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." as NSString

let textAttributes = [NSFontAttributeName: font]

let rect = textString.boundingRect/with: CGSize/width: 320, height: 2000/, options: .usesLineFragmentOrigin, attributes: textAttributes, context: nil/
return rect.size.height
}

要回复问题请先登录注册