iOS 富文本編輯器

  1. 使用CoreText 還是使用WebView 優(yōu)缺點比較

  2. CoreText
    開發(fā)維護困難钧舌,代碼繁瑣挟伙,邏輯太多,以后維護很困難
    優(yōu)點就是 性能 體驗好

  3. WebView
    簡單,快捷募狂, 維護簡單
    缺點 js html 知識欠缺畜份,與OC交互問題

webview 開發(fā)編輯器的知識點

  1. Selection WebAPIs->Selection

A **Selection** object represents the range of text selected by the user or the current position of the caret. To obtain a Selection object for examination or modification, call window.getSelection().

A user may make a selection from left to right (in document order) or right to left (reverse of document order). The anchor is where the user began the selection and the focus is where the user ends the selection. If you make a selection with a desktop mouse, the anchor is placed where you pressed the mouse button and the focus is placed where you released the mouse button. Anchor and focus should not be confused with the start and end positions of a selection, since anchor can be placed before the focus or vice versa, depending on the direction you made your selection.

  1. Range

The Range interface represents a fragment of a document that can contain nodes and parts of text nodes.

A range can be created using the createRange() method of the Document object. Range objects can also be retrieved by using the getRangeAt() method of the Selection object or the caretRangeFromPoint() method of the Documentobject.

There also is the Range() constructor available.

  1. Document.execCommand

When an HTML document has been switched to [designMode](designMode.html), its document object exposes an execCommand method to run commands that manipulate the current editable region, such as form inputs or [contentEditable](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable) elements.

Most commands affect the document's selection (bold, italics, etc.), while others insert new elements (adding a link), or affect an entire line (indenting). When using contentEditable, execCommand() affects the currently active editable element.

  1. selectionchange 事件

The **selectionchange** event of the Selection API is fired when the current text selection on a document is changed.

document.addEventListener("selectionchange", function() {
  console.log('Selection changed.'); 
});

3. 關(guān)鍵的幾個問題

  1. 如何插入圖片诞帐,刪除圖片 ?

插入圖片使用execommand insertImage爆雹,傳入必要的參數(shù)就會顯示出圖片停蕉,
刪除的話,按刪除鍵 會直接把整個img標(biāo)簽全部一次刪除了钙态。

function insertImage(imageName, imagePath) {
        restoreSelection();
        var imageElement = document.createElement('img');
        var breakElement = document.createElement('div');
        imageElement.setAttribute('src', imagePath);
        imageElement.setAttribute('id', imageName);
        breakElement.innerHTML = "<br>";
        // var img = '<img src="+'imagePath'" />';
        // editableContent.appendChild(imageElement);
        // editableContent.appendChild(breakElement);

        var html = '<img src="' + imagePath + '" alt="' + '' + '" />';
        // document.execCommand('insertImage',false,imagePath);
        document.execCommand('insertHTML',false,html);
    }

  1. 如何調(diào)整offset 慧起,不讓鍵盤蓋住輸入?yún)^(qū)域?

http://ghmagical.com/article/page/id/UjkNGQ0vmFFF
https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
http://www.reibang.com/p/c4d7824362cb

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末驯绎,一起剝皮案震驚了整個濱河市完慧,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌剩失,老刑警劉巖屈尼,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異拴孤,居然都是意外死亡脾歧,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門演熟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鞭执,“玉大人,你說我怎么就攤上這事芒粹⌒址模” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵化漆,是天一觀的道長估脆。 經(jīng)常有香客問我,道長座云,這世上最難降的妖魔是什么疙赠? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任付材,我火速辦了婚禮,結(jié)果婚禮上圃阳,老公的妹妹穿的比我還像新娘厌衔。我一直安慰自己,他們只是感情好捍岳,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布富寿。 她就那樣靜靜地躺著,像睡著了一般祟同。 火紅的嫁衣襯著肌膚如雪作喘。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天晕城,我揣著相機與錄音泞坦,去河邊找鬼。 笑死砖顷,一個胖子當(dāng)著我的面吹牛贰锁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播滤蝠,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼豌熄,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了物咳?” 一聲冷哼從身側(cè)響起锣险,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎览闰,沒想到半個月后芯肤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡压鉴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年崖咨,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片油吭。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡击蹲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出婉宰,到底是詐尸還是另有隱情歌豺,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布心包,位于F島的核電站世曾,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜轮听,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望岭佳。 院中可真熱鬧血巍,春花似錦、人聲如沸珊随。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽叶洞。三九已至鲫凶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間衩辟,已是汗流浹背螟炫。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留艺晴,地道東北人昼钻。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像封寞,于是被迫代替她去往敵國和親然评。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容