BoundingRectWithSize - Choosing Font & Font Size Used

时间:2015-07-28 16:39:37

标签: ios swift fonts uibutton uilabel

I am using BoundingRectWithSize to get back a CGRect that will appropriately fix my text. I previously had this code.

var maximumLabelSize: CGSize = CGSize(width: view.bounds.width-5, height: 150)
         var maximumLabelSize: CGSize = CGSize(width: view.bounds.width-5, height: 150)
    var labelRect: CGRect = (matches[indexPath.item].name as NSString).boundingRectWithSize(maximumLabelSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(16)], context: nil)

When changing the font to HelviticaNeue-Bold many of my labels were not displaying their full screens. My hypothesis was that since I changed the font to bold the text could no longer fit within their bounding CGRects and as a result cutting the strings short.

My proposed fix was to edit the code that created the initial appropriate bounding CGRect for my text to take into account the bold nature of the text. This was my attempt.

var labelRect: CGRect = (matches[indexPath.item].name as NSString).boundingRectWithSize(maximumLabelSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Bold", size: 16)], context: nil)

I am getting an error that my supplied arguments are incorrect.

Cannot invoke 'boundingRectWithSize' with an argument list of type '(CGSize, options: NSStringDrawingOptions, attributes: [String : UIFont?], context: nil)'

According to the iOS Developer Library these are the appropriate parameters.

func boundingRectWithSize(_ size: CGSize,
                  options options: NSStringDrawingOptions,
               attributes attributes: [String : AnyObject]?,
                  context context: NSStringDrawingContext?) -> CGRect

The conflict is within attributes, obviously the area I changed, but at least this confirms it. I am unsure of how to pursue the solution any further. Any help understanding this would be well appreciated.

2 个答案:

答案 0 :(得分:2)

在进一步分析我自己提供的boundingRectWithSize参数与iOS Developer Library中列出的参数之间的对比之后,我想知道如果我将UIFont转换为AnyObject会发生什么。

var labelRect: CGRect = (matches[indexPath.item].name as NSString).boundingRectWithSize(maximumLabelSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Bold", size: 16) as! AnyObject], context: nil)

boundingRectWithSize的attributes参数接受类型[String:AnyObject],而我应该提供[String:UIFont?]。我铸造了我的UIFont?到AnyObject。这使错误沉默,也解决了我的问题。粗体文字不再缩短。解决。

答案 1 :(得分:0)

UIFont(name, size) has an "Optional" output. And, boundingRectWithSize does not accept the optional output of UIFont with a name. Try this: it works.

if let font = UIFont(name: "HelveticaNeue-Bold", size: 16) {
        var labelRect2: CGRect = ("Random String" as NSString).boundingRectWithSize(maximumLabelSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)

}