I am working in an application where I have used "UITextField" as SEARCHBOX. when the user enter the search text in uitextfield UiTableView with sections will display the result, but I need to highlight the text entered in the textfield in the tableview. I have used attributed text but it is only possible within a range. So please help me an idea for implementing the highlighter.
myMutableString = NSMutableAttributedString(string: contactsList.familyName + contactsList.givenName, attributes: [NSAttributedStringKey.font:UIFont(name: "Catamaran", size: 14.0)!])
if contactsList.familyName.count >= 4 {
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black, range: NSRange(location:0,length:4))
}else {
myMutableString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black, range: NSRange(location:0,length:2))
}
cell?.profileName.attributedText = myMutableString
I tried to change foreground color but it just changes text color not background color.
I don't know whether this is the right solution or not, but you can try something like this.
If I understand your problem correctly,
highlihted textfield
Add a label globally
let label = UILabel()
Set label frame in viewDidLoad
label.frame.size = CGSize(width: 10, height: textfield.frame.size.height)// your settings width should be 10 or smaller
label.font = UIFont(name: "Halvetica", size: 14)
label.backgroundColor = UIColor.red
Set textfield text color to textfield background color
textfield.textColor = UIColor.white
Add textFieldEditingChanged method like this:
#IBAction func changed(_ sender: Any) {
label.removeFromSuperview()
label.text = textfield.text
textfield.addSubview(label)
label.sizeToFit()
}
Don't forget to set label and textfield fonts same. If you don't, textfield cursor may appear behind or front of label
UPDATE:
You should create attributed string with html
extension String {
var html2AttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: Data(utf8),
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
print("error: ", error)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}// you can find several way to do this just google it
}
Data source:
var dataSource = ["foo","foo1","foo2","foo3","foo4","foo5","foo6"]
Create variable for found result indexes at range in data source strings
var indexes : [Int:Range<String.Index>] = [:]
cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let string = dataSource[indexPath.row]
cell.textLabel?.attributedText = string.html2AttributedString
return cell
}
searchText editingChanged:
#IBAction func change(_ sender: UITextField) {
indexes.removeAll()
dataSource = ["foo","foo1","foo2","foo3","foo4","foo5","foo6"] // assign datasource again
for i in 0..<dataSource.count {
if let textRange = dataSource[i].range(of: textField.text!) {
indexes[i] = textRange
}
}
for (index,entry) in indexes {
let highlighted = "<text style='background-color:#FFFF00';>\(dataSource[index][entry])</text>"
dataSource[index].replaceSubrange(entry, with: highlighted)
}
tableView.reloadData()
}
You should change font and size in html after some searching.
Hope this helps
Related
So I have two UITextFields: name and amount and two UIButtons: income, expense.
When I press the expense button I want my amount textfield color to change to red or green if income button is pressed.
This only works if amount textfield is in focus, if name textfield is in focus, the color is not changed for amount.
Is there a way to change the color of the textfield if it's not in focus ?
edit:
Here is my code where I change the color:
#IBAction func typeBtnPressed(_ sender: UIButton) {
if sender.tag == Buttons.expense.rawValue {
amountTxt.textColor = .red
} else {
amountTxt.textColor = .green
}
}
After the textColor is set, the value needs to be reassigned to the textField.
textField.color = newColor
textField.text = text
It seems iOS uses by default attributedText and not text, that is why nothing is happening, and on focus it seems it takes your textColor into account, just do
let color: UIColor
if sender.tag == Buttons.expense.rawValue {
color = .red
} else {
color = .green
}
let attributedText = NSMutableAttributedString(attributedString: amountTxt.attributedText!)
attributedText.setAttributes([NSAttributedStringKey.foregroundColor : color], range: NSMakeRange(0, attributedText.length))
amountTxt.attributedText = attributedText
This will then work as soon as button is pressed
Swift 4 Xcode 10 edition
let attributedText = NSMutableAttributedString(attributedString: textField.attributedText!)
attributedText.setAttributes([NSAttributedString.Key.foregroundColor : UIColor.red], range: NSMakeRange(0, attributedText.length))
textField.attributedText = attributedText
I have been working on wrapping my head around the TextKit work flow for Swift for a couple of days using some of the great questions and answers here among others.
I'm trying to find the bounding rect of a substring found in a label, if the user touches that perform an action, and if not do nothing. Currently, my bounding rect is off. Any feedback is greatly appreciated! Thank you for your time.
TableView Delegate cellForRowAtIndex:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(termsCell, forIndexPath: indexPath) as! TableViewCell
let linkRangeIndex = attributedString.string.rangeOfString("link")
let linkRange = attributedString.string.NSRangeFromRange(linkRangeIndex!)
attributedString.setAttributes(linkAttributes, range: linkRange)
cell.termsLabel.attributedText = attributedString
cell.termsLabel.userInteractionEnabled = true
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = cell.termsLabel.lineBreakMode
textContainer.maximumNumberOfLines = cell.termsLabel.numberOfLines
let labelSize = cell.termsLabel.bounds.size
textContainer.size = labelSize
let tapGesture = UITapGestureRecognizer(target: self, action: "handleTapOnLabel:")
cell.termsLabel.addGestureRecognizer(tapGesture)
return cell
}
}
Handle Taps on Label Method:
What I'm attempting to do is get the bounding rect box of "link" portion of the main string and check if the users touches are with in that box (then preform an action). However my bounding rect box is off. It fires (a print method for testing) when I click on the right side of "with"
#IBAction func handleTapOnLabel(tapGesture: UITapGestureRecognizer) {
let glyphPointer = NSRangePointer()
let linkRangeIndex = attributedString.string.rangeOfString("link")
let linkRange = attributedString.string.NSRangeFromRange(linkRangeIndex!)
let myString = "A string"
myString.NSRangeFromRange(linkRangeIndex!)
let glyphRange = layoutManager.characterRangeForGlyphRange(linkRange, actualGlyphRange: glyphPointer)
print("glyphRange: \(glyphRange)")
let glyphRect = layoutManager.boundingRectForGlyphRange(glyphRange, inTextContainer: textContainer)
print("glyphRect: \(glyphRect)")
let touchPoint = tapGesture.locationOfTouch(0, inView: cell?.textLabel)
print(touchPoint)
// need range of points for label
if CGRectContainsPoint(glyphRect, locationOfTouchInLabel) {
print("I go home now")
} else {
print("no")
}
}
}
I have a very simple cell and I want the text to be 1.0 alpha when focused and 0.5 when unfocused. This works by overriding didUpdateFocusInContext
import Foundation
class SettingsTableViewCell: UITableViewCell {
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)
var textColor:UIColor?
if self.focused == true {
textColor = UIColor.blackColor().colorWithAlphaComponent(1.0)
} else {
textColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
}
let attributedTitle = self.textLabel?.attributedText?.mutableCopy() as! NSMutableAttributedString
let range = NSMakeRange(0, attributedTitle.length)
attributedTitle.addAttribute(NSForegroundColorAttributeName, value: textColor!, range: range)
self.textLabel?.attributedText = attributedTitle
}
}
HOWEVER, I don't want the white background when focused... how to get rid of it?
Set your cells' focusStyle to UITableViewCellFocusStyleCustom and UIKit won't do any default focus appearance at all on the cells: it'll be entirely up to you.
I am having some serious trouble getting my multiline label to show all of its lines. Often, the last line of the label simply does not appear, but it is apparent that the dynamically calculated cell height has taken in to account that it should have appeared, leaving around the appropriate amount of white space left over in my cell.
The affected label can display 1-7 lines depending on the data. I have played around with many various constraints to try and get it to display but regardless of what is on the last line, it just won't display.
The weird thing is, sometimes it will display when I segue in to the VC, but then when I use the segmented controller inside the VC to display different data and then go back again, the last line will again not display. The opposite happens frequently too (last line of label cutting off when I segue in to the VC, but then using the segmented controller inside the VC to change the displayed data and then go back, it will then display fine).
Things I have ensured: The label is set to word wrap, has line count of 0, has a height greater than or equal to the height of one of it's lines, its resistance and vertical content hugging is set to the highest of anything in the cell, and the width is set appropriately.
The below code is how I determine how many lines the label will have:
let descString = NSMutableAttributedString()
let bountyStart = NSMutableAttributedString(string: "Bounty: ", attributes: [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)])
let bountyDesc = NSMutableAttributedString(string: bounty.description)
descString.appendAttributedString(bountyStart)
descString.appendAttributedString(bountyDesc)
let bLine = NSMutableAttributedString(string: "\n\n", attributes: [NSFontAttributeName : UIFont.systemFontOfSize(2)])
descString.appendAttributedString(bLine)
if !(bounty.turtles == 0.0){
let turtleStart = NSMutableAttributedString(string: "Your turtle count: ")
let turtleAmount = NSMutableAttributedString(string: bounty.turtleCount.description)
descString.appendAttributedString(turtleStart)
descString.appendAttributedString(turtleAmount)
}
descriptionLabel.attributedText = descString
In the screen shot below, you can see that the height of the cell is being calculated appropriately but for some reason, the last line of the label just refuses to show. It should appear after the "is for noobs" line. I've manipulated the white space to appear after the line instead of elsewhere by setting the problem labels bottom to constraint to be greater than or equal, and all other top to bottom constraints as equal to.
Constraints of the problem label:
I've been stumped for quite a while on this one, and I'm starting to think it's not the constraints I've set but something much deeper. All though I would love to be proven wrong.
Here is my VC code.
import UIKit
class TrendingVC: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet weak var menubtn:UIBarButtonItem!
#IBOutlet var trendingTableView:UITableView!
var trendingToggle:Int = 0
let nwt = NWTrending()
let appUserId = NSUserDefaults.standardUserDefaults().stringForKey("UserId") ?? "1" //#TODO: remove ?? 1
var bountyArr: [Bounty] = []
var compArr: [Completion] = []
var peopleArr: [Person] = []
var userId: String = "0"
var username: String = ""
let bountyCellIdentifier = "BountyCellNew"
let personCellIdentifier = "PersonCell"
let completedCellIdentifier = "TrendingCompletedImageCell"
#IBAction func toggleTrending(sender:UISegmentedControl){
switch sender.selectedSegmentIndex{
case 0:
//loads the bounties on segmented control tab
trendingToggle=0
nwt.getTrendingBounties(appUserId, position: 0){(bountyArr, err) in //#TODO: change pos
self.bountyArr = bountyArr as [Bounty]
self.reloadTableViewContent()
}
case 1:
trendingToggle=1
nwt.getTrendingCompletions(appUserId, position: 0){(compArr, err) in
self.compArr = compArr as [Completion]
self.reloadTableViewContent()
}
case 2:
trendingToggle=2
nwt.getTrendingPeople(appUserId, position: 0){(peopleArr, err) in
self.peopleArr = peopleArr as [Person]
self.reloadTableViewContent()
}
default:
break
}
//reloadTableViewContent()
}
override func viewDidLoad() {
super.viewDidLoad()
trendingTableView.estimatedRowHeight = 300.0
trendingTableView.rowHeight = UITableViewAutomaticDimension
/******* Kyle Inserted *******/
//for followers and following back button text, you set it here for when you segue into that section
let backItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backItem
/******* END Kyle Inserted *******/
trendingTableView.allowsSelection = false;
trendingTableView.delegate = self
trendingTableView.dataSource = self
//sidebar code
if self.revealViewController() != nil {
menubtn.target = self.revealViewController()
menubtn.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
//loads the bounty on segue
nwt.getTrendingBounties(appUserId, position: 0){(bountyArr, err) in
self.bountyArr = bountyArr as [Bounty]
self.reloadTableViewContent()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//deselectAllRows()
}
func deselectAllRows() {
if let selectedRows = trendingTableView.indexPathsForSelectedRows() as? [NSIndexPath] {
for indexPath in selectedRows {
trendingTableView.deselectRowAtIndexPath(indexPath, animated: false)
}
}
}
func reloadTableViewContent() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.trendingTableView.reloadData()
println("reloading table view content")
self.trendingTableView.scrollRectToVisible(CGRectMake(0, 0, 1, 1), animated: false)
})
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(trendingTableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if trendingToggle == 0{
return bountyArr.count
}
else if trendingToggle == 1{
return compArr.count
}
else {
return peopleArr.count
}
}
func tableView(trendingTableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if trendingToggle == 0{
return bountyCellAtIndexPath(indexPath)
}
else if trendingToggle == 1{
return completedCellAtIndexPath(indexPath)
}
else{
return personCellAtIndexPath(indexPath)
}
}
//calls method to set and display each trending bounty cell
func bountyCellAtIndexPath(indexPath:NSIndexPath) -> BountyCellNew {
let cell = trendingTableView.dequeueReusableCellWithIdentifier(bountyCellIdentifier) as! BountyCellNew
var bounty = bountyArr[indexPath.row]
cell.setBountyCellTrending(bounty)
return cell
}
func completedCellAtIndexPath(indexPath:NSIndexPath) -> CompletedCell{
let cell = trendingTableView.dequeueReusableCellWithIdentifier(completedCellIdentifier) as! CompletedCell
var comp = compArr[indexPath.row]
cell.setTrendingCompletedCell(comp)
return cell
}
func personCellAtIndexPath(indexPath:NSIndexPath) -> PersonCell{
let cell = trendingTableView.dequeueReusableCellWithIdentifier(personCellIdentifier) as! PersonCell
var peop = peopleArr[indexPath.row]
cell.setTrendingPeopleCell(peop)
return cell
}
}
Unable to comment due to rep low.
This seems like a IB thing, very likely constraints.
Make sure that any component's top constraint placed under this multiline UILabel is set to the bottom of said UILabel.
For example, in this project of mine, both the TitleView and TimeAndDetailsView contain UILabels that span multiplelines. In order to allow autolayout to properly space things all constraints need to be in order, also notice how the top constraint of TimeAndDetails is the bottom of TitleView
Edit note:
Before you tableView.reloadData() type the following 2 lines, or if uncertain, put it inside viewDidLoad
tableView.estimatedRowHeight = 60 //Type your cell estimated height
tableView.rowHeight = UITableViewAutomaticDimensionhere
Went to sleep last night, ok so I decided to add your code to my details label from the storyboards picture I had posted before, and it seems to have displayed just fine. I have a few questions and pointers below.
let descString = NSMutableAttributedString()
let bountyStart = NSMutableAttributedString(string: "Bounty: ", attributes: [NSFontAttributeName : UIFont.boldSystemFontOfSize(15)])
let bountyDesc = NSMutableAttributedString(string: "this would be 'bounty.description and is set to size 40", attributes: [NSFontAttributeName : UIFont.boldSystemFontOfSize(40)])
descString.appendAttributedString(bountyStart)
descString.appendAttributedString(bountyDesc)
let bLine = NSMutableAttributedString(string: "\n\n", attributes: [NSFontAttributeName : UIFont.systemFontOfSize(10)])
descString.appendAttributedString(bLine)
let turtleStart = NSMutableAttributedString(string: "Your turtle count: ")
let turtleAmount = NSMutableAttributedString(string: "random number 1234 goes here")
descString.appendAttributedString(turtleStart)
descString.appendAttributedString(turtleAmount)
detailsLabel.attributedText = descString
Sadly, this is in a scrollview and not in a cell, so there is the first difference. My first question; is your descriptionLabel.attributedText supposed to display different fontscolors or fonttypes/sizes?, if not then I would say to just use descriptionLabel.text instead of descriptionLabel.attributedText.
Now the fact that there is white space but nothing showing us makes me wonder if bounty.turtleCount.description was previously set to color white somewhere else, or you are simply seeing the newlines you added \n\n and nothing is there because if !(bounty.turtles == 0.0) doesnt execute. Are you sure the that if statement is executed? place a breakpoint and follow along to make sure the values are appended to descString
If this is all correct, then my guess would still be on constraints.
Could you elaborate on, quote from you; "sometimes it will display when I segue in to the VC, but then when I use the segmented controller inside the VC to display different data and then go back again, the last line will again not display." what are you doing different in the seg control when loading the values and reloading the tableview that makes it not display, and under what conditions does it work properly when you segue.
I updated xcode and now this doesn't happen anymore.
I'm trying to make separate pieces of text UILabels clickable. What I'm looking for is commonly known as a hyperlink in web development.
Link 1
Link 2
Link 3
Each a tag is its own UILabel, and it would ideally open Safari to the specified href when the text between the tags is clicked.
I've found a bevy of resources on how to do this sort of thing in Objective-C, but they all seem unnecessarily complicated and don't translate well to Swift (they fit an Objective-C organizational structure that doesn't work well in Swift and goes against the recommended way of using the language).
Here are a few:
How to add hyperlink in iPhone app?
How to make a clickable link inside a NSTextField and Cocoa
Text as Hyperlink in Objective-C
If I had a 3 UILabels,
Item 1
Item 2
Item 3
then what would be the best "Swift-y" way to make each item open to a different URL in Safari?
I could create separate buttons for each, but the UILabels are programmatically populated, so I was thinking that making the text respond to taps might be a better option.
Swift 3
I created a LinkUILabel class in github:
https://github.com/jorgecsan/LinkUILabel
With this you only need add the url inspectable as the shows the image:
or assign the url variable programmatically:
linkUILabel.url = "www.example.com"
If you want to implement by your self also I found that solution!:)
using:
// This is the label
#IBOutlet weak var label: UILabel!
override func loadView() {
super.loadView()
// This is the key
let tap = UITapGestureRecognizer(target: self, action: #selector(self.onClicLabel(sender:)))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}
// And that's the function :)
func onClicLabel(sender:UITapGestureRecognizer) {
openUrl("http://www.google.com")
}
func openUrl(urlString:String!) {
let url = URL(string: urlString)!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
Hope it helps!:)
The One approach would be something like the following.
The assumptions are:
self.urls is a string array containing the urls associated with each UILabel.
Each UILabel tag has been set to the corresponding index in the array
labelTapped: is set as the touchUpInside handler for the labels.
import Foundation
import UIKit
class urltest {
var urls:[String]
init() {
self.urls=[String]() // Load URLs into here
}
#IBAction func labelTapped(sender:UILabel!) {
let urlIndex=sender.tag;
if (urlIndex >= 0 && urlIndex < self.urls.count) {
self.openUrl(self.urls[urlIndex]);
}
}
func openUrl(url:String!) {
let targetURL=NSURL.URLWithString(url)
let application=UIApplication.sharedApplication()
application.openURL(targetURL);
}
}
Hyperlink via UITextView
var termsConditionsTextView: UITextView = {
let view = UITextView()
view.backgroundColor = .clear
view.textAlignment = .left
let firstTitleString = "By registering for THIS_APP I agree with the "
let secondTitleString = "Terms & Conditions"
let finishTitleString = firstTitleString + secondTitleString
let attributedString = NSMutableAttributedString(string: finishTitleString)
attributedString.addAttribute(.link, value: "https://stackoverflow.com", range: NSRange(location: firstTitleString.count, length: secondTitleString.count))
view.attributedText = attributedString
view.textContainerInset = .zero
view.linkTextAttributes = [
.foregroundColor: UIColor.blue,
.underlineStyle: NSUnderlineStyle.single.isEmpty
]
view.font = view.font = UIFont(name: "YOUR_FONT_NAME", size: 16)
view.textColor = UIColor.black
return view }()