3
A
回答
4
具體使用CoreText,這裏應該工作(與代碼的一些註釋解釋這是怎麼回事)功能:
- (CGRect)lastCharacterRectForAttributedString:(NSAttributedString *)attributedString drawingRect:(CGRect)drawingRect
{
// Start by creating a CTFrameRef using the attributed string and rect.
CTFrameRef textFrame = NULL;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attributedString));
CGPathRef drawingPath = CGPathCreateWithRect(drawingRect, NULL);
textFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, [attributedString length]), drawingPath, NULL);
CFRelease(framesetter);
CFRelease(drawingPath);
// Line origins can be obtained from the CTFrameRef. Get the final one.
CGPoint finalLineOrigin;
CFArrayRef lines = CTFrameGetLines(textFrame);
if (CFArrayGetCount(lines) == 0) { // Safety check
CFRelease(textFrame);
return CGRectNull;
}
const CFIndex finalLineIdx = CFArrayGetCount(lines) - 1;
CTFrameGetLineOrigins(textFrame, CFRangeMake(finalLineIdx, 1), &finalLineOrigin);
// Get the glyph runs from the final line. Get the last glyph position from the final run.
CGPoint glyphPosition;
CFArrayRef runs = CTLineGetGlyphRuns(CFArrayGetValueAtIndex(lines, finalLineIdx));
if (CFArrayGetCount(runs) == 0) { // Safety check
CFRelease(textFrame);
return CGRectNull;
}
CTRunRef finalRun = CFArrayGetValueAtIndex(runs, CFArrayGetCount(runs) - 1);
if (CTRunGetGlyphCount(finalRun) == 0) { // Safety check
CFRelease(textFrame);
return CGRectNull;
}
const CFIndex lastGlyphIdx = CTRunGetGlyphCount(finalRun) - 1;
CTRunGetPositions(finalRun, CFRangeMake(lastGlyphIdx, 1), &glyphPosition);
// The bounding box of the glyph itself is extracted from the font.
CGRect glyphBounds;
CFDictionaryRef runAttributes = CTRunGetAttributes(finalRun);
CTFontRef font = CFDictionaryGetValue(runAttributes, NSFontAttributeName);
CGGlyph glyph;
CTRunGetGlyphs(finalRun, CFRangeMake(lastGlyphIdx, 1), &glyph);
CTFontGetBoundingRectsForGlyphs(font, kCTFontDefaultOrientation, &glyph, &glyphBounds, 1);
// Option 1 - The rect you've drawn in your question isn't tight to the final character; it looks approximately the height of the line. If that's what you're after:
CGRect lineBounds = CTLineGetBoundsWithOptions(CFArrayGetValueAtIndex(lines, finalLineIdx), 0);
CGRect desiredRect = CGRectMake(
CGRectGetMinX(drawingRect) + finalLineOrigin.x + glyphPosition.x + CGRectGetMinX(glyphBounds),
CGRectGetMinY(drawingRect) + (CGRectGetHeight(drawingRect) - (finalLineOrigin.y + CGRectGetMaxY(lineBounds))),
CGRectGetWidth(glyphBounds),
CGRectGetHeight(lineBounds)
);
// Option 2 - If you want a rect that closely bounds the final character, use this:
/*
CGRect desiredRect = CGRectMake(
CGRectGetMinX(drawingRect) + finalLineOrigin.x + glyphPosition.x + CGRectGetMinX(glyphBounds),
CGRectGetMinY(drawingRect) + (CGRectGetHeight(drawingRect) - (finalLineOrigin.y + glyphPosition.y + CGRectGetMaxY(glyphBounds))),
CGRectGetWidth(glyphBounds),
CGRectGetHeight(glyphBounds)
);
*/
CFRelease(textFrame);
return desiredRect;
}
相關問題
- 1. 如何獲得字符串的最後一個字符,XSLT
- 2. 如何獲得字符串的最後一個字母
- 3. 在覈心文本中查找文本的邊界矩形
- 4. 如何繪製佈局字形一一 - 核心文本
- 5. Javascript:如何獲得Dom樹中的最後一個字符?
- 6. 如何獲得字符串中「%」的最後一個索引?
- 7. 獲得的最後一個字符的字符串
- 8. 如何使用EVAL得到一個字符串的最後四個字符
- 9. 如何獲得符合特定矩形的NSAttributedString子字符串?
- 10. 如何替換最後一個字符後的所有文本
- 11. 如何獲得最後一個逗號後的字符串在一個領域
- 12. 獲得最後一個字符類型的字符串
- 13. c#TextBox如何獲得一個字符串的最後一個字母?
- 14. 如何獲得前8個字符和最後8個字符串使用JavaScript?
- 15. 如何獲得超過2個核心的CPU使用率?
- 16. 樹枝:如何獲得字符串中的最後一個字符
- 17. 獲取離中心矩形最遠的矩形
- 18. 核心文本 - 字形高度
- 19. 獲得來自URL最後一個字符串使用jQuery
- 20. 如何使用lseek讀取文件的最後一個字符
- 21. 如何獲得最後四位和第一個字符?
- 22. 如何獲取字符串的最後一個字符?
- 23. 如何獲取PHP中最後一個字符串的字符?
- 24. 如何獲取C++中字符串的最後一個字符
- 25. 如何在靠近中心的矩形內獲得一個隨機點?
- 26. 如何刪除文本中的最後一個字符?
- 27. 如何使用Excel函數獲取字符串的最後一個字符?
- 28. 核心圖形,如何繪製橢圓透明孔的矩形?
- 29. 文本獲取呈現錯誤的,每個字符是一個白色矩形
- 30. 核心圖形中的Unicode字符
嗨,@Stefan。我曾經有過類似的問題(http://stackoverflow.com/questions/21751738/ios-finding-correct-font-size-to-fit-in-a-uilabel-depending-on-its-size)posted在此之前,我認爲它與這個密切相關。但是我不是想找到最後一個字母的邊界框,而是在給定特定字體和大小的情況下,在一行中查找字母的大小,並使用CoreText均勻地在一行上將它們分隔(或「UILabel」)。看看那裏,當你有時間。提前致謝。 – Unheilig