簡體   English   中英

在UITextView上獲取特定行的文本

[英]Getting the text of a particular line on UITextView

最后,我想知道UITextView中最后一個字符的位置。 我能夠通過使用以下內容找出y位置:

CGSize size = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width-16,10000)lineBreakMode:UILineBreakModeClip];

但是為了獲得x位置,我需要從textview的最后一行獲取文本。 我嘗試從textView.text一次刪除一個字符,直到size.height更改為實現該效果,但我意識到,當考慮到linebreak時,並不總是等於我想要的。

有人建議嗎? 我花了一整天的時間試圖弄清楚這個......

我想通過使用caretRectForPosition來獲取文本視圖中光標的像素位置,我找到了一個解決方案。

// move cursor to the end of the text
textView.selectedRange = NSMakeRange([textView.text length], 0);
CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin;

您可以像這樣評估uitextview或uitextfield中的文本大小


    UIFont * myFont = [UIFont fontWithName:@"Gill Sans" size:19];
    CGSize textSize = [@"your text" sizeWithFont: myFont constrainedToSize:CGSizeMake(textFiled.frame.size.width, textFiled.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
    NSlog(@"this is your text size W=%f H=%f",textSize.width, textSize.height);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM