iOS 7 sizeWithAttributes: replacement for sizeWithFont:constrainedToSize

前端 未结 9 1643
执笔经年
执笔经年 2020-11-28 01:31

How do you return a multiline text CGSize from the new iOS 7 method sizeWithAttributes?

I would like this to produce the same results as sizeWithFont:constrainedToSi

9条回答
  •  心在旅途
    2020-11-28 01:50

    [As a new user, I cannot post a comment to @testing's answer, but to make his answer (for xamarin.ios) more useful]

    We can return a CGRect and only use the height parameter for the gui item we are targeting UIButton etc.Passing in any parameters we need as below

        public CGRect GetRectForString(String strMeasure, int fontSize, nfloat guiItemWidth)
        {
            UIFont descriptionLabelFont =  UIFont.SystemFontOfSize (fontSize);
            NSString textToMeasure = (NSString)strMeasure;
    
            CGRect labelRect = textToMeasure.GetBoundingRect (
                new CGSize(guiItemWidth, nfloat.MaxValue), 
                NSStringDrawingOptions.UsesLineFragmentOrigin, 
                new UIStringAttributes () { Font = descriptionLabelFont }, 
                new NSStringDrawingContext ()
            );
    
            return labelRect;
        }
    

            header_Revision.Frame = new CGRect (5
                                                , verticalAdjust
                                                , View.Frame.Width-10
                                                , GetRectForString( header_Revision.Title(UIControlState.Normal)
                                                                  , 18
                                                                  , View.Frame.Width-10 ).Height
                                                );
    

提交回复
热议问题