0

I have created a class of message including, content and sender. I successfully store the desired data in Parse and I am querying them. So far, no problem. Then, I attempt to filter the messages according to the sender, or the receiver, in order to display them in different manners on my tableView. For instance, let's say that is sender is currentUser, the text is blue, otherwise it is green. If currentUser sends a message, all the text becomes blue, even the one sent by the other user; and vice versa.

class Message {

var sender = ""
var message = ""
var time = NSDate()

    init(sender: String, message: String, time: NSDate) 
    {  
        self.sender = sender
        self.message = message
        self.time = time
    }
}

var message1: Message = Message(sender: "", message: "", time: NSDate())

func styleTextView() 
{
    if message1.sender == PFUser.currentUser()?.username {
        self.textView.textColor = UIColor.blueColor()
    } else {
        self.textView.textColor = UIColor.greenColor()
    }
}


func addMessageToTextView(message1: Message) 
{
    textView.text = message1.message
    self.styleTextView()
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:   NSIndexPath) -> UITableViewCell 
{
    var cell: messageCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! messageCell

    let message = self.array[indexPath.row]

    cell.setupWithMessage(message)

    return cell
}

I have other codes on my viewController, however I believe they are irrelevant to this matter; if needed, I can provide them. Any idea why I cannot have the textViews in different style according to the sender? They are all either blue, either green.

Below is the code for the full set up of the tableViewCell:

class messageCell: UITableViewCell {

    private let padding: CGFloat = 10.0
    var array1 = [Message]()

    private func styleTextView() 
    {

        let halfTextViewWidth = CGRectGetWidth(textView.bounds) / 2.0
        let targetX = halfTextViewWidth + padding
        let halfTextViewHeight = CGRectGetHeight(textView.bounds) / 2.0

        self.textView.font = UIFont.systemFontOfSize(12.0)

        if PFUser.currentUser()?.username == message1.sender && textView.text == message1.message {

            self.textView.backgroundColor = UIColor.blueColor()
            self.textView.layer.borderColor = UIColor.blueColor().CGColor
            self.textView.textColor = UIColor.whiteColor()
            textView.center.x = targetX
            textView.center.y = halfTextViewHeight 
        } else {

            self.textView.backgroundColor = UIColor.orangeColor()
            self.textView.layer.borderColor = UIColor.orangeColor().CGColor
            self.textView.textColor = UIColor.whiteColor()
            self.textView.center.x = CGRectGetWidth(self.bounds)  - targetX
            self.textView.center.y = halfTextViewHeight
        }
    }


    private lazy var textView: MessageBubbleTextView = {
        let textView = MessageBubbleTextView(frame: CGRectZero, textContainer: nil)
        self.contentView.addSubview(textView)
        return textView
    }()



    class MessageBubbleTextView: UITextView 
    {
        override init(frame: CGRect = CGRectZero, textContainer: NSTextContainer? = nil) 
        {
            super.init(frame: frame, textContainer: textContainer)
            self.scrollEnabled = false
            self.editable = false
            self.textContainerInset = UIEdgeInsets(top: 7, left: 7, bottom: 7, right: 7)
            self.layer.cornerRadius = 15
            self.layer.borderWidth = 2.0
        }

        required init(coder aDecoder: NSCoder) 
        {
            fatalError("init(coder:) has not been implemented")
        }
    }

    private let minimumHeight: CGFloat = 30.0 
    private var size = CGSizeZero

    private var maxSize: CGSize {
        get {
            let maxWidth = CGRectGetWidth(self.bounds) 
            let maxHeight = CGFloat.max
            return CGSize(width: maxWidth, height: maxHeight)
        }
    }


    func setupWithMessage(message: Message) -> CGSize 
    {
        textView.text = message.message
        size = textView.sizeThatFits(maxSize)
        if size.height < minimumHeight {
           size.height = minimumHeight
        }
        textView.bounds.size = size
        self.styleTextView()
        return size
    }
}
LuckyStarr
  • 1,468
  • 2
  • 26
  • 39
viktor
  • 31
  • 9
  • Is the UITextView inside a UITableViewCell ? From your posted code is nor clear. – LuckyStarr Jul 19 '15 at 10:39
  • Yes it is. Sorry about that, I took some segment of codes from different parts and posted it there. It is confusing indeed. – viktor Jul 19 '15 at 18:32
  • @lucky_stars: I have updated the question with the corresponding code for the set up of the tableView cell. If it helps.. – viktor Jul 19 '15 at 21:37
  • Why has the messageCell an array of Message objects? Is there not a relationship 1 to 1 between cell and message? – LuckyStarr Jul 23 '15 at 20:53
  • @ lucky_starss: sorry for the late reply. I was doing some other testing and therefore played around with that. The array is entirely irrelevant there. I have resolved the issue by changing the method used to define my users and the cell characteristics attached to them. I have used an enum, and then a switch. It seems that it is more efficient than the if statement as it draws a full separation of both sides. I have assumed the if statement was not taken into account when designing the textView accordingly, as whichever the sender, the design of the textView was made randomly. – viktor Jul 24 '15 at 11:58

2 Answers2

0

One possible error can be in this line:

if message1.sender == PFUser.currentUser()?.username

where you are comparing different types since message1 is a String whereas PFUser.currentUser()?.username is an optional String. The right way to compare them is:

if let username = PFUser.currentUser()?.username where username == message1.sender {
    self.textView.textColor = UIColor.blueColor()
} else {
    self.textView.textColor = UIColor.greenColor()
}
LuckyStarr
  • 1,468
  • 2
  • 26
  • 39
  • Thanks a lot for the suggestion; unfortunately it doesnt solve the problem. All the textViews still switch as the sender changes. – viktor Jul 19 '15 at 10:30
0

This is because you are changing the text color of the entire UITextView with your code. See the following post for a start in the right direction: Is it possible to change the color of a single word in UITextView and UITextField

Community
  • 1
  • 1
rholmes
  • 4,064
  • 3
  • 25
  • 34
  • Thanks for the link. I ll dig into that direction and give it a shot. Will come back to you afterwards. – viktor Jul 19 '15 at 10:34
  • Nope, not the right thing. It has to do with the setting of the textView. I believe using "enum" and then a "switch" instead of "if" might do the trick... Thanks anyway – viktor Jul 19 '15 at 18:34