-1

so i'm trying to implement a simple english to farsi dictionary in iOS

i'd like to include both words in one table cell, problem is that english is L>R and farsi is R>L, also i'd like to make the farsi word a bit bigger.

I created an AttributedMutableString and I thought I put down all the correct values but it looks like there is a problem since it isn't rendering correctly.

code:

cell.textLabel?.numberOfLines = 0


var myString = "\(englishConvo[indexPath.row])\n\(farsiConvo[indexPath.row])"
var mutableString = NSMutableAttributedString()

var lenOfLang1 = englishConvo[indexPath.row].characters.count
var lenOfLang2 = farsiConvo[indexPath.row].characters.count

let increaseFontSize = UIFont(name: (cell.textLabel?.font.fontName)!, size: (cell.textLabel?.font?.pointSize)! + 5)

let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = NSTextAlignment.Right

mutableString = NSMutableAttributedString(string: myString)

mutableString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange(location: lenOfLang1 + 1, length: lenOfLang2))

mutableString.addAttribute(NSFontAttributeName, value: increaseFontSize!, range: NSRange(location: lenOfLang1 + 1, length: lenOfLang2))


cell.textLabel?.attributedText = mutableString

enter image description here

If i convert to a string using this code this is what I get

cell.textLabel?.text = String(mutableString)

enter image description here

Any thoughts / ideas would be super appreciated

hnaderi
  • 409
  • 8
  • 16
  • But if you convert to a String you throw away the attributes (the styling). So aren't you just shooting yourself in the foot? – matt Mar 11 '16 at 19:34
  • why do you need to put both strings in a single label? You can have two labels in the one table cell, and get the same result you're trying to achieve, but with a lot more control – Russell Mar 11 '16 at 19:34
  • @Russell a feature i'm trying to implement is that a user can 'favorite' a particular word, so it would be a lot easier to accomplish that if everything was in one cell – hnaderi Mar 11 '16 at 20:19
  • @matt i only did that for debugging / troubleshooting purposes – hnaderi Mar 11 '16 at 20:20
  • He didn't say put it in two cells, he said display it in two labels in the same cell. The data isn't _in_ the cell in any case; it's in the _data_. – matt Mar 11 '16 at 20:21
  • @matt hey matt, actually it looks like i was trying to put the data on ONE label in ONE cell can i access a second label within one cell? – hnaderi Mar 11 '16 at 20:29

1 Answers1

1

Table cells already come with a layout that gives you two labels (text and detail), so why not just use it? Here, for example, in a language app of mine, I'm displaying a Latin word in the text label and an English translation in the detail label. You could easily do the same with Farsi and English.

enter image description here

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • If it's a RTL language, using natural justification will _automatically_ right-align it! – matt Mar 11 '16 at 20:29
  • hmm lemme look into that, it's a great suggestion only issue is i want the detail to be larger than the text. the farsi is more important than the english which is why i was trying to also increase the font – hnaderi Mar 11 '16 at 20:33
  • That's entirely up to you. It's just a label, you can format it however you like. – matt Mar 11 '16 at 20:34