Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I wonder if you could use the methods in the <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html" rel="noreferrer">NSString UIKit Additions</a> to figure out how much fits into label A.</p> <p>A crude way might be to start with the first character of your text and test for the size it would take up (<code>-sizeWithFont:forWidth:lineBreakMode:</code> maybe?) and then keep adding characters one at a time until it doesn't fit into your label A any more.</p> <p>I hope somebody else can come up with a better way to do this, but the above should work.</p> <p><strong>Update</strong></p> <p>Last night I looked a bit into Core Text for my own app and came across <a href="http://developer.apple.com/library/ios/#documentation/Carbon/Reference/CTFramesetterRef/Reference/reference.html" rel="noreferrer"><code>CTFramesetterSuggestFrameSizeWithConstraints</code></a>. You could maybe use this to figure out how much of your string fits into the label, by looking at the <code>fitRange</code> in that function.</p> <p><strong>Update 2:</strong></p> <p>I think this should work, but I have just typed this in here, so it may not even compile:</p> <pre><code>UIFont *uiFont = [UIFont systemFontOfZise:13.0f]; // whichever font you're using CTFontRef ctFont = CTFontCreateWithName((CFStringRef)uiFont.fontName, uiFont.pointSize, NULL); NSDictionary *attr = [NSDictionary dictionaryWithObject:(id)ctFont forKey:(id)kCTFontAttributeName]; CFRelease(ctfont); NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:yourLabelText attributes:attr]; CTFrameSetterRef frameSetter = CTFrameSetterCreateWithAttributedString((CFAttributedStringRef)attrString); [attrString release]; CFRange fitRange; CTFrameSetterSuggestFrameSizeWithConstrains( frameSetter, CFRangeMake(0, 0), NULL, CGSizeMake(labelWidth, labelHeight), &amp;fitRange); CFRelease(frameSetter); CFIndex numberOfCharactersThatFit = fitRange.length; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload