begin until end of selected text size

Discussion in 'iOS Programming' started by nxcoder, Dec 29, 2012.

  1. nxcoder macrumors newbie

    Joined:
    Dec 29, 2012
    #1
    hello

    i want to get the size (width and height) of between begin and end of selected text . see this :

    [​IMG]

    i am tried this code :

    Code:
    CGSize size=[myText sizeWithFont:txt.font];
    but not worked . because i have not font informations.
    Suppose that i have not the font size and font name of my text
    for example , the height of the selected texts is 250 pixel .
    help please

    thanks
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    What is your end goal for the information you seek?

    The method you are using only calculates the size if the text was on a single line. You need a method that returns the size for a multiline layout.

    I've had some recent good luck with the sizeWithFont:constrainedToSize:lineBreakMode: method. This may be useful for you but it depends on your end goal. Tip: I set the height for the constrainedToSize value greater than I expect to be using.

    You seem to be saying that you do not have the font information. The font information you are using is the txt.font property. So I don't know why you would be saying you don't have the information.

    Here is a sample using the above method:
    Code:
    UIFont * myfont = [UIFont systemFontOfSize: 24.0];
    NSString mystring = @"Some long string.";
    CGSize aBoundingBox = {125, 500}; // Height greater than expected
    CGSize boundingSize = [mystring sizeWithFont: myfont constrainedToSize: aBoundingBox lineBreakMode: NSLineBreakByWordWrapping];
    
     
  3. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #3
    if your on iOS6 then UITextView conforms to UITextInputProtocol. One of the required methods of the protocol is
    Code:
    - (NSArray *)selectionRectsForRange:(UITextRange *)range
    .

    IS that the sort of information your looking for?
     

Share This Page