rsattar / CLTokenInputView

A replica of iOS's native contact bubbles UI
MIT License
504 stars 127 forks source link

How to customize font of tokens? #9

Closed mbalex99 closed 8 years ago

mbalex99 commented 8 years ago

I can't seem to find the correct area to customize the tokens... Any guidance?

rsattar commented 8 years ago

Hey @mbalex99, I've updated the podspec to 2.1.0. You can now style the UI using UIAppearance, like this:

[UITextField appearanceWhenContainedIn:[CLTokenInputView class], nil].font = [UIFont italicSystemFontOfSize:17]; 

Thanks!

darienalvarez commented 8 years ago

hello

i made this class but i can not change the font size

import Foundation import CLTokenInputView

class ContactTokenView: CLTokenInputView, UITableViewDelegate, UITableViewDataSource, CLTokenInputViewDelegate { private var contacts = [IContact]() private var filteredContacts = [IContact]() var selectedContacts = NSMutableArray()

private var contactsTableView: UITableView!

private let tableViewRowHeight: CGFloat = 60.0
private let tableViewMaxRows = 4

var showGenericParticipant = false

var heightConstraint: NSLayoutConstraint!

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    self.backgroundColor = UIColor.whiteColor()

    let color = UIColor.lightGrayColor().colorWithAlphaComponent(0.4)

    self.backgroundColor = UIColor.whiteColor()
    self.layer.cornerRadius = 5
    self.layer.borderColor = color.CGColor
    self.layer.borderWidth = 1
    self.delegate = self

    contactsTableView = UITableView()
    contactsTableView.registerClass(ContactItem.self, forCellReuseIdentifier: "ContactItem")
    contactsTableView.delegate = self
    contactsTableView.dataSource = self
    contactsTableView.frame = CGRectMake(0, 0, 0, 0)
    contactsTableView.rowHeight = tableViewRowHeight

    ContactModel.findAll(){ contacts, error in
        if error == nil {
            self.contacts = contacts
        }
    }
}

override func layoutSubviews() {
    super.layoutSubviews()

    for constraint in self.constraints {
        if constraint.firstAttribute == NSLayoutAttribute.Height {
            heightConstraint = constraint
        }
    }

    if heightConstraint == nil {
        heightConstraint = NSLayoutConstraint(item:self, attribute:NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute:NSLayoutAttribute.NotAnAttribute, multiplier:1.0, constant:44)
    }

    self.window?.addSubview(contactsTableView)
}

// UITableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredContacts.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ContactItem", forIndexPath:indexPath) as! ContactItem
    let contact = self.filteredContacts[indexPath.row]

    cell.backgroundColor = UIColor.blackColor()
    cell.contactName.numberOfLines = 0
    cell.contactName.text = contact.getName()

    if contact.getRole() == ContactRole.ROLE_PARTICIPANT {
        cell.contactIcon.image = UIImage(named: "ic_contact")
    } else if contact.getRole() == ContactRole.ROLE_GROUP {
        cell.contactIcon.image = UIImage(named: "ic_contact_group")
    } else if contact.isGeneric() {
        cell.contactIcon.image = UIImage(named: "ic_contact_generic")
    } else {
        cell.contactIcon.image = UIImage(named: "ic_contact_provider")
    }

    return cell
}

// UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.contactsTableView.deselectRowAtIndexPath(indexPath, animated: true)

    let contact = self.filteredContacts[indexPath.row]

    let token = ContactToken(displayText: contact.getName(), contact:contact)

    if self.editing {
        self.addToken(token);
    }
}

// CLTokenInputViewDelegate
func tokenInputView(view: CLTokenInputView, didChangeText text: String?) {
    if text!.isEmpty {
        self.filteredContacts.removeAll(keepCapacity: true)
    } else {
        self.filteredContacts = contacts.filter() {
            let contact = $0 as IContact
            if showGenericParticipant {
                return contact.getSearchableString().containsString(text!)
            }

            return (!contact.isGeneric()) && contact.getSearchableString().lowercaseString.containsString(text!.lowercaseString)
        }
    }

    let x = self.frame.minX
    let y = self.frame.maxY
    let w = self.frame.width

    var h:CGFloat = tableViewRowHeight * CGFloat(tableViewMaxRows)
    if filteredContacts.count < tableViewMaxRows {
        h = tableViewRowHeight * CGFloat(filteredContacts.count)
    }

    contactsTableView.frame = CGRectMake(x, y, w, h)

    self.contactsTableView.reloadData()
}

func tokenInputView(view: CLTokenInputView, didAddToken token: CLToken) {
    let contactToken = token as! ContactToken
    self.selectedContacts.addObject(contactToken.contact)
}

func tokenInputView(view: CLTokenInputView, didRemoveToken token: CLToken) {
    let contactToken = token as! ContactToken
    self.selectedContacts.removeObject(contactToken.contact)
}

func tokenInputView(view: CLTokenInputView, didChangeHeightTo height: CGFloat) {
    if let constraint = heightConstraint {
        constraint.constant = height
    } else {
        let frame = self.frame
        self.frame = CGRectMake(frame.minX, frame.minY, frame.width, height)
    }
}

// Token class
class ContactToken : CLToken {
    var contact: IContact

    init(displayText: String, contact: IContact!) {
        self.contact = contact
        let truncate = displayText.truncate(18, trailing: "...")
        super.init(displayText: truncate, context: nil)
    }
}

}

extension String { /// Truncates the string to length number of characters and /// appends optional trailing string if longer func truncate(length: Int, trailing: String? = nil) -> String { if self.characters.count > length { return self.substringToIndex(self.startIndex.advancedBy(length)) + (trailing ?? "") } else { return self } } }