制作多行 UILabel 从顶部而不是中间开始文本
我对多行 UILabel 有一个小问题,我的 UILabel 文本奇怪地从中间开始,当新行到达时它会上升,因此最后一行始终位于中间。我希望它的行为像普通的文本视图一样,从顶部开始,各行在彼此下方,第一行保持在顶部。抱歉,如果我解释得不好,如果需要的话我可以尝试详细说明!提前致谢!
I have a little problem with multiline UILabel, my UILabel text like starts from the middle strangely and it goes up when new lines arrive so that the last line is on the middle always. I want it to behave like a normal textview, starting from top and lines going under each other, first line staying on top. Sorry if I explained it badly, I can try to elaborate if needed! Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 NSString 上的 sizeWithFont:constrainedToSize:lineBreakMode: 方法来计算给定字体和约束宽度的文本块的高度。然后,您将更新标签的框架,使其足够大以包含文本。
You can use the
sizeWithFont:constrainedToSize:lineBreakMode:
method on NSString to figure out the height of a block of text given a font and a constrained width. You would then update the frame of your label to be just large enough to encompass the text.由于 sizeWithFont 方法在 iOS 7.0 + 中已弃用,因此您可以使用名为 boundingRectWithSize 的替代方法。
例如:
Since sizeWithFont method is decprecated in iOS 7.0 +, you can use a alternative method named boundingRectWithSize.
For Example: