sizeToFit()与attributionText无法正常工作

时间:2016-05-07 14:22:17

标签: ios swift uitableview uilabel nsattributedstring

这是我的cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        //cell init
        //set values to cell property
        let str2 = NSString(format: "%@", (displayArray[indexPath.row].valueForKey("username") as? String)!)
        let attrStr2 = NSMutableAttributedString(string: str2 as String)
        attrStr2.addAttribute(NSFontAttributeName, value:UIFont(name: "HelveticaNeue-Bold", size: 12)!, range: NSMakeRange(0, str2.length ))
        topCell.username.attributedText = attrStr2

        topCell.username.sizeToFit()

        topCell.title.backgroundColor = UIColor.redColor()
        topCell.username.backgroundColor = UIColor.yellowColor()
        //some calculation which is not affecting username.frame.size.width
        return topCell;

}

这是我的tableView
enter image description here
你可以看到黄色背景的标签, 是的,autolayout是禁用的,所以我认为我的代码很好,但没有按预期工作。

请帮我解决这个问题。 提前致谢

0 个答案:

没有答案
相关问题