自动调整UILabel的大小

时间:2014-08-24 14:15:34

标签: ios swift uilabel uicollectionview

在Xcode 6 Beta 5中,我有一个看起来像iOS 7消息应用程序的聊天界面,其中文本内部的UILabel大小与文本本身的宽度相同。当我更新到Beta 6时,我注意到界面构建器中的UILabel选项,我之前没有注意到:

enter image description here

当我设置了明确的宽度时,宽度根据文本的宽度完全没有变化。当我取消选中显式时,文本的宽度至少为234,因此它会扩展到视图之外。

我在UIViewController中使用UICollectionView,这里是索引路径方法的项目单元格:

func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {

    let defaults = NSUserDefaults.standardUserDefaults()
    let row = indexPath.row

    var cell: UICollectionViewCell

    let path = UIBezierPath()

    let object: AnyObject = (messages[row] as NSDictionary).objectForKey("user_id")!
    let uid: AnyObject = defaults.objectForKey("user_id")!

    if "\(object)" == "\(uid)" {
        cell = collectionView.dequeueReusableCellWithReuseIdentifier(right_chat_bubble, forIndexPath: indexPath) as UICollectionViewCell

        path.moveToPoint(CGPointMake(0, 0))
        path.addLineToPoint(CGPointMake(0, 10))
        path.addLineToPoint(CGPointMake(12, 5))
        path.addLineToPoint(CGPointMake(0, 0))
    }
    else {
        cell = collectionView.dequeueReusableCellWithReuseIdentifier(left_chat_bubble, forIndexPath: indexPath) as UICollectionViewCell

        path.moveToPoint(CGPointMake(0, 5))
        path.addLineToPoint(CGPointMake(12, 10))
        path.addLineToPoint(CGPointMake(12, 0))
        path.addLineToPoint(CGPointMake(0, 5))
    }

    let initial_view = cell.viewWithTag(101) as UILabel
    initial_view.layer.cornerRadius = 20
    initial_view.layer.masksToBounds = true

    let name = (messages[row] as NSDictionary).objectForKey("name")! as String
    let name_array = name.componentsSeparatedByString(" ")
    let first_initial = name_array[0]
    let last_initial = name_array[1]

    let first_char = first_initial[0]
    let last_char = last_initial[0]
    let initials = first_char + last_char

    initial_view.text = initials

    let circle: UIView = cell.viewWithTag(103)! as UIView

    let mask = CAShapeLayer()
    mask.frame = circle.bounds
    mask.path = path.CGPath

    circle.layer.mask = mask

    let message = cell.viewWithTag(102) as ChatLabel
    message.enabledTextCheckingTypes = NSTextCheckingType.Link.toRaw()
    message.delegate = self
    message.text = (messages[row] as NSDictionary).objectForKey("content")! as String
    message.layer.cornerRadius = 15
    message.layer.masksToBounds = true

    message.userInteractionEnabled = true

    return cell
}

1 个答案:

答案 0 :(得分:0)

看看sizeThatFits:和sizeToFit:

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIView_class/index.html#//apple_ref/occ/instm/UIView/sizeThatFits

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIView_class/index.html#//apple_ref/occ/instm/UIView/sizeToFit

UILabel可以调用sizeThatFits,如:

myLabelLbl.text = @"some text"
CGSize maximumLabelSize = CGSizeMake(200, 800)
CGSize expectedSize = [myLabelLbl sizeThatFits:maximumLabelSize]
myLabelLbl.frame = CGRectMake(0, 0, expectedSize.width, expectedSize.height) //set the new size