如何根据iOS中的文本长度更改UILabel宽度?

iOS*_*oob 4 iphone objective-c uikit uilabel ios

我想在'UILabel'旁边显示一个图像,但是'UILabel'具有可变的文本长度,所以我不知道在哪里放置图像.

图像应根据标签的大小移动.

我怎么能做到这一点?

the*_*ude 5

虽然Keshav的答案会起作用,但它已被弃用

尝试:

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14]};

CGRect rect = [textToMeasure boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:attributes
                                          context:nil];
Run Code Online (Sandbox Code Playgroud)

然后使用rect的大小来确定您的定位和标签大小.

CGRect currentLabelFrame = self.label.frame;

currentLabelFrame.size.width = rect.size.width;

self.label.frame = currentLabelFrame;
Run Code Online (Sandbox Code Playgroud)