UITextView添加文本點(diǎn)擊事件汤纸,屏蔽其他事件問題記錄

UITextView 給某段文字添加點(diǎn)擊事件非常方便,使用富文本添加NSLinkAttributeName屬性即可暂幼。但是UITextView本身帶有許多編輯事件筏勒,導(dǎo)致在觸發(fā)NSLinkAttributeName事件時難免觸發(fā)一些我們不需要的事件移迫,影響用戶體驗(yàn),即使設(shè)置textView.editable = NO;關(guān)閉textView的編輯事件也不能完全屏蔽管行。
為了解決這一問題厨埋,將UITextView上的所有手勢遍歷一遍,發(fā)現(xiàn)有22中手勢捐顷。

for (UIGestureRecognizer *ges in text.gestureRecognizers) {
        NSLog(@"textView ges description : %@", ges.description);    
}
    

結(jié)果:

2020-08-14 10:13:57.238155+0800 workspace[19737:376980] textView ges description : <UITextMultiTapRecognizer: 0x7fa03393a860 (UITextInteractionNameSingleTap); state = Possible; delaysTouchesEnded = NO; view = <UITextView 0x7fa032067800>; target= <(action=onStateUpdate:, target=<UITextMultiTapRecognizer 0x7fa03393a860>)>>
2020-08-14 10:13:57.238509+0800 workspace[19737:376980] textView ges description : <UITapAndAHalfRecognizer: 0x7fa03393a730 (UITextInteractionNameTapAndAHalf); state = Possible; view = <UITextView 0x7fa032067800>; target= <(action=tapAndAHalf:, target=<UITextSelectionInteraction 0x600001a34a80>)>>
2020-08-14 10:13:57.238949+0800 workspace[19737:376980] textView ges description : <UIVariableDelayLoupeGesture: 0x7fa03393a360 (UITextInteractionNameInteractiveLoupe); state = Possible; delaysTouchesEnded = NO; view = <UITextView 0x7fa032067800>; target= <(action=loupeGesture:, target=<UITextLoupeInteraction 0x6000014bd810>)>; numberOfTapsRequired = 0; minimumPressDuration = 0.5>
2020-08-14 10:13:57.239315+0800 workspace[19737:376980] textView ges description : <UITapGestureRecognizer: 0x7fa031d2b650 (UITextInteractionNameLinkTap); state = Possible; delaysTouchesEnded = NO; view = <UITextView 0x7fa032067800>; target= <(action=linkTapped:, target=<_UITextMenuLinkInteraction 0x6000012b08c0>)>>
2020-08-14 10:13:57.239669+0800 workspace[19737:376980] textView ges description : <UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x600001fbea00; state = Possible; delaysTouchesBegan = YES; view = <UITextView 0x7fa032067800>; target= <(action=delayed:, target=<UITextView 0x7fa032067800>)>>
2020-08-14 10:13:57.268294+0800 workspace[19737:376980] textView ges description : <UIScrollViewPanGestureRecognizer: 0x7fa031d2a540; state = Possible; delaysTouchesEnded = NO; view = <UITextView 0x7fa032067800>; target= <(action=handlePan:, target=<UITextView 0x7fa032067800>)>>
2020-08-14 10:13:57.268759+0800 workspace[19737:376980] textView ges description : <UIScrollViewKnobLongPressGestureRecognizer: 0x7fa031d2a930; state = Possible; view = <UITextView 0x7fa032067800>; target= <(action=_handleKnobLongPressGesture:, target=<UITextView 0x7fa032067800>)>; numberOfTapsRequired = 0; minimumPressDuration = 0.1>
2020-08-14 10:13:57.268935+0800 workspace[19737:376980] textView ges description : <UIScrollViewKnobLongPressGestureRecognizer: 0x7fa031d2ab00; state = Possible; view = <UITextView 0x7fa032067800>; target= <(action=_handleKnobLongPressGesture:, target=<UITextView 0x7fa032067800>)>; numberOfTapsRequired = 0>
2020-08-14 10:13:57.269288+0800 workspace[19737:376980] textView ges description : <UIHoverGestureRecognizer: 0x7fa031d2acd0; state = Possible; view = <UITextView 0x7fa032067800>; target= <(action=_handleKnobHoverGesture:, target=<UITextView 0x7fa032067800>)>>
2020-08-14 10:13:57.269637+0800 workspace[19737:376980] textView ges description : <_UIDragAutoScrollGestureRecognizer: 0x600001caf0c0; state = Possible; cancelsTouchesInView = NO; delaysTouchesEnded = NO; view = <UITextView 0x7fa032067800>; target= <(action=_handleAutoScroll:, target=<UITextView 0x7fa032067800>)>>
2020-08-14 10:13:57.269956+0800 workspace[19737:376980] textView ges description : <_UIDragAddItemsGesture: 0x7fa031f14e10 (dragAddingItems); state = Possible; delaysTouchesBegan = YES; view = <UITextView 0x7fa032067800>>
2020-08-14 10:13:57.271577+0800 workspace[19737:376980] textView ges description : <_UIDragLiftGestureRecognizer: 0x7fa031f2a860 (dragInitiation); state = Possible; cancelsTouchesInView = NO; view = <UITextView 0x7fa032067800>; target= <(action=_dragInitiationGestureStateChanged:, target=<_UIDragInteractionLongPressDriver 0x6000011b5440>)>; numberOfTapsRequired = 0; minimumPressDuration = 0.325>
2020-08-14 10:13:57.271901+0800 workspace[19737:376980] textView ges description : <_UIRelationshipGestureRecognizer: 0x600001acea00 (dragExclusionRelationships); state = Possible; view = <UITextView 0x7fa032067800>>
2020-08-14 10:13:57.272043+0800 workspace[19737:376980] textView ges description : <_UIRelationshipGestureRecognizer: 0x600001ace920 (dragFailureRelationships); state = Possible; view = <UITextView 0x7fa032067800>>
2020-08-14 10:13:57.272365+0800 workspace[19737:376980] textView ges description : <_UIDragLiftPointerGestureRecognizer: 0x7fa031f2ae00 (dragInitiation); state = Possible; cancelsTouchesInView = NO; view = <UITextView 0x7fa032067800>; target= <(action=_dragInitiationGestureStateChanged:, target=<_UIDragInteractionLongPressDriver 0x6000011b55f0>)>; numberOfTapsRequired = 0; minimumPressDuration = 0.15>
2020-08-14 10:13:57.272552+0800 workspace[19737:376980] textView ges description : <_UIRelationshipGestureRecognizer: 0x600001acef40 (dragExclusionRelationships); state = Possible; view = <UITextView 0x7fa032067800>>
2020-08-14 10:13:57.272702+0800 workspace[19737:376980] textView ges description : <_UIRelationshipGestureRecognizer: 0x600001acee60 (dragFailureRelationships); state = Possible; view = <UITextView 0x7fa032067800>>
2020-08-14 10:13:57.273017+0800 workspace[19737:376980] textView ges description : <_UISecondaryClickDriverGestureRecognizer: 0x600001afe060; state = Possible; view = <UITextView 0x7fa032067800>; target= <(action=_handleGestureRecognizer:, target=<_UISecondaryClickClickInteractionDriver 0x6000036cd500>)>>
2020-08-14 10:13:57.273402+0800 workspace[19737:376980] textView ges description : <_UITouchDurationObservingGestureRecognizer: 0x7fa031d2c310 (com.apple.UIKit.longPressClickDriverPrimary); state = Possible; cancelsTouchesInView = NO; view = <UITextView 0x7fa032067800>; target= <(action=_handleGestureRecognizer:, target=<_UILongPressTimeoutClickInteractionDriver 0x600000088c80>)>>
2020-08-14 10:13:57.273564+0800 workspace[19737:376980] textView ges description : <_UITouchDurationObservingGestureRecognizer: 0x7fa031d2c640 (com.apple.UIKit.longPressClickDriverPrimary); state = Possible; cancelsTouchesInView = NO; view = <UITextView 0x7fa032067800>; target= <(action=_handleGestureRecognizer:, target=<_UILongPressTimeoutClickInteractionDriver 0x600000088cd0>)>>
2020-08-14 10:13:57.275039+0800 workspace[19737:376980] textView ges description : <_UIRelationshipGestureRecognizer: 0x600001a34700 (com.apple.UIKit.clickPresentationExclusion); state = Possible; view = <UITextView 0x7fa032067800>>
2020-08-14 10:13:57.275212+0800 workspace[19737:376980] textView ges description : <_UIRelationshipGestureRecognizer: 0x600001a347e0 (com.apple.UIKit.clickPresentationFailure); state = Possible; view = <UITextView 0x7fa032067800>>

其中與NSLinkAttributeName相關(guān)的linkTapped事件是一個UITapGestureRecognizer點(diǎn)擊事件荡陷,有且僅有一個,那么大膽嘗試一下迅涮,將所有除UITapGestureRecognizer外的手勢事件全部刪除:

for (UIGestureRecognizer *ges in textView.gestureRecognizers) {
            if (![ges isMemberOfClass:[UITapGestureRecognizer class]]) {
                [textView removeGestureRecognizer:ges];
            }
        }

刪除后發(fā)現(xiàn)確實(shí)只剩下了自己添加的NSLinkAttributeName點(diǎn)擊事件會響應(yīng)废赞,其他不相關(guān)事件的始終與完美屏蔽。

UITextView文本顯示邊距問題

1.去除文本上下邊距
textView.textContainerInset = UIEdgeInsetsZero;
2.出去文本左右邊距(將內(nèi)容縮進(jìn)設(shè)置為0)
textView.textContainer.lineFragmentPadding = 0;
使用上述方法去除文本左右邊距后叮姑,UITextView選擇功能出現(xiàn)問題唉地,左邊選擇控件無法移動問題据悔。
解決辦法:
textView.textContainer.lineFragmentPadding 默認(rèn)值為5,即文本默認(rèn)左右邊距為5耘沼,可通過textView.contentInset與textView.textContainer.lineFragmentPadding計(jì)算設(shè)置文本左右邊距极颓。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市群嗤,隨后出現(xiàn)的幾起案子菠隆,更是在濱河造成了極大的恐慌,老刑警劉巖狂秘,帶你破解...
    沈念sama閱讀 210,978評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件骇径,死亡現(xiàn)場離奇詭異,居然都是意外死亡者春,警方通過查閱死者的電腦和手機(jī)既峡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評論 2 384
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來碧查,“玉大人运敢,你說我怎么就攤上這事≈沂郏” “怎么了传惠?”我有些...
    開封第一講書人閱讀 156,623評論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長稻扬。 經(jīng)常有香客問我卦方,道長,這世上最難降的妖魔是什么泰佳? 我笑而不...
    開封第一講書人閱讀 56,324評論 1 282
  • 正文 為了忘掉前任盼砍,我火速辦了婚禮,結(jié)果婚禮上逝她,老公的妹妹穿的比我還像新娘浇坐。我一直安慰自己,他們只是感情好黔宛,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,390評論 5 384
  • 文/花漫 我一把揭開白布近刘。 她就那樣靜靜地躺著,像睡著了一般臀晃。 火紅的嫁衣襯著肌膚如雪觉渴。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,741評論 1 289
  • 那天徽惋,我揣著相機(jī)與錄音案淋,去河邊找鬼。 笑死险绘,一個胖子當(dāng)著我的面吹牛踢京,可吹牛的內(nèi)容都是我干的回右。 我是一名探鬼主播,決...
    沈念sama閱讀 38,892評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼漱挚,長吁一口氣:“原來是場噩夢啊……” “哼翔烁!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起旨涝,我...
    開封第一講書人閱讀 37,655評論 0 266
  • 序言:老撾萬榮一對情侶失蹤蹬屹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后白华,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體慨默,經(jīng)...
    沈念sama閱讀 44,104評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年弧腥,在試婚紗的時候發(fā)現(xiàn)自己被綠了厦取。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,569評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡管搪,死狀恐怖虾攻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情更鲁,我是刑警寧澤霎箍,帶...
    沈念sama閱讀 34,254評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站澡为,受9級特大地震影響漂坏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜媒至,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,834評論 3 312
  • 文/蒙蒙 一顶别、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拒啰,春花似錦驯绎、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,725評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蛤织,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鸿染,已是汗流浹背指蚜。 一陣腳步聲響...
    開封第一講書人閱讀 31,950評論 1 264
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涨椒,地道東北人摊鸡。 一個月前我還...
    沈念sama閱讀 46,260評論 2 360
  • 正文 我出身青樓绽媒,卻偏偏與公主長得像,于是被迫代替她去往敵國和親免猾。 傳聞我的和親對象是個殘疾皇子是辕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,446評論 2 348