php


swift 3: Get rtf formatted text from MySQL


In my App I have a lot of rtf files that I read and put the text into a UITextView with AttributedText.
Now I want to put all those files into a MySQL database and read them with a PHP file and URLSession.
import UIKit
class ViewController: UIViewController, URLSessionDataDelegate {
var returnString = ""
var aOverskrifter:NSMutableArray = []
var aLilleTekster:NSMutableArray = []
var aLangTekster:NSMutableArray = []
var styledText = NSMutableAttributedString()
#IBOutlet var txtWikiTekst: UITextView!
//** Button to get data from php file
#IBAction func btnHentWiki_Click(_ sender: UIButton) {
//** Læs fra nettet
let myURLString = "xxxx.php"
HentResponseString(url: myURLString)
}
//** Show rtf text in UITextView
#IBAction func btnVisData_Click(_ sender: UIButton) {
txtWikiTekst.attributedText = styledText
}
//** Function the gets the text from php page
func HentResponseString(url: String) {
let url = URL(string: url)!
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print("request failed \(error)")
return
}
let responseString = String(data: data, encoding: .utf8)
//** Bearbejd responseString
let lines = responseString?.components(separatedBy: "#")
for line in lines! {
let fields = line.components(separatedBy: "$")
var xcounter:Int = 0
for field in fields {
//** Overskrift
if xcounter == 0 {
self.aOverskrifter.add(field)
}
//** Lille tekst
if xcounter == 1 {
self.aLilleTekster.add(field)
}
//** Lang tekst
if xcounter == 2 {
self.aLangTekster.add(field)
styledText = field
}
xcounter+=1
}
}
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
But when I run this code I only see the rtf text in the UITextView and not the formatted text.
In the database I'm using UTF8 and in the PHP file i use UTF8, but it does not work correctly.
Does anyone know how to fix this or can you point me in the right direction.
Once you have the RTF text you should convert it to a Data object, and use the Data object to initialize your NSAttributedString variable. To demonstrate this I created an RTF "Hello, world" document in TextEdit, with "Hello" in red text and "world" in blue. The following code, which you can run in a playground, creates that colored attributed string from the contents of the saved TextEdit .rtf file:
let rtfText = "{\\rtf1\\ansi\\ansicpg1252\\cocoartf1504\\cocoasubrtf760\n{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}\n{\\colortbl;\\red255\\green255\\blue255;\\red255\\green0\\blue0;\\red0\\green0\\blue255;}\n{\\*\\expandedcolortbl;;\\csgenericrgb\\c100000\\c0\\c0;\\csgenericrgb\\c0\\c0\\c100000;}\n\\margl1440\\margr1440\\vieww12600\\viewh7800\\viewkind0\n\\pard\\tx720\\tx1440\\tx2160\\tx2880\\tx3600\\tx4320\\tx5040\\tx5760\\tx6480\\tx7200\\tx7920\\tx8640\\pardirnatural\\partightenfactor0\n\n\\f0\\fs24 \\cf2 Hello\\cf0 , \\cf3 world}"
let rtfData = rtfText.data(using: .utf8)!
let attributedString = try! NSAttributedString(data:rtfData, options: [:], documentAttributes: nil)

Related Links

Reading word document
Why is the number 16 converted to float(6.1026988574311E_320) by PHP using Zend_Amf
Taking the data mapper approach in Zend Framework
Display amazon books/products with PHP [closed]
PHP OPML Parser
is it a good practice to use mysql_free_result($result)?
How to match a quoted string with escaped quotes in it?
Best way to flush or clear Ajax “temp” stuffs in IE7
Connecting flex/php to Active Directory
How do I control retrieving data from tables in a database
php strtotime() some help
Issues with links while trying to converting HTML to XML
problem with resubmission of form data
Including dynamic HTML with PHP
Problem with my implementation of the Post/Redirect/Get pattern
How to get PHP time tomorrow 6pm CST?

Categories

HOME
message-queue
vsts-build
yql
mathematical-optimization
google-sheets-query
tfs2015
material-components
haxe
phpseclib
jpa-criteria
clickable-image
scrape
sql-injection
supervisord
pickle
nullreferenceexception
pe
yii2-basic-app
multi-upload
websphere-portal
raima
password-generator
prompt
android-service
tortoisehg
pyspark-sql
riotjs
custom-keyboard
bluemixtools
stackpanel
code-rally
uivisualeffectview
xll
django-smart-selects
flowchart
uipickerview
resampling
coverage.py
word-cloud
nuclio
ol3-google-maps
solr-query-syntax
sequence-diagram
degrees
emf-compare
s-function
jpda
wininet
eclemma
static-methods
verisign
preferenceactivity
openmrs
vim-plugin
teensy
enunciate
perceptron
fanotify
dsa
canopy
gitlab-omnibus
erlog
physicsjs
webproject
tidyr
dos2unix
utf8-decode
angular-ui-select
xcode6.3.1
lumia-imaging-sdk
accessory
commenting
meteor-collections
preorder
pre
sensormanager
non-ascii-characters
delphi-xe4
onconfigurationchanged
getproperty
kolite
n-queens
fbml
filelock
sql-parametrized-query
startupscript
azure-acs
active-record-query
mongrel
urchin
.net-services
ppc
source-code-protection
tso
account-management

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App