Git Product home page Git Product logo

Comments (2)

rsattar avatar rsattar commented on September 23, 2024

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!

from cltokeninputview.

darienalvarez avatar darienalvarez commented on September 23, 2024

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
}
}
}

from cltokeninputview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.