迅速:以编程方式创建UILabel固定宽度,该宽度根据文本长度垂直调整 [英] swift: programmatically create UILabel fixed width that resizes vertically according to text length

查看:65
本文介绍了迅速:以编程方式创建UILabel固定宽度,该宽度根据文本长度垂直调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了涉及自动布局的垂直大小调整的答案,但是我正在创建的 UILabel 仅在运行时需要.(我可能需要从零到许多这样的标签.)

示例(忽略颜色)

  1. 短文本(注意与长文本相同的宽度):

  1. 较长的文本(注意与较短的文本示例具有相同的宽度,但添加文本的行数更多):

如果文本可以放在固定宽度的一行中,则标签不需要垂直调整大小.但是,如果有更多字符,则标签应保持垂直扩展以适合这些其他字符.文本应连续换行.文本应从标签的左上角开始.

更具体地说:

 让标记= GMSMarker(位置:myLatLng)//有关imageWithView的信息,请参见http://stackoverflow.com/a/40211383/1168364marker.icon = imageWithView(label)//**如何创建此标签?**marker.map =地图//地图是GMSMapView 

这些标签可以在屏幕上的任何位置.这是针对将每个标签放置在随机位置的地图应用程序.标签的位置彼此无关.

解决方案

UIView有两种有用的方法:

P.S.您也可以使用自动布局约束来解决问题,但是我不太喜欢以编程方式使用它们.

I've seen answers to vertical resizing that involve autolayout, but the UILabels I'm creating are only needed at runtime. (I might need anywhere from zero to many of these labels.)

Examples (ignore the color)

  1. Short text (note same width as longer text):

  1. Longer text (note same width as shorter text example with more lines for add'l text):

If the text can fit in one line of fixed width, the label shouldn't need to resize vertically. But if there are more characters, the label should keep expanding vertically to fit these additional characters. The text should keep wrapping around line after line. The text should start in the top left corner of the label.

To be more specific:

let marker = GMSMarker(position: myLatLng)
// see http://stackoverflow.com/a/40211383/1168364 for imageWithView
marker.icon = imageWithView(label) // **how do i create this label?**
marker.map = map // map is a GMSMapView

These labels can be anywhere on the screen. This is for a map application where each label will be placed at a random location. The labels' locations have no relationship to one another.

解决方案

There are two usefull methods of UIView: sizeToFit() and sizeThatFits(_:)

The first one resizes a view to a minimal size to fit subviews' content and the second one doesn't change frame at all, but returns calculated size which: (1) fit all subviews and (2) doesn't exceed parameter size

So you can use sizeThatFits for you purpose:

let label = UILabel()

override func viewDidLoad() {
    super.viewDidLoad()

    label.backgroundColor = UIColor.orange
    label.textColor = UIColor.white
//  label.text = "ultimate Frisbee"
    label.text = "ultimate Frisbee\nin 3 minutes,\nall welcome|2"
    label.numberOfLines = 10
    view.addSubview(label)

    updateLabelFrame()
}

func updateLabelFrame() {
    let maxSize = CGSize(width: 150, height: 300)
    let size = label.sizeThatFits(maxSize)
    label.frame = CGRect(origin: CGPoint(x: 100, y: 100), size: size)
}

Output:

P.S. You also can solve your problem with autolayout constraints, but I am not a big fan of using them programmatically.

这篇关于迅速:以编程方式创建UILabel固定宽度,该宽度根据文本长度垂直调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆