matthew.kempson
matthew.kempson

Reputation: 1054

Height required for UILabel dependent on string length (Swift, iOS 8)

I am struggling to get the height that is required for a UILabel based on variable sized text that it could accept. From hours of research, I have not yet discovered a viable way of achieving this. Currently my code is as follows:

func getHeightForTitle(postTitle: NSString) -> CGFloat {
    // Get the height of the font
    let constraintSize = CGSizeMake(self.cellTextWidth, CGFloat.max)

    let attributes = [NSFontAttributeName: [UIFont.systemFontOfSize(16.0)]]
    let labelSize = postTitle.boundingRectWithSize(constraintSize,
        options: NSStringDrawingOptions.UsesLineFragmentOrigin,
        attributes: attributes,
        context: nil)

    return labelSize.height
}

This however throws the following error:

2014-08-02 12:09:37.370 Testing App[8365:351906] - [_TtCSs23_ContiguousArrayStorage00007FD26C15F708 pointSize]: unrecognized selector sent to instance 0x11e640050

This is always thrown at the let labelSize = postTitle... method and I believe it is down to the attributes variable. I however maybe wrong. Any help is appreciated, and much thanked!

Please note: This is for an iOS 8, Swift development project.

Upvotes: 1

Views: 5488

Answers (2)

mano
mano

Reputation: 47

Put the text into the label and call sizeThatFits() on the label with the desired width and large height.

sub_label=UILabel(frame: CGRectMake(0, 0, self.view.bounds.width, 50))

    sub_label.numberOfLines=0;
    sub_label.textAlignment=NSTextAlignment.Left

    sub_label.lineBreakMode=NSLineBreakMode.ByWordWrapping
    let subfont = UIFont(name: "Helvetica", size: 20.0)
    sub_label.font=subfont

    sub_label.text="his is just a load of texthis is just a load of texthis is just a load of texthis is just a load of texthis is just a load of texthis is just a load of text"

  sub_label.backgroundColor=UIColor.clearColor()

var textViewSizesub=sub_label.sizeThatFits(CGSizeMake(self.view.bounds.width, CGFloat.max))
sub_label.frame=CGRectMake(0, textViewSize.height, self.view.bounds.width-5, textViewSizesub.height)

Upvotes: 0

matt
matt

Reputation: 535222

Two observations. First, what's wrong with your code is that this line is not Swift:

let attributes = [NSFontAttributeName: [UIFont.systemFontOfSize(16.0)]]

The stuff after the equals sign is Objective-C, and Swift is having trouble interpreting it. Remove the square brackets from around the UIFont call; you have turned this into an array, which is the source of the error you're seeing.

Second, and more important, there are many much simpler ways to do this, by letting the label tell you its size for the desired text:

  • Put the text into the label and call sizeToFit() on the label.

  • Put the text into the label and call sizeThatFits() on the label with the desired width and large height.

  • Under auto layout, set the label's preferredMaxLayoutWidth to the desired width and put the text into the label; it will size itself.

However, I would urge you not to do this if you don't have to. A label is already self-sizing under auto layout, and in iOS 8 there's a new feature where a table cell will self-adjust its height to its contents, so there is now very rarely a need to pre-measure a label's dimensions.

Upvotes: 9

Related Questions