久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 NSRange 創建 UITextRange

Create UITextRange from NSRange(從 NSRange 創建 UITextRange)
本文介紹了從 NSRange 創建 UITextRange的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我需要在文本視圖中找到不同范圍的像素幀.我正在使用 - (CGRect)firstRectForRange:(UITextRange *)range; 來做到這一點.但是我不知道如何實際創建 UITextRange.

I need to find the pixel-frame for different ranges in a textview. I'm using the - (CGRect)firstRectForRange:(UITextRange *)range; to do it. However I can't find out how to actually create a UITextRange.

基本上這就是我要找的東西:

Basically this is what I'm looking for:

- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView {

    UITextRange*range2 = [UITextRange rangeWithNSRange:range]; //DOES NOT EXIST 
    CGRect rect = [textView firstRectForRange:range2];
    return rect;
}

Apple 說必須繼承 UITextRangeUITextPosition 才能采用 UITextInput 協議.我不這樣做,但我還是嘗試了,按照文檔的示例代碼并將子類傳遞給 firstRectForRange 導致崩潰.

Apple says one has to subclass UITextRange and UITextPosition in order to adopt the UITextInput protocol. I don't do that, but I tried anyway, following the doc's example code and passing the subclass to firstRectForRange which resulted in crashing.

如果有更簡單的方法可以將不同顏色的 UILables 添加到 textview,請告訴我.我曾嘗試使用 UIWebView 并將 content editable 設置為 TRUE,但我不喜歡與 JS 交流,而著色是我唯一需要的.

If there is a easier way of adding different colored UILables to a textview, please tell me. I have tried using UIWebView with content editable set to TRUE, but I'm not fond of communicating with JS, and coloring is the only thing I need.

提前致謝.

推薦答案

您可以使用 textRangeFromPosition:toPosition 方法創建文本范圍.此方法需要兩個位置,因此您需要計算范圍開始和結束的位置.這是通過 positionFromPosition:offset 方法完成的,該方法從另一個位置返回一個位置和一個字符偏移量.

You can create a text range with the method textRangeFromPosition:toPosition. This method requires two positions, so you need to compute the positions for the start and the end of your range. That is done with the method positionFromPosition:offset, which returns a position from another position and a character offset.

- (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView
{
    UITextPosition *beginning = textView.beginningOfDocument;
    UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
    UITextPosition *end = [textView positionFromPosition:start offset:range.length];
    UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];
    CGRect rect = [textView firstRectForRange:textRange];
    return [textView convertRect:rect fromView:textView.textInputView];
}

這篇關于從 NSRange 創建 UITextRange的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動運行/壓力測試 iOS 應用程序以找出罕見 UI 錯誤的原因?)
Can#39;t change target membership visibility in Xcode 4.5(無法更改 Xcode 4.5 中的目標成員身份可見性)
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態和動態單元格中處理單元格選擇)
How to remove Address Bar in Safari in iOS?(如何在 iOS 中刪除 Safari 中的地址欄?)
iOS 5 SDK is gone after upgrade to Xcode 4.5(升級到 Xcode 4.5 后,iOS 5 SDK 消失了)
Having trouble creating UIImage from CIImage in iOS5(在 iOS5 中從 CIImage 創建 UIImage 時遇到問題)
主站蜘蛛池模板: 日韩在线视频一区二区三区 | 日韩二三区 | 国产精品国产三级国产 | 久久久久久久久久国产精品 | 成人免费精品 | 久久免费视频观看 | 亚洲精品久久久久avwww潮水 | 精品一区二区三 | 夜夜躁狠狠躁日日躁av | 日韩av手机在线观看 | 免费不卡av | 亚洲伊人色 | 免费看片黄色 | 色哟哟一区二区三区 | 福利视频一区 | 精品在线免费视频 | 日韩aaaa| 久草久草| 久久久国产精品人人片 | 黄色小视频在线免费观看 | 欧美成人精品欧美一级私黄 | 91av视频在线观看 | 欧美日韩一区二区在线 | 青青草视频在线观看 | 日韩国产精品一区二区 | 免费国产网站 | 手机看片欧美 | 亚洲天堂一区 | 影音先锋在线视频 | 久久艹精品 | 色天天综合| 国产一级一片免费播放放a 男男成人高潮片免费网站 精品视频在线观看 | 激情久久综合 | 加勒比综合 | 秋霞一区二区三区 | 欧美日韩成人在线观看 | 久操视频在线观看 | 中文字幕在线观看一区二区三区 | 亚洲精品日本 | 久久理论片| 伦一理一级一a一片 |