قالب وردپرس درنا توس
Home / IOS Development / ios – textView with exclusion paths and video

ios – textView with exclusion paths and video



I put pictures inside a textView if there is video in the text, then I swap the video's image with the player, the problem arose with the player, I can't understand why the player's position is incorrect, I noticed replace figures are not working properly,
copy? .replaceCharacters (in: videoRange [i - wrong] with: "this is the correct position") replaces only the first image and copy? .replaceCharacters (in: videoRange [i - wrong] with: "+")
anything but with the wrong position, example of how replacesCharacter's work is under

  extension UITextView {

// convert range to CGRect
func boundingRect (forCharacterRange range: NSRange) -> CGRect? {

guard let attributeText = attributeText else {return nil}
let textStorage = NSTextStorage (attributeString: attributeText)
easy layoutManager = NSLayoutManager ()
textStorage.addLayoutManager (layoutManager)
let textContainer = NSTextContainer (size: intrinsicContentSize)
textContainer.lineFragmentPadding = 0
//layoutManager.hyphenationFactor = 1
.0 layoutManager.addTextContainer (textContainer) var glyphRange = NSRange () layoutManager.characterRange (forGlyphRange: range, actualGlyphRange: & glyphRange) var rect = layoutManager.boundingRect (forGlyphRange: glyphRange, in: textContainer) rect.size.height = self.frame.width / 1.7 rect.size.width = self.frame.width rect.origin.x = 0 return policy } // add image and video to text func convertToInlineImageFormat (htmlString: String) { let text = videoInText (htmlString: htmlString) la videoId = text.1 let HTMLString = text.0 guard let data = HTMLString.data (user: String.Encoding.unicode, allowLossyConversion: true) else {return} let content = try! NSMutableAttributedString ( data: data, options: [ .documentType: NSAttributedString.DocumentType.html], documentAttributes: null) var videoRange = [NSRange] () var height = [CGFloat] () var isVideo = [Bool] () la fontDesc = UIFont (name: "roboto-light", size: 19) content.addAttribute (NSAttiatedString.Key.font, value: fontDesc!, range: NSRange (location: 0, length: content.length)) content.enumerateAttribute (NSAttiatedString.Key.attachment, in: NSRange (location: 0, length: content.length), options: [] user: {(value, range, stop) -> Fill in if (value is NSTextAttachment) { add attachment: NSTextAttachment? = (value as? NSTextAttachment) let fileWrapper = attachment? .filWrapper DispatchQueue.main.async { la width = self.frame.size.width whose fileWrapper? .preferredFilename == "0.jpg" { isVideo.append (true) videoRange.append (range) height.append (self.frame.width / 1.7) attachments? .size.size = CGSize (width: width, height: self.frame.width / 1.7) } else { isVideo.append (false) height.append (0) attachments? .boundaries.origin = CGPoint (x: 0, y: 20) attachments? .band size = CGSize (width: width, height: CGFloat (width / (attachment? .band. width? width) * (attachment? .band. height? width))) } } } }) DispatchQueue.main.async { self.textContainer.lineFragmentPadding = 0 // отступы внутри textView self.textContainerInset = UIEdgeInsets (top: 0, left: 0, bottom: 0, right: 0) self.attiatedText = content was wrong = 0 for I in 0 .. <height.number { // get CGRect off the player var rect = self.boundingRect (forCharacterRange: videoRange [i - wrong])! print (straight, "straight") if isVideo [i] { for i in 0 .. < i { rect.origin.y += height[i] } //place for player let imgRect : UIBezierPath = UIBezierPath(rect:rect) if self.textContainer.exclusionPaths == [] { self.textContainer.exclusionPaths = [imgRect] } else { self.textContainer.exclusionPaths.append(imgRect) } //add player YouTubeManager.shared.addVideo(textView: self, id: videoId[i - wrong], rect: rect) // replace image of video let copy = self.attributedText.mutableCopy() as? NSMutableAttributedString copy?.replaceCharacters(in: videoRange[i - wrong], with: "this is correct position") self.attributedText = copy } else { wrong += 1 } } } } //remove iframe and return video`s id func videoInText(htmlString:String)-> (String, [String]) { func setText (text: String) -> (String, [String]) { return formatString (text: text); } // main feature that adds to the YouTube frame func formatString (text: String) -> (String, [String]) { let iframe_texts = match (for: ". * iframe. *", i: text); var new_text = text; var video_id = [String] () if iframe_texts.count> 0 { for iframe_text in iframe_texts { let iframe_id = match (for: "((? <= (v | V) /) | (? <= be /) | (? <= (\? | \ &) v =) | (? <=embed/))([\w-]++)", in: iframe_text); if iframe_id.count > 0 {// in case there is another type of iframe new_text = new_text.replacingOccurrences (av: iframe_text, with: ""); video_id.append (iframe_id [0]) } } } else { // print ("there is no iframe in this text"); } return (new_text, video_id) } func matches (for regex: String, in text: String) -> [String] { do { let regex = try NSRegularExpression (pattern: regex, options: .caseInsensitive) let nsString = text as NSString let results = regex.matches (in: text, range: NSRange (position: 0, length: nsString.length)) return results.map {nsString.substring (with: $ 0.range)} } catch fail { print ("invalid regex: (error.localizedDescription)") return [] } } return setText (text: htmlString) } }

  ---
  ---
  ---


Source link