![](/img/trans.png)
[英]How to improve the performance of [NSString drawInRect:withFont]?
[英]Simple way to see how many lines NSString drawInRect will use?
一直在閑逛,但無法在這里找到答案:
有沒有一種簡單的方法可以計算/查看使用NSString的drawInRect
方法(在子類UIView的drawRect
方法中)將導致多少行文本。
例如以下代碼段:
[self.text drawInRect:titleRect withFont:[UIFont systemFontOfSize:15.] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
我將如何計算此處將產生多少行文本?
謝謝!
使用NSString sizeWithFont:constrainedToSize:lineBreakMode:
方法確定大小。 傳入的CGSize應該具有所需的寬度和很高的高度值。
我結束了一些自定義計算,因為sizeWithFont無法正確使用自定義字體。 一些基本但可行的方法:
NSString *text = post.content;
int cntLB = [[text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] count];
int numLines = [text length]/38+cntLB/2;
其中38 es取決於文本與with的某個閾值參數,cntLB / 2指的是雙\\ r \\ n字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.