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

Having issue modifying theme code to replace logo URL
Combination of php_opcache and IIS causing intermittent 500 errors
DOMPDF class not found
wordpress APi integration
How Remove extra tag in xml in php
How to add a custom year drop-down in place of Export CSV drop-down on admin panel for custom grid and filter the collection?
Sending boolean values with $.ajax to PHP
Loop row in bootstrap every 3 columns
Get friends name/id list form graph api into array
LAMP shows index.php in var/www/html instead of project
How To play 25MB+ audio file directly from google drive in HTML 5?
PHP/MYSQL - Select option value not being sent?
Laravel : How to use a parameters in a Form POST to be use in a Route::post?
How to convert object format to json format in php
laravel,how i can change the code to return an array?
How to make At Least Two Field Required to fill in Laravel 5.2

Categories

HOME
memory
functional-programming
shinyapps
xcode8.1
bokeh
activemq
adobe
biztalk-2010
moodle-api
azure-data-lake
impala
angular-meteor
anchor
twitter-oauth
migrate
pushdown-automaton
sql-injection
squid
texas-instruments
fosuserbundle
varnish-vcl
renderscript
angular2-highcharts
sqlplus
nurbs
openbravo
fatfs
unmarshalling
wampsharp
nest
winrm
apptentive
bean-validation
amazon-kms
stat
httphandler
apache-toree
destroy
brightscript
password-hash
visual-format-language
polymaps
dymola
openerp-6
prefetch
ogg
researchkit
mathml
cdo.message
unsigned
android-syncadapter
rails-engines
simian
swiffy
distributed-cache
gdl
phpquery
decidable
bjam
green-threads
android-handler
embeddedwebserver
shoes
scala-macros
subview
password-recovery
dnsjava
image-rotation
inputbox
quantify
libgcc
communicate
two.js
forceclose
incognito-mode
air-native-extension
preorder
virtual-channel
form-for
apportable
aero
chefspec
office-app
sfinae
websphere-6.1
sigar
firefly-mv
database-deadlocks
azure-acs
printqueue
hungarian-notation
ninject-interception
tabbarcontroller
3270
gtktextview
xap
iphone-sdk-3.1.3
version-control-migration
regioninfo
html-generation

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App