简体   繁体   English

用文本内的图像构造UIViewControllers

[英]Structuring UIViewControllers with images inside of text

I have to create an application that has 5 UIViewControllers , each with big text and images inside the text. 我必须创建一个具有5个UIViewControllers的应用程序,每个UIViewControllers都有大文本和文本内部的图像。 What I have done so far was to add a UITextView in the UIViewController and load with an rtf file the whole text (every text is big). 到目前为止,我所做的是在UIViewController添加UITextView并使用rtf文件加载整个文本(每个文本都很大)。

Now I have to add images inside the text in some places. 现在,我必须在某些位置的文本内添加图像。 What do you propose that could be the best way to construct it? 您认为这可能是构建它的最佳方法? I tried to add the image inside the the rtf file but it is not working properly. 我试图将图像添加到rtf文件中,但无法正常工作。 Since the text is too big I did not want to add the text manually by typing it. 由于文本太大,我不想通过键入文本来手动添加文本。 Also, I have a top bar menu that slides the view to each content, that is why I had to have only one UITextView . 另外,我还有一个顶部菜单,可将视图滑动到每个内容,这就是为什么我只需要一个UITextView I am looking for a best solution. 我正在寻找最佳解决方案。

What about adding your texts with images on UIWebView with loading these texts by wrapping them into html? 如何通过在UIWebView上添加带有图像的文本并通过将它们包装到html中来加载这些文本呢?

You can also add javascript callbacks which you will be able to handle in swift or obj-c by adding JavascriptCore.framework to your build phases: 您还可以通过将JavascriptCore.framework添加到构建阶段来添加可在swift或obj-c中处理的javascript回调:

Add button in your code: 在代码中添加按钮:

<button text="Close" onclick="javascript:callSwiftCode()">Call swift code</button>

And in your UIWebViewDelegate class: 在您的UIWebViewDelegate类中:

func webViewDidFinishLoad(webView: UIWebView) {
    let context: JSContext =  webView.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext") as! JSContext

    let codeClosure: @convention(block) ()->() = { ()->() in
        print ("This is callback from javascript you can add your code in this closure")
    }

    let casted: AnyObject = unsafeBitCast(codeClosure, AnyObject.self) as AnyObject
    context.setObject(casted, forKeyedSubscript: "callSwiftCode")
}

You can achieve this using NSAttributedString and NSTextAttachment . 您可以使用NSAttributedStringNSTextAttachment实现此NSTextAttachment Attributed strings are strings with formatting attached(bold, italics, colors, etc), but you can also attach the images inside attributed strings, and they just get drawn right along with the text. 属性字符串是带有附加格式(粗体,斜体,颜色等)的字符串,但是您也可以将图像附加在属性字符串中,并且它们会随文本一起正确绘制。 Below example might help you understand: 以下示例可以帮助您理解:

//Create a mutable attributed string so that we could append everything to it.
let bigText = NSMutableAttributedString(string: "Your text starts here")

//Create a NSTextAttachment
let image1Attachment = NSTextAttachment()
image1Attachment.image = UIImage(named: "image1.png")

// wrap the attachment in its own attributed string so we can append it
let image1String = NSAttributedString(attachment: image1Attachment)

// add the NSTextAttachment wrapper to our full string, then add some more text.
bigText.appendAttributedString(image1String)
bigText.appendAttributedString(NSAttributedString(string: "End of text"))

// Then set this bigText to your label's attributedText property.
yourLabel.attributedText = bigText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在UICollectionviewCells中嵌入UIViewcontrollers - Embedding UIViewcontrollers inside UICollectionviewCells RootViewController内有多个UIViewControllers - Multiple UIViewControllers inside RootViewController UICollectionView内部的UiviewControllers - UiviewControllers inside UICollectionViewCell Swift:如何在 UIViewControllers 中安排 UIViews? - Swift: How to arrange UIViews inside UIViewControllers? UIScrollView中有两个UIViewControllers-退出第一响应者 - Two UIViewControllers inside UIScrollView - resigning first responder UIScrollView内的UIViewControllers内的UIbutton不会在触摸时触发 - UIbutton inside UIViewControllers inside UIScrollView not getting triggered on touch VIPER 架构可以在单个模块中拥有多个 UIViewControllers 吗? - Can a VIPER architecture have multiple UIViewControllers inside a single module? UIScrollview中的两个UIviewControllers-如何在它们之间调用方法 - Two UIviewControllers inside UIScrollview - how to call methods between them 带有许多 UITableVIew 的 UIScrollView 在 UIViewControllers 中消失了 UITableViewCell 的点击 - UIScrollView with many UITableVIew inside UIViewControllers disappear UITableViewCell's on tap 如何在UIActionSheet中的文本旁边插入图像? - How can I insert images next to text inside a UIActionSheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM