屬性
iOS中的字體大小,即UIFont的pointSize屬性,并不直接對(duì)應(yīng)行高,一般一個(gè)字體是10的文本,line height一般為11.89,大約為字體大小的1.2倍,所以按照這個(gè)結(jié)果來(lái)看,iOS字體默認(rèn)是1.2倍行高。
行間距和行高
- line space:行間距,直接對(duì)應(yīng)兩行文本之間的間距值。
- line height of font:每一行文字的高度。
- 行高:每一行的實(shí)際高度,= line height of font + line space。
- 行高倍數(shù):lineHeightMultiple,幾倍行高,= 行高 / line height。
UITextView光標(biāo)問(wèn)題
設(shè)置textView的行間距
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:textContent];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = 6;
[attr addAttributes:@{NSParagraphStyleAttributeName:style} range:NSMakeRange(0, textContent.length)];
[attr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:18]
range:NSMakeRange(0, textContent.length)];
- 設(shè)置行間距之后,輸入時(shí)光標(biāo)高度會(huì)變大
如圖截屏 2024-09-09 10.32.53.jpeg
- UITextView中重寫- (CGRect)caretRectForPosition:(UITextPosition *)position 方法
- //原理 //UITextView遵循了UITextInput協(xié)議,其中有返回光標(biāo)frame的方法
- (CGRect)caretRectForPosition:(UITextPosition *)position {
CGRect originalRect = [super caretRectForPosition:position];
originalRect.size.height = self.font.lineHeight + 2.f;
originalRect.size.width = 2.f;
return originalRect;
}
