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

從 NSRange 創(chuàng)建 UITextRange

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

問題描述

限時送ChatGPT賬號..

我需要在文本視圖中找到不同范圍的像素幀.我正在使用 - (CGRect)firstRectForRange:(UITextRange *)range; 來做到這一點(diǎn).但是我不知道如何實(shí)際創(chuàng)建 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 協(xié)議.我不這樣做,但我還是嘗試了,按照文檔的示例代碼并將子類傳遞給 firstRectForRange 導(dǎo)致崩潰.

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 設(shè)置為 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 方法創(chuàng)建文本范圍.此方法需要兩個位置,因此您需要計算范圍開始和結(jié)束的位置.這是通過 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];
}

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

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

相關(guān)文檔推薦

KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自動運(yùn)行/壓力測試 iOS 應(yīng)用程序以找出罕見 UI 錯誤的原因?)
Can#39;t change target membership visibility in Xcode 4.5(無法更改 Xcode 4.5 中的目標(biāo)成員身份可見性)
UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合單元格表視圖靜態(tài)和動態(tài)單元格中處理單元格選擇)
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 創(chuàng)建 UIImage 時遇到問題)
主站蜘蛛池模板: 国产久视频 | 欧美成人精品一区二区男人看 | 国产一级片精品 | 久久久福利 | 日韩电影免费在线观看中文字幕 | 亚洲国产一区二区三区 | 国产女人叫床高潮大片免费 | 欧美一区二区免费电影 | 午夜日韩 | 成人av高清 | 久久九九99 | a爱视频 | 暴草美女 | 网页av | 日本a在线 | 一区二区三区免费看 | 一区二区在线看 | 成人超碰 | av资源中文在线 | 网络毛片| 久久99精品久久久 | 免费日韩av | 亚洲一区二区三区在线免费观看 | 国产精品日日做人人爱 | 在线国产一区 | 男女羞羞视频在线免费观看 | 欧美中文一区 | 夜夜艹 | 国产91一区二区三区 | 欧美日韩精品一区二区三区视频 | av喷水| 国产日韩精品久久 | 国产欧美精品一区二区色综合 | 欧美不卡一区二区三区 | 九九精品在线 | 麻豆精品国产91久久久久久 | 国产精品久久久久久久久久久新郎 | 欧美成ee人免费视频 | 色播视频在线观看 | 亚洲日韩视频 | 日韩综合在线播放 |